public void handle(Request request, final Response response) { if (!Tracker.ANNOUNCE_URL.equals(request.getPath().toString())) { response.setCode(404); response.setText("Not Found"); response.setDate("Date", System.currentTimeMillis()); if ("GET".equalsIgnoreCase(request.getMethod())) { myRequestProcessor.process(request.getAddress().toString(), request.getClientAddress().getAddress().getHostAddress(), getRequestHandler(response)); } else { myMultiAnnounceRequestProcessor.process(request.getContent(), request.getAddress().toString(), request.getClientAddress().getAddress().getHostAddress(), getRequestHandler(response));
@Override public void handle(final Request request, final Response response) { final ResponseWriter responseWriter = new ResponseWriter(response, scheduler); final URI baseUri = getBaseUri(request); final URI requestUri = getRequestUri(request, baseUri); try { final ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, request.getMethod(), getSecurityContext(request), new MapPropertiesDelegate()); requestContext.setEntityStream(request.getInputStream()); for (final String headerName : request.getNames()) { requestContext.headers(headerName, request.getValue(headerName)); } requestContext.setWriter(responseWriter); requestContext.setRequestScopedInitializer(injectionManager -> { injectionManager.<Ref<Request>>getInstance(RequestTYPE).set(request); injectionManager.<Ref<Response>>getInstance(ResponseTYPE).set(response); }); appHandler.handle(requestContext); } catch (final Exception ex) { throw new RuntimeException(ex); } finally { if (!responseWriter.isSuspended()) { close(response); } } }
private URI getRequestUri(final Request request, final URI baseUri) { try { final String serverAddress = getServerAddress(baseUri); String uri = ContainerUtils.getHandlerPath(request.getTarget()); final String queryString = request.getQuery().toString(); if (queryString != null) { uri = uri + "?" + ContainerUtils.encodeUnsafeCharacters(queryString); } return new URI(serverAddress + uri); } catch (URISyntaxException ex) { throw new IllegalArgumentException(ex); } }
private URI getBaseUri(final Request request) { try { final String hostHeader = request.getValue("Host"); if (hostHeader != null) { final String scheme = request.isSecure() ? "https" : "http"; return new URI(scheme + "://" + hostHeader + "/"); } else { final Address address = request.getAddress(); return new URI(address.getScheme(), null, address.getDomain(), address.getPort(), "/", null, null); } } catch (final URISyntaxException ex) { throw new IllegalArgumentException(ex); } }
public static FullHttpRequest convert(final Request request) { byte[] data = null; try { final InputStream inputStream = request.getInputStream(); try { data = IOUtils.toByteArray(inputStream); } finally { inputStream.close(); } } catch (final IOException e) { LOGGER.error("IOException when getting request content.", e); } final FullHttpRequestImpl httpRequest = new FullHttpRequestImpl(); httpRequest.domain(request.getAddress().getDomain()); httpRequest.port(request.getAddress().getPort()); httpRequest.method(Method.valueOf(request.getMethod())); httpRequest.path(request.getPath().getPath()); if (data.length > 0) { httpRequest.content(data); } for (final String headerField : request.getNames()) { for (final String headerFieldValue : request.getValues(headerField)) { httpRequest.httpMessageHeader(headerField, headerFieldValue); } } for (final Entry<String, String> entry : request.getQuery().entrySet()) { httpRequest.queryParameter(entry.getKey(), entry.getValue()); } return httpRequest; }
public void handle(Request req, Response resp) { try { if (req.getPath().getPath().contains("/redirect/")) { resp.setCode(303); resp.add("Location", "/"); } else { if (req.getPath().getPath().contains("/timeout/")) { try { Thread.sleep(500); if (req.getPath().getPath().contains("/gzip/")) { if (!"gzip".equals(req.getValue("Accept-Encoding"))) { throw new IllegalStateException("Should accept gzip"); gzipOutputStream.close(); } else { resp.getPrintStream().append("agent=" + req.getValues("User-Agent").get(0));
/** * This can be used to get the HTTP method for this request. The * HTTP specification RFC 2616 specifies the HTTP request methods * in section 9, Method Definitions. Typically this will be a * GET, POST or a HEAD method, although any string is possible. * * @return the request method for this request message */ public String getMethod() { return request.getMethod(); }
@Override public String getAbsoluteUrl() { String s = baseRequest.getTarget(); return s; } else { String host = baseRequest.getValue("Host"); Address a = baseRequest.getAddress(); if (host == null) { host = a.getDomain(); if (baseRequest.isSecure()) { s = "https"; } else { s = s + ":" + a.getPort(); s = s + baseRequest.getTarget();
@Override public InputStream getRequestEntityStream(long size) { try { return this.request.getInputStream(); } catch (Exception ex) { return null; } }
public void handle(Request request, Response response) { WebApplication target = application; final URI baseUri = getBaseUri(request); final URI requestUri = baseUri.resolve(request.getTarget()); try { final ContainerRequest cRequest = new ContainerRequest( target, request.getMethod(), baseUri, requestUri, getHeaders(request), request.getInputStream()); target.handleRequest(cRequest, new Writer(request, response)); } catch (Exception ex) { throw new RuntimeException(ex); } finally { close(response); } }
public void handle( Request req, Response resp ) { try { PrintStream out = resp.getPrintStream(1024); String address = req.getAddress().toString(); if( address.equals(DEFAULT_ENPOINT) ) { String content = readInputStreamAsString(req.getInputStream()); JaxbCommandsRequest cmdsReq = (JaxbCommandsRequest) jaxbSerializationProvider.deserialize(content); String [] headerNames = { TEST_HEADER_NAME, ANOTHER_TEST_HEADER_NAME, NOT_SENT_HEADER_NAME }; List<String> headerValues = new ArrayList<String>(); for( String headerName : headerNames ) { String headerVal = req.getValue(headerName); if( headerVal != null ) { headerValues.add(headerVal); } } String output = handleJaxbCommandsRequest(cmdsReq, headerValues); resp.setCode(HttpURLConnection.HTTP_OK); out.print(output); } else { resp.setCode(HttpURLConnection.HTTP_BAD_REQUEST); } out.close(); } catch( Exception e ) { e.printStackTrace(); } }
Path reqPath = request.getPath(); String[] parts = request.getTarget().split("\\?"); Map<String, String> qry = null; if (parts.length > 1) { if (request.getTarget().startsWith(SESSION)) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; sessions.put(sessionid, request.getClientAddress().getHostString()); Element result = doc.getDocumentElement(); result.setAttribute("status", "success"); byte[] rb = com.google.common.io.BaseEncoding.base64Url().decode(request.getParameter("data")); byte[] rslt = new BatchGetBlocksCmd().getResult(rb); long time = System.currentTimeMillis(); SDFSLogger.getLog().error("invalid path " + reqPath.getPath()); body.close(); } else if (request.getTarget().startsWith(IO_PATH)) { String pth = request.getTarget().substring(IO_PATH.length()).split("\\?")[0]; } else if (Main.matcher != null && request.getTarget().startsWith(Main.matcher.getWPath())) { Main.matcher.getResult(request.getTarget(), 0, response); } else if (request.getTarget().startsWith(METADATA_PATH)) { long time = System.currentTimeMillis(); response.setDate("Date", time); response.setDate("Last-Modified", time);
@Override public String getRequestHeader(Header header) { return baseRequest.getValue(header.code); }
/** * This method is used to acquire the query part from the HTTP * request URI target and a form post if it exists. Both the * query and the form post are merge together in a single query. * * @return the query associated with the HTTP target URI */ public Query getQuery() { return request.getQuery(); }
/** * This can be used to get the URI specified for this HTTP request. * This corresponds to the either the full HTTP URI or the path * part of the URI depending on how the client sends the request. * * @return the URI address that this HTTP request is targeting */ public String getTarget() { return request.getTarget(); }
/** * This is used to acquire the path as extracted from the HTTP * request URI. The <code>Path</code> object that is provided by * this method is immutable, it represents the normalized path * only part from the request uniform resource identifier. * * @return this returns the normalized path for the request */ public Path getPath() { return request.getPath(); }
/** * This is used to acquire the remote client address. This can * be used to acquire both the port and the I.P address for the * client. It allows the connected clients to be logged and if * require it can be used to perform course grained security. * * @return this returns the client address for this request */ public InetSocketAddress getClientAddress() { return request.getClientAddress(); }
@Override public String toString() { return request.getMethod() + " " + request.getAddress().toString(); }
/** * This method is used to get a <code>List</code> of the names * for the headers. This will provide the original names for the * HTTP headers for the message. Modifications to the provided * list will not affect the header, the list is a simple copy. * * @return this returns a list of the names within the header */ public List<String> getNames() { return request.getNames(); }