public static ResteasyUriInfo extractUriInfo(HttpExchange exchange) { String host = exchange.getLocalAddress().getHostName(); if (exchange.getLocalAddress().getPort() != 80 && exchange.getLocalAddress().getPort() != 443) { host += ":" + exchange.getLocalAddress().getPort(); } String uri = exchange.getRequestURI().toString(); String protocol = exchange.getHttpContext().getServer() instanceof HttpsServer ? "https" : "http"; URI absoluteURI = URI.create(protocol + "://" + host + uri); String contextPath = exchange.getHttpContext().getPath(); String path = PathHelper.getEncodedPathInfo(absoluteURI.getRawPath(), contextPath); if (!path.startsWith("/")) { path = "/" + path; } URI baseURI = absoluteURI; if (!path.trim().equals("")) { String tmpContextPath = contextPath; if (!tmpContextPath.endsWith("/")) tmpContextPath += "/"; baseURI = UriBuilder.fromUri(absoluteURI).replacePath(tmpContextPath).replaceQuery(null).build(); } else { baseURI = UriBuilder.fromUri(absoluteURI).replaceQuery(null).build(); } URI relativeURI = UriBuilder.fromUri(path).replaceQuery(absoluteURI.getRawQuery()).build(); // System.out.println("path: " + path); // System.out.println("query string: " + request.getQueryString()); ResteasyUriInfo uriInfo = new ResteasyUriInfo(baseURI, relativeURI); return uriInfo; }
public static UriInfoImpl extractUriInfo(HttpServletRequest request, String servletPrefix) { String contextPath = request.getContextPath(); if (servletPrefix != null && servletPrefix.length() > 0) { if (!contextPath.endsWith("/") && !servletPrefix.startsWith("/")) contextPath += "/"; contextPath += servletPrefix; } URI absolutePath = null; try { URL absolute = new URL(request.getRequestURL().toString()); UriBuilderImpl builder = new UriBuilderImpl(); builder.scheme(absolute.getProtocol()); builder.host(absolute.getHost()); builder.port(absolute.getPort()); builder.path(absolute.getPath()); builder.replaceQuery(absolute.getQuery()); absolutePath = builder.build(); } catch (MalformedURLException e) { throw new RuntimeException(e); } String path = PathHelper.getEncodedPathInfo(absolutePath.getRawPath(), contextPath); List<PathSegment> pathSegments = PathSegmentImpl.parseSegments(path, false); URI baseURI = absolutePath; if (!path.trim().equals("")) { String tmpContextPath = contextPath; if (!tmpContextPath.endsWith("/")) tmpContextPath += "/"; baseURI = UriBuilder.fromUri(absolutePath).replacePath(tmpContextPath).build(); } // System.out.println("path: " + path); // System.out.println("query string: " + request.getQueryString()); UriInfoImpl uriInfo = new UriInfoImpl(absolutePath, baseURI, path, request.getQueryString(), pathSegments); return uriInfo; }