use of org.openecard.apache.http.Header in project open-ecard by ecsec.
the class Http11Response method copyHttpResponse.
/**
* Copy the content of a HttpResponse to another instance.
*
* @param in HttpResponse
* @param out HttpResponse
*/
public static void copyHttpResponse(HttpResponse in, HttpResponse out) {
// remove and copy headers
HeaderIterator headIt = out.headerIterator();
while (headIt.hasNext()) {
headIt.nextHeader();
headIt.remove();
}
headIt = in.headerIterator();
while (headIt.hasNext()) {
Header next = headIt.nextHeader();
out.addHeader(next);
}
// set entity stuff
if (in.getEntity() != null) {
HttpEntity entity = in.getEntity();
out.setEntity(entity);
if (entity.getContentType() != null) {
out.setHeader(entity.getContentType());
}
if (entity.getContentEncoding() != null) {
out.setHeader(entity.getContentEncoding());
}
if (entity.getContentLength() > 0) {
out.setHeader(HeaderTypes.CONTENT_LENGTH.fieldName(), Long.toString(entity.getContentLength()));
}
// TODO: use chunked, repeatable and streaming attribute from entity
}
// copy rest
Locale l = in.getLocale();
if (l != null) {
out.setLocale(l);
}
out.setStatusLine(in.getStatusLine());
}
use of org.openecard.apache.http.Header in project open-ecard by ecsec.
the class StreamHttpClientConnectionTest method consumeEntity.
private void consumeEntity(StreamHttpClientConnection conn, String hostName, int numIt) throws IOException, HttpException {
HttpContext ctx = new BasicHttpContext();
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpResponse response = null;
DefaultConnectionReuseStrategy reuse = new DefaultConnectionReuseStrategy();
int i = 0;
while (i == 0 || (i < numIt && reuse.keepAlive(response, ctx))) {
i++;
// send request and receive response
HttpRequest request = new BasicHttpRequest("GET", "/");
HttpRequestHelper.setDefaultHeader(request, hostName);
response = httpexecutor.execute(request, conn, ctx);
conn.receiveResponseEntity(response);
HttpEntity entity = response.getEntity();
assertNotNull(entity);
// consume entity
byte[] content = FileUtils.toByteArray(entity.getContent());
// read header and check if content size is correct
Header lengthHeader = response.getFirstHeader("Content-Length");
long length = Long.parseLong(lengthHeader.getValue());
assertNotNull(lengthHeader);
assertEquals(entity.getContentLength(), length);
assertEquals(content.length, length);
// consume everything from the entity and close stream
EntityUtils.consume(entity);
}
}
use of org.openecard.apache.http.Header in project open-ecard by ecsec.
the class HttpUtils method dumpHttpRequest.
/**
* Dump the given HTTP request and log it with the given logger instance.
* An optional message can be given wich will be printed in the head of the log entry to define the context of the
* message. The request message is not modified by this method.
*
* @param logger Logger to dump HTTP request to.
* @param msg Message qualifying the context of the request.
* @param req Request to dump.
*/
public static void dumpHttpRequest(@Nonnull Logger logger, @Nullable String msg, @Nonnull HttpRequest req) {
if (logger.isDebugEnabled()) {
StringWriter w = new StringWriter();
PrintWriter pw = new PrintWriter(w);
pw.print("HTTP Request");
if (msg != null) {
pw.format(" (%s)", msg);
}
pw.println(":");
RequestLine rl = req.getRequestLine();
pw.format(" %s %s %s%n", rl.getMethod(), rl.getUri(), rl.getProtocolVersion().toString());
for (Header h : req.getAllHeaders()) {
pw.format(" %s: %s%n", h.getName(), h.getValue());
}
pw.flush();
logger.debug(w.toString());
}
}
use of org.openecard.apache.http.Header in project open-ecard by ecsec.
the class ErrorResponseInterceptor method process.
@Override
public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (errorCodes.contains(statusCode)) {
LOG.debug("HTTP response intercepted");
Header contentType = httpResponse.getFirstHeader(HeaderTypes.CONTENT_TYPE.fieldName());
if (contentType != null) {
// Intercept response with the content type "text/plain"
if (contentType.getValue().contains(MimeType.TEXT_PLAIN.getMimeType())) {
// Remove old headers
httpResponse.removeHeaders(HeaderTypes.CONTENT_TYPE.fieldName());
httpResponse.removeHeaders(HeaderTypes.CONTENT_LENGTH.fieldName());
// Read message body
String content = readEntity(httpResponse.getEntity());
// escape string to prevent script content to be injected into the template (XSS)
content = HTMLUtils.escapeHtml(content);
template.setProperty("%%%MESSAGE%%%", content);
}
} else {
template.setProperty("%%%MESSAGE%%%", lang.translationForKey("http." + statusCode));
}
template.setProperty("%%%TITLE%%%", "Error");
String reason = statusLine.getReasonPhrase();
template.setProperty("%%%HEADLINE%%%", reason);
// Add new content
httpResponse.setEntity(new StringEntity(template.toString(), "UTF-8"));
httpResponse.addHeader(HeaderTypes.CONTENT_TYPE.fieldName(), MimeType.TEXT_HTML.getMimeType() + "; charset=utf-8");
httpResponse.addHeader(HeaderTypes.CONTENT_LENGTH.fieldName(), String.valueOf(template.getBytes().length));
}
}
Aggregations