Package org.apache.pivot.web

Examples of org.apache.pivot.web.QueryDictionary



public class QueryDictionaryTest {
    @Test(expected=IndexOutOfBoundsException.class)
    public void testQueryDictionary() {
        QueryDictionary dict = new QueryDictionary();

        assertNull(dict.get("key"));

        dict.put("key", "value");
        assertNotNull(dict.get("key"));

        assertEquals("value", dict.get("key", 0));

        assertEquals(1, dict.getLength("key"));

        dict.get("key", 1);

        assertEquals("value", dict.put("key", "value2"));
        assertEquals("value2", dict.get("key"));

        dict.add("key", "another value");

        assertEquals("another value", dict.get("key", 1));

        assertEquals(0, dict.getLength("nokey"));

        assertEquals(0, dict.add("key2", "new value"));

        dict.insert("key", "yet another value", 0);

        assertEquals(3, dict.getLength("key"));

        dict.insert("key", "bad value", 10);

        assertEquals("yet another value", dict.remove("key"));
        assertNull(dict.remove("key"));

        dict.add("key2", "2nd value");
        assertEquals("new value", dict.remove("key2", 0));

        dict.remove("key2", 10);

        dict.add("key3", "something");

        Set<String> validKeys = new HashSet<String>();
        validKeys.add("key2");
        validKeys.add("key3");

        for (String s : dict) {
            assertTrue(s, validKeys.remove(s));
        }

        assertEquals(0, validKeys.size());

        assertTrue(dict.containsKey("key2"));

        dict.clear();
    }
View Full Code Here


    @Override
    @SuppressWarnings("unchecked")
    protected void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        try {
            parameters.set(new QueryDictionary());
            requestHeaders.set(new QueryDictionary());
            responseHeaders.set(new QueryDictionary());

            boolean proceed = true;

            if (authenticationRequired) {
                String authorization = request.getHeader("Authorization");

                if (authorization == null) {
                    proceed = false;
                    doUnauthorized(request, response);
                } else {
                    String encodedCredentials = authorization.substring
                        (BASIC_AUTHENTICATION_TAG.length() + 1);
                    String decodedCredentials = new String(Base64.decode(encodedCredentials));
                    String[] credentialsPair = decodedCredentials.split(":");

                    String username = credentialsPair.length > 0 ? credentialsPair[0] : "";
                    String password = credentialsPair.length > 1 ? credentialsPair[1] : "";

                    credentials.set(new Credentials(username, password));
                }
            }

            if (proceed) {
                // Extract our location context
                try {
                    URL url = new URL(request.getRequestURL().toString());
                    String requestURI = request.getRequestURI();
                    String requestContext = request.getContextPath();

                    hostname.set(url.getHost());
                    contextPath.set(requestContext);
                    queryPath.set(requestURI);
                    port.set(request.getLocalPort());
                    secure.set(url.getProtocol().equalsIgnoreCase(HTTPS_PROTOCOL));
                    method.set(Method.valueOf(request.getMethod().toUpperCase()));

                    if (requestURI.startsWith(requestContext)) {
                        queryPath.set(requestURI.substring(requestContext.length()));
                    }
                } catch (MalformedURLException exception) {
                    throw new ServletException(exception);
                }

                // Copy the query string into our arguments dictionary
                String queryString = request.getQueryString();
                if (queryString != null) {
                    QueryDictionary parametersDictionary = parameters.get();
                    String[] pairs = queryString.split("&");

                    for (int i = 0, n = pairs.length; i < n; i++) {
                        String[] pair = pairs[i].split("=");

                        String key = URLDecoder.decode(pair[0], URL_ENCODING);
                        String value = URLDecoder.decode((pair.length > 1) ? pair[1] : "", URL_ENCODING);

                        parametersDictionary.add(key, value);
                    }
                }

                // Copy the request headers into our request properties dictionary
                QueryDictionary requestHeaderDictionary = requestHeaders.get();
                Enumeration<String> headerNames = request.getHeaderNames();
                while (headerNames.hasMoreElements()) {
                    String headerName = headerNames.nextElement();
                    String headerValue = request.getHeader(headerName);

                    requestHeaderDictionary.add(headerName, headerValue);
                }

                if (authenticationRequired) {
                    try {
                        authorize();
View Full Code Here

        response.setContentLength(0);
        response.flushBuffer();
    }

    private void setResponseHeaders(HttpServletResponse response) {
        QueryDictionary responseHeaderDictionary = responseHeaders.get();

        for (String key : responseHeaderDictionary) {
            for (int i = 0, n = responseHeaderDictionary.getLength(key); i < n; i++) {
                response.addHeader(key, responseHeaderDictionary.get(key, i));
            }
        }
    }
View Full Code Here

                secure.set(url.getProtocol().equalsIgnoreCase(HTTPS_PROTOCOL));
            } catch (MalformedURLException exception) {
                throw new ServletException(exception);
            }

            parameters.set(new QueryDictionary(true));
            requestHeaders.set(new QueryDictionary(false));
            responseHeaders.set(new QueryDictionary(false));

            // Copy the query string into the arguments dictionary
            String queryString = request.getQueryString();
            if (queryString != null) {
                QueryDictionary parametersDictionary = parameters.get();
                String[] pairs = queryString.split("&");

                for (int i = 0, n = pairs.length; i < n; i++) {
                    String[] pair = pairs[i].split("=");

                    String key = URLDecoder.decode(pair[0], URL_ENCODING);
                    String value = URLDecoder.decode((pair.length > 1) ? pair[1] : "", URL_ENCODING);

                    parametersDictionary.add(key, value);
                }
            }

            // Copy the request headers into the request properties dictionary
            QueryDictionary requestHeaderDictionary = requestHeaders.get();
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                String headerValue = request.getHeader(headerName);

                requestHeaderDictionary.add(headerName, headerValue);
            }

            // Prepare the servlet for request processing
            prepare();
View Full Code Here

        return path;
    }

