예제 #1
0
  @Test
  public void testReplaceMatrixParams() {
    UriBuilder ubu =
        UriBuilder.fromUri("http://localhost:8080/a/b/c;a=x;b=y")
            .replaceMatrixParam("a", "z", "zz");

    {
      URI uri = ubu.build();
      List<PathSegment> ps = UriComponent.decodePath(uri, true);
      MultivaluedMap<String, String> mps = ps.get(2).getMatrixParameters();
      List<String> a = mps.get("a");
      Assert.assertEquals(2, a.size());
      Assert.assertEquals("z", a.get(0));
      Assert.assertEquals("zz", a.get(1));
      List<String> b = mps.get("b");
      Assert.assertEquals(1, b.size());
      Assert.assertEquals("y", b.get(0));
    }

    {
      URI uri = ubu.replaceMatrixParam("a", "_z_", "_zz_").build();
      List<PathSegment> ps = UriComponent.decodePath(uri, true);
      MultivaluedMap<String, String> mps = ps.get(2).getMatrixParameters();
      List<String> a = mps.get("a");
      Assert.assertEquals(2, a.size());
      Assert.assertEquals("_z_", a.get(0));
      Assert.assertEquals("_zz_", a.get(1));
      List<String> b = mps.get("b");
      Assert.assertEquals(1, b.size());
      Assert.assertEquals("y", b.get(0));
    }

    {
      URI uri =
          UriBuilderImpl.fromUri("http://localhost:8080/a/b/c;a=x;b=y")
              .replaceMatrixParam("a", "z", "zz")
              .matrixParam("c", "c")
              .path("d")
              .build();

      List<PathSegment> ps = UriComponent.decodePath(uri, true);
      MultivaluedMap<String, String> mps = ps.get(2).getMatrixParameters();
      List<String> a = mps.get("a");
      Assert.assertEquals(2, a.size());
      Assert.assertEquals("z", a.get(0));
      Assert.assertEquals("zz", a.get(1));
      List<String> b = mps.get("b");
      Assert.assertEquals(1, b.size());
      Assert.assertEquals("y", b.get(0));
      List<String> c = mps.get("c");
      Assert.assertEquals(1, c.size());
      Assert.assertEquals("c", c.get(0));
    }
  }
예제 #2
0
  /**
   * Get the path of the current request relative to the application root (base) URI as a string.
   *
   * @param decode controls whether sequences of escaped octets are decoded ({@code true}) or not
   *     ({@code false}).
   * @return relative request path.
   */
  public String getPath(boolean decode) {
    if (decode) {
      if (decodedRelativePath != null) {
        return decodedRelativePath;
      }

      return decodedRelativePath =
          UriComponent.decode(encodedRelativePath(), UriComponent.Type.PATH);
    } else {
      return encodedRelativePath();
    }
  }
예제 #3
0
 @Test
 public void testReplaceMatrixParamsEmpty() {
   UriBuilder ubu =
       UriBuilder.fromUri("http://localhost:8080/a/b/c").replaceMatrixParam("a", "z", "zz");
   {
     URI uri = ubu.build();
     List<PathSegment> ps = UriComponent.decodePath(uri, true);
     MultivaluedMap<String, String> mps = ps.get(2).getMatrixParameters();
     List<String> a = mps.get("a");
     Assert.assertEquals(2, a.size());
     Assert.assertEquals("z", a.get(0));
     Assert.assertEquals("zz", a.get(1));
   }
 }
