use of org.openecard.apache.http.HttpEntity in project open-ecard by ecsec.
the class StreamHttpClientConnection method receiveResponseEntity.
@Override
public void receiveResponseEntity(HttpResponse response) throws HttpException, IOException {
assertOpen();
final HttpEntity entity = prepareInput(response);
response.setEntity(entity);
}
use of org.openecard.apache.http.HttpEntity in project open-ecard by ecsec.
the class HttpGetTask method getRequest.
private void getRequest() throws IOException, ConnectionError, URISyntaxException, HttpException {
TlsConnectionHandler tlsHandler = new TlsConnectionHandler(dispatcher, tokenRequest, connectionHandle);
tlsHandler.setUpClient();
// connect the tls endpoint and make a get request
TlsClientProtocol handler = tlsHandler.createTlsConnection();
// set up connection to endpoint
InputStream in = handler.getInputStream();
OutputStream out = handler.getOutputStream();
StreamHttpClientConnection conn = new StreamHttpClientConnection(in, out);
// prepare HTTP connection
HttpContext ctx = new BasicHttpContext();
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
DefaultConnectionReuseStrategy reuse = new DefaultConnectionReuseStrategy();
// prepare request
String resource = tlsHandler.getResource();
BasicHttpEntityEnclosingRequest req = new BasicHttpEntityEnclosingRequest("GET", resource);
HttpRequestHelper.setDefaultHeader(req, tlsHandler.getServerAddress());
req.setHeader("Accept", "text/html, */*;q=0.8");
req.setHeader("Accept-Charset", "utf-8, *;q=0.8");
HttpUtils.dumpHttpRequest(LOG, req);
// send request and receive response
HttpResponse response = httpexecutor.execute(req, conn, ctx);
int statusCode = response.getStatusLine().getStatusCode();
conn.receiveResponseEntity(response);
HttpEntity entity = response.getEntity();
byte[] entityData = FileUtils.toByteArray(entity.getContent());
HttpUtils.dumpHttpResponse(LOG, response, entityData);
conn.close();
if (statusCode < 200 || statusCode > 299) {
throw new ConnectionError(WRONG_SERVER_RESULT, statusCode);
}
}
use of org.openecard.apache.http.HttpEntity in project open-ecard by ecsec.
the class ResourceContext method getStreamInt.
private static ResourceContext getStreamInt(URL url, CertificateValidator v, List<Pair<URL, TlsServerCertificate>> serverCerts, int maxRedirects) throws IOException, ResourceException, ValidationError, InvalidAddressException {
try {
DynamicContext dynCtx = DynamicContext.getInstance(TR03112Keys.INSTANCE_KEY);
CookieManager cManager = (CookieManager) dynCtx.get(TR03112Keys.COOKIE_MANAGER);
LOG.info("Trying to load resource from: {}", url);
if (maxRedirects == 0) {
throw new ResourceException(MAX_REDIRECTS);
}
maxRedirects--;
String protocol = url.getProtocol();
String hostname = url.getHost();
int port = url.getPort();
if (port == -1) {
port = url.getDefaultPort();
}
String resource = url.getFile();
resource = resource.isEmpty() ? "/" : resource;
if (!"https".equals(protocol)) {
throw new InvalidAddressException(INVALID_ADDRESS);
}
// open a TLS connection, retrieve the server certificate and save it
TlsClientProtocol h;
DynamicAuthentication tlsAuth = new DynamicAuthentication(hostname);
// add PKIX validator if not doin nPA auth
if (isPKIXVerify()) {
tlsAuth.addCertificateVerifier(new JavaSecVerifier());
}
// FIXME: validate certificate chain as soon as a usable solution exists for the trust problem
// tlsAuth.setCertificateVerifier(new JavaSecVerifier());
TlsCrypto crypto = new BcTlsCrypto(ReusableSecureRandom.getInstance());
ClientCertTlsClient tlsClient = new ClientCertDefaultTlsClient(crypto, hostname, true);
tlsClient.setAuthentication(tlsAuth);
// connect tls client
tlsClient.setClientVersion(ProtocolVersion.TLSv12);
Socket socket = ProxySettings.getDefault().getSocket(protocol, hostname, port);
h = new TlsClientProtocol(socket.getInputStream(), socket.getOutputStream());
LOG.debug("Performing TLS handshake.");
h.connect(tlsClient);
LOG.debug("TLS handshake performed.");
serverCerts.add(new Pair<>(url, tlsAuth.getServerCertificate()));
// check result
CertificateValidator.VerifierResult verifyResult = v.validate(url, tlsAuth.getServerCertificate());
if (verifyResult == CertificateValidator.VerifierResult.FINISH) {
List<Pair<URL, TlsServerCertificate>> pairs = Collections.unmodifiableList(serverCerts);
return new ResourceContext(tlsClient, h, pairs);
}
StreamHttpClientConnection conn = new StreamHttpClientConnection(h.getInputStream(), h.getOutputStream());
HttpContext ctx = new BasicHttpContext();
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
BasicHttpEntityEnclosingRequest req = new BasicHttpEntityEnclosingRequest("GET", resource);
HttpRequestHelper.setDefaultHeader(req, url);
req.setHeader("Accept", "text/xml, */*;q=0.8");
req.setHeader("Accept-Charset", "utf-8, *;q=0.8");
setCookieHeader(req, cManager, url);
HttpUtils.dumpHttpRequest(LOG, req);
LOG.debug("Sending HTTP request.");
HttpResponse response = httpexecutor.execute(req, conn, ctx);
storeCookies(response, cManager, url);
LOG.debug("HTTP response received.");
StatusLine status = response.getStatusLine();
int statusCode = status.getStatusCode();
String reason = status.getReasonPhrase();
HttpUtils.dumpHttpResponse(LOG, response, null);
HttpEntity entity = null;
boolean finished = false;
if (TR03112Utils.isRedirectStatusCode(statusCode)) {
Header[] headers = response.getHeaders("Location");
if (headers.length > 0) {
String uri = headers[0].getValue();
url = new URL(uri);
} else {
// FIXME: refactor exception handling
throw new ResourceException(MISSING_LOCATION_HEADER);
}
} else if (statusCode >= 400) {
// according to the HTTP RFC, codes greater than 400 signal errors
LOG.debug("Received a result code {} '{}' from server.", statusCode, reason);
throw new InvalidResultStatus(LANG.translationForKey(INVALID_RESULT_STATUS, statusCode, reason));
} else {
if (verifyResult == CertificateValidator.VerifierResult.CONTINUE) {
throw new InvalidAddressException(INVALID_REFRESH_ADDRESS_NOSOP);
} else {
conn.receiveResponseEntity(response);
entity = response.getEntity();
finished = true;
}
}
// follow next redirect or finish?
if (finished) {
assert (entity != null);
ResourceContext result = new ResourceContext(tlsClient, h, serverCerts);
LimitedInputStream is = new LimitedInputStream(entity.getContent());
result.setStream(is);
return result;
} else {
h.close();
return getStreamInt(url, v, serverCerts, maxRedirects);
}
} catch (URISyntaxException ex) {
throw new IOException(LANG.translationForKey(FAILED_PROXY), ex);
} catch (HttpException ex) {
// don't translate this, it is handled in the ActivationAction
throw new IOException("Invalid HTTP message received.", ex);
}
}
use of org.openecard.apache.http.HttpEntity 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.HttpEntity 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);
}
}
Aggregations