Java Code Examples for javax.ws.rs.core.HttpHeaders#getRequestHeaders()

The following examples show how to use javax.ws.rs.core.HttpHeaders#getRequestHeaders() . 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: QueuedStatementResource.java    From presto with Apache License 2.0 6 votes vote down vote up
@ResourceSecurity(AUTHENTICATED_USER)
@POST
@Produces(APPLICATION_JSON)
public Response postStatement(
        String statement,
        @Context HttpServletRequest servletRequest,
        @Context HttpHeaders httpHeaders,
        @Context UriInfo uriInfo)
{
    if (isNullOrEmpty(statement)) {
        throw badRequest(BAD_REQUEST, "SQL statement is empty");
    }

    String remoteAddress = servletRequest.getRemoteAddr();
    Optional<Identity> identity = Optional.ofNullable((Identity) servletRequest.getAttribute(AUTHENTICATED_IDENTITY));
    MultivaluedMap<String, String> headers = httpHeaders.getRequestHeaders();

    SessionContext sessionContext = new HttpRequestSessionContext(headers, remoteAddress, identity, groupProvider);
    Query query = new Query(statement, sessionContext, dispatchManager);
    queries.put(query.getQueryId(), query);

    // let authentication filter know that identity lifecycle has been handed off
    servletRequest.setAttribute(AUTHENTICATED_IDENTITY, null);

    return Response.ok(query.getQueryResults(query.getLastToken(), uriInfo)).build();
}
 
Example 2
Source File: RestTracerAdapter.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * Decode baggage items from netty request to sofa request context
 *
 * @param request       netty http request
 * @param sofaRequest   rpc request holder
 */
private static void decodeBaggageItemsFromRequest(NettyHttpRequest request,
                                                  SofaRequest sofaRequest) {
    HttpHeaders headers = request.getHttpHeaders();
    // Decode baggage items
    MultivaluedMap<String, String> headerMaps = headers.getRequestHeaders();
    if (!RpcInvokeContext.isBaggageEnable() || headerMaps == null || headerMaps.isEmpty()) {
        return;
    }

    Map<String, String> baggageItems = new HashMap<String, String>();
    for (Map.Entry<String, List<String>> entry : headerMaps.entrySet()) {
        if (!entry.getKey().startsWith(RPC_REQUEST_BAGGAGE_PREFIX) ||
            entry.getValue() == null ||
            entry.getValue().isEmpty()) {
            continue;
        }

        String value = entry.getValue().get(0);
        String key = entry.getKey().substring(RPC_REQUEST_BAGGAGE_PREFIX_LEN);
        baggageItems.put(key, value);
    }
    sofaRequest.addRequestProp(RemotingConstants.RPC_REQUEST_BAGGAGE, baggageItems);

    BaggageResolver.pickupFromRequest(RpcInvokeContext.peekContext(), sofaRequest, true);
}
 
Example 3
Source File: TrellisRequest.java    From trellis with Apache License 2.0 6 votes vote down vote up
/**
 * Bundle together some request contexts.
 * @param request the Request object
 * @param uriInfo the URI information
 * @param headers the HTTP headers
 * @param secCtx the security context
 */
public TrellisRequest(final Request request, final UriInfo uriInfo, final HttpHeaders headers,
        final SecurityContext secCtx) {
    // Extract header values
    this.headers = headers.getRequestHeaders();
    this.acceptableMediaTypes = headers.getAcceptableMediaTypes();

    // Extract URI values
    this.parameters = uriInfo.getQueryParameters();
    this.baseUrl = buildBaseUrl(uriInfo.getBaseUri(), this.headers);
    this.path = uriInfo.getPathParameters().getFirst("path");
    this.trailingSlash = uriInfo.getPath().endsWith("/");

    // Extract request method
    this.method = request.getMethod();

    // Security context value
    this.secCtx = secCtx;
}
 
Example 4
Source File: HttpHeadersImplTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testUnmodifiableRequestHeaders() throws Exception {

    MetadataMap<String, String> headers =
        createHeader(HttpHeaders.ACCEPT_LANGUAGE,
                     "en;q=0.7, en-gb;q=0.8, da");
    Message m = createMessage(headers);
    m.put(HttpHeadersImpl.HEADER_SPLIT_PROPERTY, "true");
    HttpHeaders h = new HttpHeadersImpl(m);
    List<Locale> languages = h.getAcceptableLanguages();
    assertEquals(3, languages.size());
    languages.clear();
    languages = h.getAcceptableLanguages();
    assertEquals(3, languages.size());

    MultivaluedMap<String, String> rHeaders = h.getRequestHeaders();
    List<String> acceptL = rHeaders.get(HttpHeaders.ACCEPT_LANGUAGE);
    assertEquals(3, acceptL.size());

    rHeaders.clear();
}
 