예제 #4
0
  public void testFolder() {
    RequestView rv;
    Map<String, List<String>> mvmap = UriComponent.decodeMatrix("/path;children", true);

    rv = new RequestView(RecordType.FOLDER, mvmap);
    assertTrue(rv.containsKey("children"));

    // Should pass
    mvmap = UriComponent.decodeMatrix("/path;children;stat=dataset", true);
    rv = new RequestView(RecordType.FOLDER, mvmap);
    assertTrue(rv.containsKey("stat"));

    mvmap = UriComponent.decodeMatrix("/path;children;v=3", true);
    rv = new RequestView(RecordType.FOLDER, mvmap);
    assertTrue(rv.getDatasetView().getVersionId() == 3);

    mvmap = UriComponent.decodeMatrix("/path;metadata", true);
    rv = new RequestView(RecordType.FOLDER, mvmap);
    assertTrue(rv.containsKey("metadata"));

    mvmap = UriComponent.decodeMatrix("/path;metadata;versions;pk", true);
    rv = new RequestView(RecordType.FOLDER, mvmap);
    assertTrue(rv.containsKey("pk"));

    // Should fail
    mvmap = UriComponent.decodeMatrix("/path;children", true);
    try {
      rv = new RequestView(RecordType.DATASET, mvmap);
    } catch (IllegalArgumentException ex) {
    }

    mvmap = UriComponent.decodeMatrix("/path;children;stat=dataset;v=2", true);
    try {
      rv = new RequestView(RecordType.FOLDER, mvmap);
    } catch (IllegalArgumentException ex) {
    }

    mvmap = UriComponent.decodeMatrix("/path;children;stat=dataset;v=2", true);
    try {
      rv = new RequestView(RecordType.FOLDER, mvmap);
    } catch (IllegalArgumentException ex) {
    }
  }
예제 #5
0
  @Test
  public void testReplaceQueryParamsEmpty() {
    UriBuilder ubu =
        UriBuilder.fromUri("http://localhost:8080/a/b/c")
            .replaceQueryParam("a", "z", "zz")
            .queryParam("c", "c");

    {
      URI uri = ubu.build();

      MultivaluedMap<String, String> qps = UriComponent.decodeQuery(uri, true);
      List<String> a = qps.get("a");
      Assert.assertEquals(2, a.size());
      Assert.assertEquals("z", a.get(0));
      Assert.assertEquals("zz", a.get(1));
      List<String> c = qps.get("c");
      Assert.assertEquals(1, c.size());
      Assert.assertEquals("c", c.get(0));
    }
  }
예제 #6
0
  @Test
  public void testReplaceQueryParams() {
    UriBuilder ubu =
        UriBuilder.fromUri("http://localhost:8080/a/b/c?a=x&b=y")
            .replaceQueryParam("a", "z", "zz")
            .queryParam("c", "c");

    {
      URI uri = ubu.build();

      MultivaluedMap<String, String> qps = UriComponent.decodeQuery(uri, true);
      List<String> a = qps.get("a");
      Assert.assertEquals(2, a.size());
      Assert.assertEquals("z", a.get(0));
      Assert.assertEquals("zz", a.get(1));
      List<String> b = qps.get("b");
      Assert.assertEquals(1, b.size());
      Assert.assertEquals("y", b.get(0));
      List<String> c = qps.get("c");
      Assert.assertEquals(1, c.size());
      Assert.assertEquals("c", c.get(0));
    }

    {
      URI uri = ubu.replaceQueryParam("a", "_z_", "_zz_").build();

      MultivaluedMap<String, String> qps = UriComponent.decodeQuery(uri, true);
      List<String> a = qps.get("a");
      Assert.assertEquals(2, a.size());
      Assert.assertEquals("_z_", a.get(0));
      Assert.assertEquals("_zz_", a.get(1));
      List<String> b = qps.get("b");
      Assert.assertEquals(1, b.size());
      Assert.assertEquals("y", b.get(0));
      List<String> c = qps.get("c");
      Assert.assertEquals(1, c.size());
      Assert.assertEquals("c", c.get(0));
    }

    // issue 257 - param is removed after setting it to null
    {
      URI u1 =
          UriBuilder.fromPath("http://localhost:8080")
              .queryParam("x", "10")
              .replaceQueryParam("x", null)
              .build();
      Assert.assertTrue(u1.toString().equals("http://localhost:8080"));

      URI u2 =
          UriBuilder.fromPath("http://localhost:8080")
              .queryParam("x", "10")
              .replaceQueryParam("x")
              .build();
      Assert.assertTrue(u2.toString().equals("http://localhost:8080"));
    }

    // issue 257 - IllegalArgumentException
    {
      boolean caught = false;

      try {
        URI u =
            UriBuilder.fromPath("http://localhost:8080")
                .queryParam("x", "10")
                .replaceQueryParam("x", "1", null, "2")
                .build();
      } catch (IllegalArgumentException iae) {
        caught = true;
      }

      Assert.assertTrue(caught);
    }
  }