    private void setResponseHeaders(HttpServletResponse response) {
        QueryDictionary responseHeaderDictionary = responseHeaders.get();

        for (String key : responseHeaderDictionary) {
            for (int i = 0, n = responseHeaderDictionary.getLength(key); i < n; i++) {
                response.addHeader(key, responseHeaderDictionary.get(key, i));
            }
        }
    }
View Full Code Here

                secure.set(url.getProtocol().equalsIgnoreCase(HTTPS_PROTOCOL));
            } catch (MalformedURLException exception) {
                throw new ServletException(exception);
            }

            parameters.set(new QueryDictionary(true));
            requestHeaders.set(new QueryDictionary(false));
            responseHeaders.set(new QueryDictionary(false));

            // Copy the query string into the arguments dictionary
            String queryString = request.getQueryString();
            if (queryString != null) {
                QueryDictionary parametersDictionary = parameters.get();
                String[] pairs = queryString.split("&");

                for (int i = 0, n = pairs.length; i < n; i++) {
                    String[] pair = pairs[i].split("=");

                    String key = URLDecoder.decode(pair[0], URL_ENCODING);
                    String value = URLDecoder.decode((pair.length > 1) ? pair[1] : "", URL_ENCODING);

                    parametersDictionary.add(key, value);
                }
            }

            // Copy the request headers into the request properties dictionary
            QueryDictionary requestHeaderDictionary = requestHeaders.get();
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                String headerValue = request.getHeader(headerName);

                requestHeaderDictionary.add(headerName, headerValue);
            }

            // Prepare the servlet for request processing
            prepare();
View Full Code Here

        return path;
    }

    private void setResponseHeaders(HttpServletResponse response) {
        QueryDictionary responseHeaderDictionary = responseHeaders.get();

        for (String key : responseHeaderDictionary) {
            for (int i = 0, n = responseHeaderDictionary.getLength(key); i < n; i++) {
                response.addHeader(key, responseHeaderDictionary.get(key, i));
            }
        }
    }
View Full Code Here

                secure.set(url.getProtocol().equalsIgnoreCase(HTTPS_PROTOCOL));
            } catch (MalformedURLException exception) {
                throw new ServletException(exception);
            }

            parameters.set(new QueryDictionary(true));
            requestHeaders.set(new QueryDictionary(false));
            responseHeaders.set(new QueryDictionary(false));

            // Copy the query string into the arguments dictionary
            String queryString = request.getQueryString();
            if (queryString != null) {
                QueryDictionary parametersDictionary = parameters.get();
                String[] pairs = queryString.split("&");

                for (int i = 0, n = pairs.length; i < n; i++) {
                    String[] pair = pairs[i].split("=");

                    String key = URLDecoder.decode(pair[0], URL_ENCODING);
                    String value = URLDecoder.decode((pair.length > 1) ? pair[1] : "", URL_ENCODING);

                    parametersDictionary.add(key, value);
                }
            }

            // Copy the request headers into the request properties dictionary
            QueryDictionary requestHeaderDictionary = requestHeaders.get();
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                String headerValue = request.getHeader(headerName);

                requestHeaderDictionary.add(headerName, headerValue);
            }

            // Prepare the servlet for request processing
            prepare();
View Full Code Here

        return path;
    }

    private void setResponseHeaders(HttpServletResponse response) {
        QueryDictionary responseHeaderDictionary = responseHeaders.get();

        for (String key : responseHeaderDictionary) {
            for (int i = 0, n = responseHeaderDictionary.getLength(key); i < n; i++) {
                response.addHeader(key, responseHeaderDictionary.get(key, i));
            }
        }
    }
View Full Code Here


public class QueryDictionaryTest {
    @Test(expected=IndexOutOfBoundsException.class)
    public void testQueryDictionary() {
        QueryDictionary dict = new QueryDictionary(true);

        assertNull(dict.get("key"));

        dict.put("key", "value");
        assertNotNull(dict.get("key"));

        assertEquals("value", dict.get("key", 0));

        assertEquals(1, dict.getLength("key"));

        dict.get("key", 1);

        assertEquals("value", dict.put("key", "value2"));
        assertEquals("value2", dict.get("key"));

        dict.add("key", "another value");

        assertEquals("another value", dict.get("key", 1));

        assertEquals(0, dict.getLength("nokey"));

        assertEquals(0, dict.add("key2", "new value"));

        dict.insert("key", "yet another value", 0);

        assertEquals(3, dict.getLength("key"));

        dict.insert("key", "bad value", 10);

        assertEquals("yet another value", dict.remove("key"));
        assertNull(dict.remove("key"));

        dict.add("key2", "2nd value");
        assertEquals("new value", dict.remove("key2", 0));

        dict.remove("key2", 10);

        dict.add("key3", "something");

        Set<String> validKeys = new HashSet<String>();
        validKeys.add("key2");
        validKeys.add("key3");

        for (String s : dict) {
            assertTrue(s, validKeys.remove(s));
        }

        assertEquals(0, validKeys.size());

        assertTrue(dict.containsKey("key2"));

        dict.clear();
    }
View Full Code Here

TOP

Related Classes of org.apache.pivot.web.QueryDictionary

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.