Example 5
Source File: HttpHeadersMethodsResource.java    From tomee with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/requestheaders")
public String getHeaders(@Context HttpHeaders headers, @QueryParam("name") String headerName) {
    final MultivaluedMap<String, String> requestHeaders = headers.getRequestHeaders();
    final List<String> keys = new ArrayList<String>(requestHeaders.keySet());
    Collections.sort(keys);
    final StringBuilder sb = new StringBuilder("requestheaders:");
    for (final String k : keys) {
        sb.append(k).append("=");
        List<String> values = requestHeaders.get(k);
        if (values != null) {
            values = new ArrayList<String>(values);
            Collections.sort(values);
            sb.append(values).append(":");
        }
    }
    return sb.toString();
}
 
Example 6
Source File: HttpMessageHelper.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public static void writeHttpMessage(HttpHeaders headers, byte[] body, ClientMessage message) throws Exception {

      MultivaluedMap<String, String> hdrs = headers.getRequestHeaders();
      for (Entry<String, List<String>> entry : hdrs.entrySet()) {
         String key = entry.getKey();
         if (isTransferableHttpHeader(key) || isMessageProperty( key )) {
            List<String> vals = entry.getValue();
            String value = concatenateHeaderValue(vals);
            message.putStringProperty(HttpHeaderProperty.toPropertyName(key), value);
         }
      }
      message.putBooleanProperty(POSTED_AS_HTTP_MESSAGE, true);
      message.getBodyBuffer().writeBytes(body);
   }
 
Example 7
Source File: ODataExceptionWrapper.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public ODataExceptionWrapper(final UriInfo uriInfo, final HttpHeaders httpHeaders,
    final ODataErrorCallback errorCallback) {
  try {
    contentType = getContentType(uriInfo, httpHeaders).toContentTypeString();
    requestUri = uriInfo != null ? uriInfo.getRequestUri() : null;
  } catch (IllegalArgumentException e) {
    contentType = null;
    requestUri = null;
  }
  messageLocale = MessageService.getSupportedLocale(getLanguages(httpHeaders), DEFAULT_RESPONSE_LOCALE);
  httpRequestHeaders = httpHeaders.getRequestHeaders();
  callback = errorCallback;
}
 
Example 8
Source File: HttpHeadersImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHeaders() throws Exception {

    Message m = createMessage(createHeaders());
    m.put(HttpHeadersImpl.HEADER_SPLIT_PROPERTY, "true");

    HttpHeaders h = new HttpHeadersImpl(m);
    MultivaluedMap<String, String> hs = h.getRequestHeaders();
    List<String> acceptValues = hs.get("Accept");
    assertEquals(3, acceptValues.size());
    assertEquals("text/bar;q=0.6", acceptValues.get(0));
    assertEquals("text/*;q=1", acceptValues.get(1));
    assertEquals("application/xml", acceptValues.get(2));
    assertEquals(hs.getFirst("Content-Type"), "*/*");
}
 
Example 9
Source File: RequestUtils.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static String buildBaseURI(UriInfo uriInfo, HttpHeaders headers)
{
  URI base = uriInfo.getBaseUriBuilder().path(uriInfo.getPath()).build();

  if (log.isDebugEnabled() && headers != null && headers.getRequestHeaders() != null) {
    for (String key: headers.getRequestHeaders().keySet()) {
      log.debug("HTTP header name: " + key);
      for (String h: headers.getRequestHeader(key)) {
        log.debug("  " + h);
      }
    }
  }
  String scheme = base.getScheme();
  if (headers != null) {
    List<String> protos = headers.getRequestHeader("X-Forwarded-Proto");
    if (protos != null && protos.size() >= 1) {
      scheme = protos.get(0);
    }
  }
  List<String> proxies = (headers == null) ? null : headers.getRequestHeader("X-Forwarded-Host");
  if (proxies == null || proxies.size() < 1)
  {
    return base.toASCIIString();
  }
  else
  {
    try
    {
      URI proxy = new URI("http://" + proxies.get(0));  // the 1st proxy is the original one...

      return new URI(scheme, base.getRawUserInfo(), proxy.getHost(), proxy.getPort(),
          base.getRawPath(), base.getRawQuery(), base.getRawFragment()).toASCIIString();
    }
    catch (URISyntaxException e)
    {
      return base.toASCIIString();
    }
  }
}
 
Example 10
Source File: ODataExceptionWrapper.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
public ODataExceptionWrapper(final UriInfo uriInfo, final HttpHeaders httpHeaders, final ServletConfig servletConfig, final HttpServletRequest servletRequest) {
  contentType = getContentType(uriInfo, httpHeaders).toContentTypeString();
  messageLocale = MessageService.getSupportedLocale(getLanguages(httpHeaders), DEFAULT_RESPONSE_LOCALE);
  httpRequestHeaders = httpHeaders.getRequestHeaders();
  requestUri = uriInfo.getRequestUri();
  try {
    callback = getErrorHandlerCallbackFromServletConfig(servletConfig, servletRequest);
  } catch (Exception e) {
    throw new ODataRuntimeException("Exception occurred", e);
  }
}