예제 #7
0
  /**
   * Receives standard HTTP requests from the public {@code service} method and dispatches them to
   * the {@code do}<i>XXX</i> methods defined in this class. This method is an HTTP-specific version
   * of the {@link javax.servlet.Servlet#service} method. There's no need to override this method.
   *
   * @param request the {@link HttpServletRequest} object that contains the request the client made
   *     of the servlet
   * @param response the {@link HttpServletResponse} object that contains the response the servlet
   *     returns to the client
   * @throws IOException if an input or output error occurs while the servlet is handling the HTTP
   *     request
   * @throws ServletException if the HTTP request cannot be handled
   * @see javax.servlet.Servlet#service
   */
  @Override
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    /**
     * There is an annoying edge case where the service method is invoked for the case when the URI
     * is equal to the deployment URL minus the '/', for example
     * http://locahost:8080/HelloWorldWebApp
     */
    final String servletPath = request.getServletPath();
    String pathInfo = request.getPathInfo();
    StringBuffer requestURL = request.getRequestURL();
    String requestURI = request.getRequestURI();
    final boolean checkPathInfo = pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/");
    if (checkPathInfo && !request.getRequestURI().endsWith("/")) {
      // Only do this if the last segment of the servlet path does not contain '.'
      // This handles the case when the extension mapping is used with the servlet
      // see issue 506
      // This solution does not require parsing the deployment descriptor,
      // however still leaves it broken for the very rare case if a standard path
      // servlet mapping would include dot in the last segment (e.g. /.webresources/*)
      // and somebody would want to hit the root resource without the trailing slash
      int i = servletPath.lastIndexOf('/');
      if (servletPath.substring(i + 1).indexOf('.') < 0) {
        // TODO (+ handle request URL with invalid characters - see the creation of
        // absoluteUriBuilder bellow)
        //                if
        // (webComponent.getResourceConfig().getFeature(ResourceConfig.FEATURE_REDIRECT)) {
        //                    URI l = UriBuilder.fromUri(request.getRequestURL().toString()).
        //                            path("/").
        //                            replaceQuery(request.getQueryString()).build();
        //
        //                    response.setStatus(307);
        //                    response.setHeader("Location", l.toASCIIString());
        //                    return;
        //                } else {
        //                pathInfo = "/";
        //                requestURL.append("/");
        //                requestURI += "/";
        //                }
      }
    }

    /**
     * The HttpServletRequest.getRequestURL() contains the complete URI minus the query and fragment
     * components.
     */
    UriBuilder absoluteUriBuilder;
    try {
      absoluteUriBuilder = UriBuilder.fromUri(requestURL.toString());
    } catch (IllegalArgumentException iae) {
      final Response.Status badRequest = Response.Status.BAD_REQUEST;
      if (webComponent.configSetStatusOverSendError) {
        response.reset();
        response.setStatus(badRequest.getStatusCode(), badRequest.getReasonPhrase());
      } else {
        response.sendError(badRequest.getStatusCode(), badRequest.getReasonPhrase());
      }
      return;
    }

    /**
     * The HttpServletRequest.getPathInfo() and HttpServletRequest.getServletPath() are in decoded
     * form.
     *
     * <p>On some servlet implementations the getPathInfo() removed contiguous '/' characters. This
     * is problematic if URIs are embedded, for example as the last path segment. We need to work
     * around this and not use getPathInfo for the decodedPath.
     */
    final String decodedBasePath = request.getContextPath() + servletPath + "/";

    final String encodedBasePath = UriComponent.encode(decodedBasePath, UriComponent.Type.PATH);

    if (!decodedBasePath.equals(encodedBasePath)) {
      throw new ProcessingException(
          "The servlet context path and/or the "
              + "servlet path contain characters that are percent encoded");
    }

    final URI baseUri;
    final URI requestUri;
    try {
      baseUri = absoluteUriBuilder.replacePath(encodedBasePath).build();

      String queryParameters = request.getQueryString();
      if (queryParameters == null) {
        queryParameters = "";
      }

      requestUri = absoluteUriBuilder.replacePath(requestURI).replaceQuery(queryParameters).build();
    } catch (UriBuilderException ex) {
      final Response.Status badRequest = Response.Status.BAD_REQUEST;
      if (webComponent.configSetStatusOverSendError) {
        response.reset();
        response.setStatus(badRequest.getStatusCode(), badRequest.getReasonPhrase());
      } else {
        response.sendError(badRequest.getStatusCode(), badRequest.getReasonPhrase());
      }
      return;
    }

    service(baseUri, requestUri, request, response);
  }