Java Code Examples for com.sun.jersey.api.core.HttpRequestContext#getHeaderValue()

The following examples show how to use com.sun.jersey.api.core.HttpRequestContext#getHeaderValue() . 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: InstrumentedRequestDispatcher.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private String generateRequestResponseErrorMessage(HttpContext context, Exception e) {
    StringBuilder result = new StringBuilder();
    HttpRequestContext request = context.getRequest();
    HttpResponseContext response = context.getResponse();
    result.append("An error occurred during an HTTP request:\r\n");
    if (request != null) {
        String bodyLengthString = request.getHeaderValue("Content-Length");
        result.append("Request Path: " + request.getMethod().toUpperCase() + " " + request.getRequestUri().toString() + "\r\n");
        result.append("Request Content-Length: " + bodyLengthString + "\r\n");
        result.append("Request Headers:\r\n" + request.getRequestHeaders()
                .entrySet()
                .stream()
                .map(entry -> "\t" + entry.getKey() + ": " + entry.getValue() + "\r\n")
                .collect(Collectors.joining())
        );

        long bodyLength = Strings.isNullOrEmpty(bodyLengthString) ? 0 : Long.parseLong(bodyLengthString);
        if (bodyLength > 0 && ((ContainerRequest) request).getEntityInputStream().markSupported()) {
            try {
                ((ContainerRequest) request).getEntityInputStream().reset();
                result.append("Request Body:\r\n" + request.getEntity(String.class) + "\r\n");
            } catch (Exception ignore) {
            }
        }
    }

    result.append("Error response http code: " + response.getStatus() + "\r\n");
    result.append("Error message: " + e.getMessage() + "\r\n");
    result.append("Error stack trace :\r\n" + Throwables.getStackTraceAsString(e) + "\r\n");

    return result.toString();
}
 
Example 2
Source File: ApiKeyAuthenticationTokenGenerator.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
public ApiKeyAuthenticationToken createToken(HttpRequestContext context) {
    String apiKey = context.getHeaderValue(ApiKeyRequest.AUTHENTICATION_HEADER);
    if (Strings.isNullOrEmpty(apiKey)) {
        apiKey = context.getQueryParameters().getFirst(ApiKeyRequest.AUTHENTICATION_PARAM);
        if (Strings.isNullOrEmpty(apiKey)) {
            return null;
        }
    }

    return new ApiKeyAuthenticationToken(apiKey);
}
 
Example 3
Source File: RequestDecoder.java    From jersey-hmac-auth with Apache License 2.0 4 votes vote down vote up
private String getRequiredHeaderField(HttpRequestContext request, String name) {
    String value = request.getHeaderValue(name);
    checkArgument(!isNullOrEmpty(value), "Missing required HTTP header: " + name);
    return value;
}
 
Example 4
Source File: FileResource.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
/**
 * Validate and process range request.
 *
 * @param req
 *          The http request context
 * @param full
 *          The full range of the requested file
 * @param ranges
 *          The list where the processed ranges are stored
 * @param fileLength
 *          The length of the requested file
 * @param lastModifiedTime
 *          The last modified time of the file
 * @param eTag an ETag for the current state of the resource
 * @return null if the range request is valid or a ResponseBuilder set with
 * the status of 416 if the range request cannot be processed.  A returned
 * ResponseBuilder will include a Content-Range set to the file length.
 */
private ResponseBuilder processRangeHeader(final HttpRequestContext req,
        final Range full, final List<Range> ranges,
        final long fileLength, final long lastModifiedTime, final String eTag) {

    String range = req.getHeaderValue("Range");
    if (range != null && range.length() > 0) {

        // Range header should match format "bytes=n-n,n-n,n-n...". If not, then return 416.
        if (!range.matches("^bytes=\\d*-\\d*(,\\d*-\\d*)*$")) {
            logSecurityEvent(req.getRequestUri(), "Range header doesn't match format");
            return Response.status(416).header("Content-Range", "bytes */" + fileLength);// Required in 416.
        }

        // If-Range header should either match ETag or be greater then LastModified. If not,
        // then return full file.
        String ifRange = req.getHeaderValue("If-Range");
        if (ifRange != null && !ifRange.equals(eTag)) {
            try {
                long ifRangeTime = HttpHeaderReader.readDate(ifRange).getTime();
                if (ifRangeTime > 0 && ifRangeTime + 1000 < lastModifiedTime) {
                    ranges.add(full);
                }
            } catch (ParseException ignore) {
                ranges.add(full);
            }
        }

        // If any valid If-Range header, then process each part of byte range.
        if (ranges.isEmpty()) {
            for (String part : range.substring(6).split(",")) {
                // Assuming a file with fileLength of 100, the following examples returns bytes at:
                // 50-80 (50 to 80), 40- (40 to fileLength=100), -20 (fileLength-20=80 to fileLength=100).
                long start = sublong(part, 0, part.indexOf("-"));
                long end = sublong(part, part.indexOf("-") + 1, part.length());

                if (start == -1) {
                    start = Math.max(0, fileLength - end);
                    end = fileLength - 1;
                } else if (end == -1 || end > fileLength - 1) {
                    end = fileLength - 1;
                }

                // Check if Range is syntactically valid. If not, then return 416.
                if (start > end) {
                    logSecurityEvent(req.getRequestUri(), "If Range is not syntactically valid");
                    return Response.status(416).header("Content-Range", "bytes */" + fileLength); // Required in 416.
                }

                // Add range.
                ranges.add(new Range(start, end, fileLength));
            }
        }
    }
    return null;
}