Java Code Examples for org.eclipse.jetty.server.Request#getHeader()

The following examples show how to use org.eclipse.jetty.server.Request#getHeader() . 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: RequestContextScope.java    From jetty-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public void enterScope(Context context, Request request, Object reason) {
  if (logger.isLoggable(Level.FINE)) {
    logger.fine("enterScope " + context);
  }
  if (request != null) {
    Integer depth = contextDepth.get();
    if (depth == null || depth.intValue() == 0) {
      contextDepth.set(1);
      String currentTraceId = (String) request.getAttribute(X_CLOUD_TRACE);
      if (currentTraceId == null) {
        // extract xCloud Trace in format: TRACE_ID/SPAN_ID;o=TRACE_TRUE
        String cloudTrace = request.getHeader(X_CLOUD_TRACE);
        if (cloudTrace != null) {
          int split = cloudTrace.indexOf('/');
          if (split < 0) {
            split = cloudTrace.indexOf(';');
          }
          String traceId = split >= 0 ? cloudTrace.substring(0, split) : cloudTrace;
          if (traceId != null) {
            currentTraceId = String.format("projects/%s/traces/%s", projectId, traceId);
            request.setAttribute(X_CLOUD_TRACE, currentTraceId);
            TraceLoggingEnhancer.setCurrentTraceId(currentTraceId);
          }
        }
      }
    } else {
      contextDepth.set(depth + 1);
    }
  }
}
 
