Mistři v programování na #hovnokod

Java #229

Jednoduche parsovanie HTTP GET parametrov

private MyRequest parseRequestParameters(HttpServletRequest request) {
        String sessionId = "";
        String serverUrl = "";
        String targetUrl = "";
        String verifyLevelStr = "";
        final String requestUri = request.getQueryString();
        sessionId = getRequestParamValue(requestUri, "sessionId");
        serverUrl = getRequestParamValue(requestUri, "serverURL");
        targetUrl = getRequestParamValue(requestUri, "targetIURL");
        verifyLevelStr = getRequestParamValue(requestUri, "verify_level");
        if (StringUtils.isEmpty(verifyLevelStr)) {
            // set default value
            verifyLevelStr = "0";
        }

        int verifyLevel = Integer.parseInt(verifyLevelStr);

        return new MyRequest(sessionId, serverUrl, targetUrl, verifyLevel);
    }

    private String getRequestParamValue(String requestUri, String paramName) {
        final int paramNameStartIndex = requestUri.indexOf(paramName);
        if (paramNameStartIndex < 0) {
            return "";
        }
        final String paramValueSubstring = requestUri.substring(paramNameStartIndex + paramName.length() + 1);
        final int sessionIdIndex = paramValueSubstring.indexOf("sessionId") > -1
                ? paramValueSubstring.indexOf(SESSION_ID_PARAM)
                : Integer.MAX_VALUE;
        final int serverUrlIndex = paramValueSubstring.indexOf("serverURL") > -1
                ? paramValueSubstring.indexOf("serverURL")
                : Integer.MAX_VALUE;
        final int targetUrlIndex = paramValueSubstring.indexOf("targetURL") > -1
                ? paramValueSubstring.indexOf("targetURL")
                : Integer.MAX_VALUE;
        final int verifyLevelIndex = paramValueSubstring.indexOf("verify_level") > -1
                ? paramValueSubstring.indexOf("verify_level)
                : Integer.MAX_VALUE;

        int minIndex = Math.min(sessionIdIndex, Math.min(serverUrlIndex, Math.min(targetUrlIndex, verifyLevelIndex)));
        if (minIndex < 0 || minIndex == Integer.MAX_VALUE) {
            minIndex = paramValueSubstring.length();
        }

        String paramValue = paramValueSubstring.substring(0, minIndex);
        if (paramValue.endsWith("=")) {
            paramValue = paramValue.substring(0, paramValue.length() - 1);
        }
        if (paramValue.endsWith("%3D")) {
            paramValue = paramValue.substring(0, paramValue.length() - 3);
        }
        if (paramValue.endsWith("&amp;")) {
            paramValue = paramValue.substring(0, paramValue.length() - 5);
        }
        if (paramValue.endsWith("&")) {
            paramValue = paramValue.substring(0, paramValue.length() - 1);
        }

        try {
            return URLDecoder.decode(paramValue, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return paramValue;
        }
    }

Juraj Martinka,