List of usage examples for org.apache.http.message BasicHttpRequest getAllHeaders
public Header[] getAllHeaders()
From source file:org.jnode.jersey.JNContainer.java
@Override public void incomingRequest(BasicHttpRequest req, NHttpResponse res) { try {//from ww w.ja v a2s .c o m log.trace("Incoming request"); final URI baseUri = getBaseUri(req); final URI requestUri = getRequestUri(req, baseUri); ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, req.getRequestLine().getMethod(), null, new MapPropertiesDelegate()); requestContext.setEntityStream(new ByteArrayInputStream("".getBytes())); Arrays.stream(req.getAllHeaders()).forEach((Header he) -> { requestContext.headers(he.getName(), he.getValue()); }); requestContext.setWriter(new Writer(res)); requestContext.setRequestScopedInitializer(new RequestScopedInitializer() { @Override public void initialize(ServiceLocator locator) { // locator.<Ref<BasicHttpRequest>>getService(RequestTYPE).set(req); // locator.<Ref<NHttpResponse>>getService(ResponseTYPE).set(res); } }); requestContext.setSecurityContext(new BaseSecurityContext()); handler.handle(requestContext); } catch (Throwable t) { log.error("Unhandle exception ", t); res.end(); } }
From source file:com.k42b3.aletheia.protocol.http.HttpProtocol.java
public void run() { try {/*from w w w . j av a 2s. c o m*/ // get socket Socket socket = this.getSocket(); conn.bind(socket, params); // build request BasicHttpRequest request; if (!this.getRequest().getBody().isEmpty()) { request = new BasicHttpEntityEnclosingRequest(this.getRequest().getMethod(), this.getRequest().getPath()); } else { request = new BasicHttpRequest(this.getRequest().getMethod(), this.getRequest().getPath()); } // add headers String boundary = null; ArrayList<String> ignoreHeader = new ArrayList<String>(); ignoreHeader.add("Content-Length"); ignoreHeader.add("Expect"); LinkedList<Header> headers = this.getRequest().getHeaders(); for (int i = 0; i < headers.size(); i++) { if (!ignoreHeader.contains(headers.get(i).getName())) { // if the content-type header gets set the conent-length // header is automatically added request.addHeader(headers.get(i)); } if (headers.get(i).getName().equals("Content-Type") && headers.get(i).getValue().startsWith("multipart/form-data")) { String header = headers.get(i).getValue().substring(headers.get(i).getValue().indexOf(";") + 1) .trim(); if (!header.isEmpty()) { String parts[] = header.split("="); if (parts.length >= 2) { boundary = parts[1]; } } } } // set body if (request instanceof BasicHttpEntityEnclosingRequest && boundary != null) { boundary = "--" + boundary; StringBuilder body = new StringBuilder(); String req = this.getRequest().getBody(); int i = 0; String partHeader; String partBody; while ((i = req.indexOf(boundary, i)) != -1) { int hPos = req.indexOf("\n\n", i + 1); if (hPos != -1) { partHeader = req.substring(i + boundary.length() + 1, hPos).trim(); } else { partHeader = null; } int bpos = req.indexOf(boundary, i + 1); if (bpos != -1) { partBody = req.substring(hPos == -1 ? i : hPos + 2, bpos); } else { partBody = req.substring(hPos == -1 ? i : hPos + 2); } if (partBody.equals(boundary + "--")) { body.append(boundary + "--" + "\r\n"); break; } else if (!partBody.isEmpty()) { body.append(boundary + "\r\n"); if (partHeader != null && !partHeader.isEmpty()) { body.append(partHeader.replaceAll("\n", "\r\n")); body.append("\r\n"); body.append("\r\n"); } body.append(partBody); } i++; } this.getRequest().setBody(body.toString().replaceAll("\r\n", "\n")); HttpEntity entity = new StringEntity(this.getRequest().getBody()); ((BasicHttpEntityEnclosingRequest) request).setEntity(entity); } else if (request instanceof BasicHttpEntityEnclosingRequest) { HttpEntity entity = new StringEntity(this.getRequest().getBody()); ((BasicHttpEntityEnclosingRequest) request).setEntity(entity); } logger.info("> " + request.getRequestLine().getUri()); // request request.setParams(params); httpexecutor.preProcess(request, httpproc, context); HttpResponse response = httpexecutor.execute(request, conn, context); response.setParams(params); httpexecutor.postProcess(response, httpproc, context); logger.info("< " + response.getStatusLine()); // update request headers LinkedList<Header> header = new LinkedList<Header>(); Header[] allHeaders = request.getAllHeaders(); for (int i = 0; i < allHeaders.length; i++) { header.add(allHeaders[i]); } this.getRequest().setHeaders(header); // create response this.response = new Response(response); // call callback callback.onResponse(this.request, this.response); } catch (Exception e) { Aletheia.handleException(e); } finally { try { conn.close(); } catch (Exception e) { Aletheia.handleException(e); } } }