Example 2
Source File: CrossOriginConstraintSecurityHandler.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
private static boolean isPreFlightRequest(Request request) {
    if(HttpMethods.OPTIONS.equalsIgnoreCase(request.getMethod())) {
        // If the origin does not match allowed the filter will skip anyway so don't bother checking it.
        if(request.getHeader(ORIGIN_HEADER) != null &&
           request.getHeader(CrossOriginFilter.ACCESS_CONTROL_REQUEST_METHOD_HEADER) != null) {
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: VmRuntimeWebAppContext.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the request was made over HTTPS. If so it modifies the request so that
 * {@code HttpServletRequest#isSecure()} returns true, {@code HttpServletRequest#getScheme()}
 * returns "https", and {@code HttpServletRequest#getServerPort()} returns 443. Otherwise it sets
 * the scheme to "http" and port to 80.
 *
 * @param request The request to modify.
 */
private void setSchemeAndPort(Request request) {
  String https = request.getHeader(VmApiProxyEnvironment.HTTPS_HEADER);
  String proto = request.getHeader(VmApiProxyEnvironment.X_FORWARDED_PROTO_HEADER);
  if ("on".equals(https) || "https".equals(proto)) {
    request.setSecure(true);
    request.setScheme(HttpScheme.HTTPS.toString());
    request.setAuthority(request.getServerName(), 443);
  } else {
    request.setSecure(false);
    request.setScheme(HttpScheme.HTTP.toString());
    request.setAuthority(request.getServerName(), defaultEnvironment.getServerPort());
  }
}
 
Example 4
Source File: TraceeClientHttpInterceptorSpringContextIT.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException,
	ServletException {
	final String incomingTraceeHeader = request.getHeader(TraceeConstants.TPIC_HEADER);
	collector.checkThat(incomingTraceeHeader, equalTo("before+Request=yip"));
	httpServletResponse.setHeader(TraceeConstants.TPIC_HEADER, "response+From+Server=yesSir");
	httpServletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
	request.setHandled(true);
}
 
Example 5
Source File: TraceeClientHttpInterceptorIT.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
	final String incomingTraceeHeader = request.getHeader(TraceeConstants.TPIC_HEADER);
	assertThat(incomingTraceeHeader, equalTo("before+Request=yip"));
	httpServletResponse.setHeader(TraceeConstants.TPIC_HEADER, "response+From+Server=yesSir");
	httpServletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
	request.setHandled(true);
}
 
Example 6
Source File: TraceeHttpInterceptorsIT.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
	final String incomingTraceeHeader = request.getHeader(TraceeConstants.TPIC_HEADER);

	assertThat(incomingTraceeHeader, equalTo("before+Request=yip"));

	httpServletResponse.setHeader(TraceeConstants.TPIC_HEADER, "responseFromServer=yes+Sir");
	httpServletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
	request.setHandled(true);
}
 
Example 7
Source File: TraceeHttpClientIT.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
	final String incomingTraceeHeader = request.getHeader(TraceeConstants.TPIC_HEADER);

	assertThat(incomingTraceeHeader, equalTo("beforeRequest=yip"));

	httpServletResponse.setHeader(TraceeConstants.TPIC_HEADER, "responseFromServer=yesSir");
	httpServletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
	request.setHandled(true);
}
 
Example 8
Source File: TraceeHttpInterceptorsIT.java    From tracee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
	final String incomingTraceeHeader = request.getHeader(TraceeConstants.TPIC_HEADER);

	assertThat(incomingTraceeHeader, equalTo("before+Request=yip"));

	httpServletResponse.setHeader(TraceeConstants.TPIC_HEADER, "responseFromServer=yes+Sir");
	httpServletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
	request.setHandled(true);
}
 
Example 9
Source File: JettyHttpServer.java    From product-private-paas with Apache License 2.0 5 votes vote down vote up
private boolean isValidRequest(Request request) {

        String authHeader = request.getHeader("Authorization");

        if (authHeader != null)
            return (request.getContentType().contains("application/json") && request.getHeader("Authorization").
                    contains("Basic"));
        else {
            return false;
        }

    }
 
Example 10
Source File: BasicToApiKeyAuthenticationFilter.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    Request httpRequest = (request instanceof Request) ?
            (Request) request :
            HttpConnection.getCurrentConnection().getHttpChannel().getRequest();

    // If there already is an API key present then perform no further action
    String apiKeyHeader = httpRequest.getHeader(ApiKeyRequest.AUTHENTICATION_HEADER);
    String apiKeyParam = httpRequest.getParameter(ApiKeyRequest.AUTHENTICATION_PARAM);
    if (!Strings.isNullOrEmpty(apiKeyHeader) || !Strings.isNullOrEmpty(apiKeyParam)) {
        chain.doFilter(request, response);
        return;
    }

    // If there is no authentication header then perform no further action
    String authenticationHeader = httpRequest.getHeader(HttpHeader.AUTHORIZATION.asString());
    if (Strings.isNullOrEmpty(authenticationHeader)) {
        chain.doFilter(request, response);
        return;
    }

    // Parse the authentication header to determine if it matches the replication user's credentials
    int space = authenticationHeader.indexOf(' ');
    if (space != -1 && "basic".equalsIgnoreCase(authenticationHeader.substring(0, space))) {
        try {
            String credentials = new String(
                    BaseEncoding.base64().decode(authenticationHeader.substring(space+1)), Charsets.UTF_8);

            for (Map.Entry<String, String> entry : _basicAuthToApiKeyMap.entrySet()) {
                if (entry.getKey().equals(credentials)) {
                    // The user name and password matches the replication credentials.  Insert the header.
                    HttpFields fields = httpRequest.getHttpFields();
                    fields.put(ApiKeyRequest.AUTHENTICATION_HEADER, entry.getValue());
                }
            }
        } catch (Exception e) {
            // Ok, the header wasn't formatted properly.  Do nothing.
        }
    }

    chain.doFilter(request, response);
}
 
Example 11
Source File: AthenzRequestLog.java    From athenz with Apache License 2.0 4 votes vote down vote up
@Override
public void log(Request request, Response response) {
    try {
        if (!this.isEnabled()) {
            return;
        }

        StringBuilder buf = TLS_BUILDER.get();
        buf.setLength(0);

        String addr = request.getHeader(HttpHeader.X_FORWARDED_FOR.toString());
        if (addr == null) {
            addr = request.getRemoteAddr();
        }
        buf.append(addr);
        buf.append(" - ");
        logPrincipal(buf, request);

        buf.append(" [");
        buf.append(logDateCache.format(request.getTimeStamp()));
        buf.append("] \"");

        append(buf, request.getMethod());
        buf.append(' ');

        logRequestUri(buf, request);
        buf.append(' ');

        append(buf, request.getProtocol());
        buf.append("\" ");

        logStatus(buf, response.getCommittedMetaData().getStatus());
        buf.append(' ');

        logLength(buf, response.getHttpChannel().getBytesWritten());
        buf.append(' ');

        logExtended(buf, request, response);

        buf.append(' ');
        logLength(buf, request.getContentLengthLong());

        buf.append(' ');
        buf.append(System.currentTimeMillis() - request.getTimeStamp());

        write(buf.toString());

    } catch (IOException ex) {
        LOG.warn(ex);
    }
}