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

The following examples show how to use org.eclipse.jetty.server.Request#getParameter() . 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: BuildReportFileUploaderTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(
    String s, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse)
    throws IOException {
  httpResponse.setStatus(200);
  request.setHandled(true);

  String uuid = request.getParameter("uuid");
  assertEquals(buildId.toString(), uuid);

  String traceFileKind = request.getParameter("trace_file_kind");
  assertEquals(TRACE_FILE_KIND, traceFileKind);

  String requestString =
      CharStreams.toString(new InputStreamReader(httpRequest.getInputStream()));

  // Ideally we would parse it as a proper multipart body request, but the HttpForTests'
  // servlets are not configured for parsing that, so let's just check at least that the
  // information is included.

  assertThat(
      "request contains file content", requestString, Matchers.containsString(TEST_CONTENT));

  assertThat(
      "request contains file name", requestString, Matchers.containsString(TEST_FILE_NAME));

  assertThat(
      "request contains form-data field name",
      requestString,
      Matchers.containsString("trace_file"));

  DataOutputStream out = new DataOutputStream(httpResponse.getOutputStream());
  out.writeBytes("{}");
}
 
Example 2
Source File: BuildReportUploaderTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(
    String s, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse)
    throws IOException {
  httpResponse.setStatus(200);
  request.setHandled(true);

  String uuid = request.getParameter("uuid");
  assertEquals(buildId.toString(), uuid);

  JsonNode reportReceived = ObjectMappers.READER.readTree(httpRequest.getParameter("data"));

  JsonNode currentConfig = reportReceived.get("currentConfig");
  assertNotNull(currentConfig);
  assertNotNull(currentConfig.get("rawConfig"));
  assertNotNull(currentConfig.get("configsMap"));

  JsonNode versionControlStats = reportReceived.get("versionControlStats");
  assertNotNull(versionControlStats);
  assertNotNull(versionControlStats.get("pathsChangedInWorkingDirectory"));
  assertNotNull(versionControlStats.get("currentRevisionId"));

  httpResponse.setContentType("application/json");
  httpResponse.setCharacterEncoding("utf-8");

  // this is an example json of what the real endpoint should return.
  String jsonResponse = "{ \"uri\": \"a.test.uri.com\"}";

  DataOutputStream out = new DataOutputStream(httpResponse.getOutputStream());
  out.writeBytes(jsonResponse);
}
 
Example 3
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 4
Source File: TraceDataHandler.java    From buck with Apache License 2.0 4 votes vote down vote up
private void doGet(Request baseRequest, HttpServletResponse response) throws IOException {
  String path = baseRequest.getPathInfo();
  Matcher matcher = ID_PATTERN.matcher(path);

  if (!matcher.matches()) {
    Responses.writeFailedResponse(baseRequest, response);
    return;
  }

  String id = matcher.group(1);

  response.setContentType(MediaType.JAVASCRIPT_UTF_8.toString());
  response.setStatus(HttpServletResponse.SC_OK);

  boolean hasValidCallbackParam = false;
  Writer responseWriter = response.getWriter();
  String callback = baseRequest.getParameter("callback");
  if (callback != null) {
    Matcher callbackMatcher = CALLBACK_PATTERN.matcher(callback);
    if (callbackMatcher.matches()) {
      hasValidCallbackParam = true;
      responseWriter.write(callback);
      responseWriter.write("(");
    }
  }

  responseWriter.write("[");

  Iterator<InputStream> traceStreams = buildTraces.getInputsForTraces(id).iterator();
  boolean isFirst = true;

  while (traceStreams.hasNext()) {
    if (!isFirst) {
      responseWriter.write(",");
    } else {
      isFirst = false;
    }
    try (InputStream input = traceStreams.next();
        InputStreamReader inputStreamReader = new InputStreamReader(input)) {
      CharStreams.copy(inputStreamReader, responseWriter);
    }
  }

  responseWriter.write("]");

  if (hasValidCallbackParam) {
    responseWriter.write(");\n");
  }

  response.flushBuffer();
  baseRequest.setHandled(true);
}