Search in sources :

Example 1 with DefaultConnectionReuseStrategy

use of org.openecard.apache.http.impl.DefaultConnectionReuseStrategy 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);
    }
}
Also used : HttpRequestExecutor(org.openecard.apache.http.protocol.HttpRequestExecutor) HttpEntity(org.openecard.apache.http.HttpEntity) InputStream(java.io.InputStream) BasicHttpContext(org.openecard.apache.http.protocol.BasicHttpContext) OutputStream(java.io.OutputStream) TlsClientProtocol(org.openecard.bouncycastle.tls.TlsClientProtocol) BasicHttpContext(org.openecard.apache.http.protocol.BasicHttpContext) HttpContext(org.openecard.apache.http.protocol.HttpContext) DefaultConnectionReuseStrategy(org.openecard.apache.http.impl.DefaultConnectionReuseStrategy) HttpResponse(org.openecard.apache.http.HttpResponse) StreamHttpClientConnection(org.openecard.transport.httpcore.StreamHttpClientConnection) BasicHttpEntityEnclosingRequest(org.openecard.apache.http.message.BasicHttpEntityEnclosingRequest)

Example 2 with DefaultConnectionReuseStrategy

use of org.openecard.apache.http.impl.DefaultConnectionReuseStrategy 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);
    }
}
Also used : HttpRequest(org.openecard.apache.http.HttpRequest) BasicHttpRequest(org.openecard.apache.http.message.BasicHttpRequest) HttpRequestExecutor(org.openecard.apache.http.protocol.HttpRequestExecutor) HttpEntity(org.openecard.apache.http.HttpEntity) Header(org.openecard.apache.http.Header) BasicHttpContext(org.openecard.apache.http.protocol.BasicHttpContext) BasicHttpContext(org.openecard.apache.http.protocol.BasicHttpContext) HttpContext(org.openecard.apache.http.protocol.HttpContext) DefaultConnectionReuseStrategy(org.openecard.apache.http.impl.DefaultConnectionReuseStrategy) HttpResponse(org.openecard.apache.http.HttpResponse) BasicHttpRequest(org.openecard.apache.http.message.BasicHttpRequest)

Example 3 with DefaultConnectionReuseStrategy

use of org.openecard.apache.http.impl.DefaultConnectionReuseStrategy in project open-ecard by ecsec.

the class PAOS method sendStartPAOS.

/**
 * Sends start PAOS and answers all successor messages to the server associated with this instance.
 * Messages are exchanged until the server replies with a {@code StartPAOSResponse} message.
 *
 * @param message The StartPAOS message which is sent in the first message.
 * @return The {@code StartPAOSResponse} message from the server.
 * @throws DispatcherException In case there errors with the message conversion or the dispatcher.
 * @throws PAOSException In case there were errors in the transport layer.
 * @throws PAOSConnectionException
 */
