Java Code Examples for play.mvc.Http#Header

The following examples show how to use play.mvc.Http#Header . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServletWrapper.java    From restcommander with Apache License 2.0 6 votes vote down vote up
protected static Map<String, Http.Header> getHeaders(HttpServletRequest httpServletRequest) {
    Map<String, Http.Header> headers = new HashMap<String, Http.Header>(16);

    Enumeration headersNames = httpServletRequest.getHeaderNames();
    while (headersNames.hasMoreElements()) {
        Http.Header hd = new Http.Header();
        hd.name = (String) headersNames.nextElement();
        hd.values = new ArrayList<String>();
        Enumeration enumValues = httpServletRequest.getHeaders(hd.name);
        while (enumValues.hasMoreElements()) {
            String value = (String) enumValues.nextElement();
            hd.values.add(value);
        }
        headers.put(hd.name.toLowerCase(), hd);
    }

    return headers;
}
 
Example 2
Source File: FunctionalTest.java    From restcommander with Apache License 2.0 6 votes vote down vote up
public static Response makeRequest(final Request request) {
    Response response = newResponse();
    makeRequest(request, response);

    if (response.status == 302) { // redirect
        // if Location-header is pressent, fix it to "look like" a functional-test-url
        Http.Header locationHeader = response.headers.get("Location");
        if (locationHeader != null) {
            String locationUrl = locationHeader.value();
            if (locationUrl.startsWith("http://localhost/")) {
                locationHeader.values.clear();
                locationHeader.values.add( locationUrl.substring(16));//skip 'http://localhost'
            }
        }
    }
    return response;
}
 
Example 3
Source File: Administration.java    From restcommander with Apache License 2.0 5 votes vote down vote up
@Before
protected static void check () {
    Http.Header auth = request.headers.get("authorization");
    if(auth != null) {
        String encodedPassword = auth.value().substring("Basic ".length());
        String password = new String(Codec.decodeBASE64(encodedPassword));
        String user = password.substring(0, password.indexOf(':'));
        String pwd = password.substring((user + ":").length());
        System.out.println("user is " + user + " , passwd is " + pwd);
        if (! pwd.equals(Play.configuration.getProperty("play.search.password","search")))
                unauthorized("You are not authorized");
     } else {
         unauthorized("You are not authorized");
     }
}
 
Example 4
Source File: FunctionalTest.java    From restcommander with Apache License 2.0 5 votes vote down vote up
/**
 * sends a GET request to the application under tests.
 * @param url relative url such as <em>"/products/1234"</em>
 * @param followRedirect indicates if request have to follow redirection (status 302)
 * @return the response
 */
public static Response GET(Object url, boolean followRedirect) {
    Response response = GET(url);
    if (Http.StatusCode.FOUND == response.status && followRedirect) {
        Http.Header redirectedTo = response.headers.get("Location");
        java.net.URL redirectedUrl = null;
        try {
            redirectedUrl = new java.net.URL(redirectedTo.value());
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        response = GET(redirectedUrl.getPath());
    }
    return response;
}
 
Example 5
Source File: PlayGrizzlyAdapter.java    From restcommander with Apache License 2.0 4 votes vote down vote up
public static Request parseRequest(GrizzlyRequest grizzlyRequest) throws Exception {
    Request request = new Http.Request();
    Request.current.set(request);
    URI uri = new URI(grizzlyRequest.getRequestURI());
    request.method = grizzlyRequest.getMethod().intern();
    request.path = uri.getPath();
    request.querystring = grizzlyRequest.getQueryString() == null ? "" : grizzlyRequest.getQueryString();

    Router.routeOnlyStatic(request);

    if (grizzlyRequest.getHeader("Content-Type") != null) {
        request.contentType = grizzlyRequest.getHeader("Content-Type").split(";")[0].trim().toLowerCase().intern();
    } else {
        request.contentType = "text/html".intern();
    }

    if (grizzlyRequest.getHeader("X-HTTP-Method-Override") != null) {
        request.method = grizzlyRequest.getHeader("X-HTTP-Method-Override").intern();
    }

    request.body = grizzlyRequest.getInputStream();
    request.secure = grizzlyRequest.isSecure();

    request.url = uri.toString() + (grizzlyRequest.getQueryString() == null ? "" : "?" + grizzlyRequest.getQueryString());
    request.host = grizzlyRequest.getHeader("host");
    if (request.host.contains(":")) {
        request.port = Integer.parseInt(request.host.split(":")[1]);
        request.domain = request.host.split(":")[0];
    } else {
        request.port = 80;
        request.domain = request.host;
    }

    request.remoteAddress = grizzlyRequest.getRemoteAddr();

    if (Play.configuration.containsKey("XForwardedSupport") && grizzlyRequest.getHeader("X-Forwarded-For") != null) {
        if (!Arrays.asList(Play.configuration.getProperty("XForwardedSupport", "127.0.0.1").split(",")).contains(request.remoteAddress)) {
            throw new RuntimeException("This proxy request is not authorized");
        } else {
            request.secure = ("https".equals(Play.configuration.get("XForwardedProto")) || "https".equals(grizzlyRequest.getHeader("X-Forwarded-Proto")) || "on".equals(grizzlyRequest.getHeader("X-Forwarded-Ssl")));
            if (Play.configuration.containsKey("XForwardedHost")) {
                request.host = (String) Play.configuration.get("XForwardedHost");
            } else if (grizzlyRequest.getHeader("X-Forwarded-Host") != null) {
                request.host = grizzlyRequest.getHeader("X-Forwarded-Host");
            }
            if (grizzlyRequest.getHeader("X-Forwarded-For") != null) {
                request.remoteAddress = grizzlyRequest.getHeader("X-Forwarded-For");
            }
        }
    }

    Enumeration headersNames = grizzlyRequest.getHeaderNames();
    while (headersNames.hasMoreElements()) {
        Http.Header hd = new Http.Header();
        hd.name = (String) headersNames.nextElement();
        hd.values = new ArrayList<String>();
        Enumeration enumValues = grizzlyRequest.getHeaders(hd.name);
        while (enumValues.hasMoreElements()) {
            String value = (String) enumValues.nextElement();
            hd.values.add(value);
        }
        request.headers.put(hd.name.toLowerCase(), hd);
    }

    request.resolveFormat();

    Cookie[] cookies = grizzlyRequest.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            Http.Cookie playCookie = new Http.Cookie();
            playCookie.name = cookie.getName();
            playCookie.path = cookie.getPath();
            playCookie.domain = cookie.getDomain();
            playCookie.secure = cookie.getSecure();
            playCookie.value = cookie.getValue();
            playCookie.maxAge = cookie.getMaxAge();
            request.cookies.put(playCookie.name, playCookie);
        }
    }

    request._init();

    return request;
}