public StartPAOSResponse sendStartPAOS(StartPAOS message) throws DispatcherException, PAOSException, PAOSConnectionException {
    Object msg = message;
    StreamHttpClientConnection conn = null;
    HttpContext ctx = new BasicHttpContext();
    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
    DefaultConnectionReuseStrategy reuse = new DefaultConnectionReuseStrategy();
    boolean connectionDropped = false;
    ResponseBaseType lastResponse = null;
    try {
        // loop and send makes a computer happy
        while (true) {
            // set up connection to PAOS endpoint
            // if this one fails we may not continue
            conn = openHttpStream();
            boolean isReusable;
            // send as long as connection is valid
            try {
                do {
                    // save the last message we sent to the eID-Server.
                    if (msg instanceof ResponseBaseType) {
                        lastResponse = (ResponseBaseType) msg;
                    }
                    // prepare request
                    String resource = tlsHandler.getResource();
                    BasicHttpEntityEnclosingRequest req = new BasicHttpEntityEnclosingRequest("POST", resource);
                    HttpRequestHelper.setDefaultHeader(req, tlsHandler.getServerAddress());
                    req.setHeader(HEADER_KEY_PAOS, headerValuePaos);
                    req.setHeader("Accept", "text/xml, application/xml, application/vnd.paos+xml");
                    ContentType reqContentType = ContentType.create("application/vnd.paos+xml", "UTF-8");
                    HttpUtils.dumpHttpRequest(LOG, "before adding content", req);
                    String reqMsgStr = createPAOSResponse(msg);
                    StringEntity reqMsg = new StringEntity(reqMsgStr, reqContentType);
                    req.setEntity(reqMsg);
                    req.setHeader(reqMsg.getContentType());
                    req.setHeader("Content-Length", Long.toString(reqMsg.getContentLength()));
                    // send request and receive response
                    LOG.debug("Sending HTTP request.");
                    HttpResponse response = httpexecutor.execute(req, conn, ctx);
                    LOG.debug("HTTP response received.");
                    int statusCode = response.getStatusLine().getStatusCode();
                    try {
                        checkHTTPStatusCode(statusCode);
                    } catch (PAOSConnectionException ex) {
                        // response with error. So check the status of our last response to the eID-Server
                        if (lastResponse != null) {
                            WSHelper.checkResult(lastResponse);
                        }
                        throw ex;
                    }
                    conn.receiveResponseEntity(response);
                    HttpEntity entity = response.getEntity();
                    byte[] entityData = FileUtils.toByteArray(entity.getContent());
                    HttpUtils.dumpHttpResponse(LOG, response, entityData);
                    // consume entity
                    Object requestObj = processPAOSRequest(new ByteArrayInputStream(entityData));
                    // break when message is startpaosresponse
                    if (requestObj instanceof StartPAOSResponse) {
                        StartPAOSResponse startPAOSResponse = (StartPAOSResponse) requestObj;
                        // an ok.
                        if (lastResponse != null) {
                            WSHelper.checkResult(lastResponse);
                        }
                        WSHelper.checkResult(startPAOSResponse);
                        return startPAOSResponse;
                    }
                    // send via dispatcher
                    msg = dispatcher.deliver(requestObj);
                    // check if connection can be used one more time
                    isReusable = reuse.keepAlive(response, ctx);
                    connectionDropped = false;
                } while (isReusable);
            } catch (IOException ex) {
                if (!connectionDropped) {
                    connectionDropped = true;
                    LOG.warn("PAOS server closed the connection. Trying to connect again.");
                } else {
                    String errMsg = "Error in the link to the PAOS server.";
                    LOG.error(errMsg);
                    throw new PAOSException(DELIVERY_FAILED, ex);
                }
            }
        }
    } catch (HttpException ex) {
        throw new PAOSException(DELIVERY_FAILED, ex);
    } catch (SOAPException ex) {
        throw new PAOSException(SOAP_MESSAGE_FAILURE, ex);
    } catch (MarshallingTypeException ex) {
        throw new PAOSDispatcherException(MARSHALLING_ERROR, ex);
    } catch (InvocationTargetException ex) {
        throw new PAOSDispatcherException(DISPATCHER_ERROR, ex);
    } catch (TransformerException ex) {
        throw new DispatcherException(ex);
    } catch (WSException ex) {
        throw new PAOSException(ex);
    } finally {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (IOException ex) {
        // throw new PAOSException(ex);
        }
    }
}
Also used : MarshallingTypeException(org.openecard.ws.marshal.MarshallingTypeException) HttpRequestExecutor(org.openecard.apache.http.protocol.HttpRequestExecutor) ContentType(org.openecard.apache.http.entity.ContentType) HttpEntity(org.openecard.apache.http.HttpEntity) BasicHttpContext(org.openecard.apache.http.protocol.BasicHttpContext) DefaultConnectionReuseStrategy(org.openecard.apache.http.impl.DefaultConnectionReuseStrategy) StartPAOSResponse(iso.std.iso_iec._24727.tech.schema.StartPAOSResponse) StreamHttpClientConnection(org.openecard.transport.httpcore.StreamHttpClientConnection) StringEntity(org.openecard.apache.http.entity.StringEntity) SOAPException(org.openecard.ws.soap.SOAPException) WSException(org.openecard.common.WSHelper.WSException) HttpException(org.openecard.apache.http.HttpException) TransformerException(javax.xml.transform.TransformerException) BasicHttpEntityEnclosingRequest(org.openecard.apache.http.message.BasicHttpEntityEnclosingRequest) BasicHttpContext(org.openecard.apache.http.protocol.BasicHttpContext) HttpContext(org.openecard.apache.http.protocol.HttpContext) HttpResponse(org.openecard.apache.http.HttpResponse) DispatcherException(org.openecard.common.interfaces.DispatcherException) ResponseBaseType(oasis.names.tc.dss._1_0.core.schema.ResponseBaseType) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

HttpEntity (org.openecard.apache.http.HttpEntity)3 HttpResponse (org.openecard.apache.http.HttpResponse)3 DefaultConnectionReuseStrategy (org.openecard.apache.http.impl.DefaultConnectionReuseStrategy)3 BasicHttpContext (org.openecard.apache.http.protocol.BasicHttpContext)3 HttpContext (org.openecard.apache.http.protocol.HttpContext)3 HttpRequestExecutor (org.openecard.apache.http.protocol.HttpRequestExecutor)3 BasicHttpEntityEnclosingRequest (org.openecard.apache.http.message.BasicHttpEntityEnclosingRequest)2 StreamHttpClientConnection (org.openecard.transport.httpcore.StreamHttpClientConnection)2 StartPAOSResponse (iso.std.iso_iec._24727.tech.schema.StartPAOSResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 TransformerException (javax.xml.transform.TransformerException)1 ResponseBaseType (oasis.names.tc.dss._1_0.core.schema.ResponseBaseType)1 Header (org.openecard.apache.http.Header)1 HttpException (org.openecard.apache.http.HttpException)1 HttpRequest (org.openecard.apache.http.HttpRequest)1 ContentType (org.openecard.apache.http.entity.ContentType)1