Search in sources :

Example 6 with MarshallingTypeException

use of org.openecard.ws.marshal.MarshallingTypeException in project open-ecard by ecsec.

the class JAXBMarshaller method marshal.

@Override
public synchronized Document marshal(Object o) throws MarshallingTypeException {
    try {
        Document d = w3Builder.newDocument();
        marshaller.getMarshaller().marshal(o, d);
        return d;
    } catch (JAXBException ex) {
        throw new MarshallingTypeException(ex);
    }
}
Also used : MarshallingTypeException(org.openecard.ws.marshal.MarshallingTypeException) JAXBException(javax.xml.bind.JAXBException) Document(org.w3c.dom.Document)

Example 7 with MarshallingTypeException

use of org.openecard.ws.marshal.MarshallingTypeException in project open-ecard by ecsec.

the class CryptoMarkerBuilder method build.

public CryptoMarkerType build() {
    CryptoMarkerType marker = new CryptoMarkerType();
    marker.setProtocol(PROTOCOL);
    if (algInfo != null) {
        try {
            JAXBElement<AlgorithmInfoType> e;
            e = new JAXBElement<>(new QName(ISONS, "AlgorithmInfo"), AlgorithmInfoType.class, algInfo);
            Document d = m.marshal(e);
            marker.getAny().add(d.getDocumentElement());
        } catch (MarshallingTypeException ex) {
            LOG.error("Failed to marshal AlgorithmInfo element.", ex);
        }
    }
    if (keyInfo != null) {
        try {
            JAXBElement<CryptoKeyInfoType> e;
            e = new JAXBElement<>(new QName(ISONS, "KeyInfo"), CryptoKeyInfoType.class, keyInfo);
            Document d = m.marshal(e);
            marker.getAny().add(d.getDocumentElement());
        } catch (MarshallingTypeException ex) {
            LOG.error("Failed to marshal KeyInfo element.", ex);
        }
    }
    if (sigGenInfo != null) {
        try {
            JAXBElement<String> e;
            e = new JAXBElement(new QName(ISONS, "SignatureGenerationInfo"), String.class, sigGenInfo);
            Document d = m.marshal(e);
            marker.getAny().add(d.getDocumentElement());
        } catch (MarshallingTypeException ex) {
            LOG.error("Failed to marshal SignatureGenerationInfo element.", ex);
        }
    }
    if (legacySignGenInfo != null) {
        try {
            JAXBElement<LegacySignatureGenerationType> e;
            e = new JAXBElement(new QName(ISONS, "LegacySignatureGenerationInfo"), LegacySignatureGenerationType.class, legacySignGenInfo);
            Document d = m.marshal(e);
            marker.getAny().add(d.getDocumentElement());
        } catch (MarshallingTypeException ex) {
            LOG.error("Failed to marshal LegacySignatureGenerationInfo element.", ex);
        }
    }
    if (hashGenInfo != null) {
        try {
            JAXBElement<HashGenerationInfoType> e;
            e = new JAXBElement(new QName(ISONS, "HashGenerationInfo"), HashGenerationInfoType.class, hashGenInfo);
            Document d = m.marshal(e);
            marker.getAny().add(d.getDocumentElement());
        } catch (MarshallingTypeException ex) {
            LOG.error("Failed to marshal HashGenerationInfo element.", ex);
        }
    }
    for (CertificateRefType certRef : getCertRefs()) {
        try {
            JAXBElement<CertificateRefType> e;
            e = new JAXBElement(new QName(ISONS, "CertificateRef"), CertificateRefType.class, certRef);
            Document d = m.marshal(e);
            marker.getAny().add(d.getDocumentElement());
        } catch (MarshallingTypeException ex) {
            LOG.error("Failed to marshal CertificateRef element.", ex);
        }
    }
    if (legacyKeyname != null) {
        try {
            JAXBElement<String> e;
            e = new JAXBElement(new QName(ISONS, "LegacyKeyName"), String.class, legacyKeyname);
            Document d = m.marshal(e);
            marker.getAny().add(d.getDocumentElement());
        } catch (MarshallingTypeException ex) {
            LOG.error("Failed to marshal LegacyKeyName element.", ex);
        }
    }
    return marker;
}
Also used : MarshallingTypeException(org.openecard.ws.marshal.MarshallingTypeException) LegacySignatureGenerationType(iso.std.iso_iec._24727.tech.schema.LegacySignatureGenerationType) QName(javax.xml.namespace.QName) CryptoMarkerType(iso.std.iso_iec._24727.tech.schema.CryptoMarkerType) JAXBElement(javax.xml.bind.JAXBElement) Document(org.w3c.dom.Document) HashGenerationInfoType(iso.std.iso_iec._24727.tech.schema.HashGenerationInfoType) CertificateRefType(iso.std.iso_iec._24727.tech.schema.CertificateRefType) CryptoKeyInfoType(iso.std.iso_iec._24727.tech.schema.CryptoKeyInfoType) AlgorithmInfoType(iso.std.iso_iec._24727.tech.schema.AlgorithmInfoType)

Example 8 with MarshallingTypeException

use of org.openecard.ws.marshal.MarshallingTypeException 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)

Example 9 with MarshallingTypeException

use of org.openecard.ws.marshal.MarshallingTypeException in project open-ecard by ecsec.

the class PAOS method processPAOSRequest.

private Object processPAOSRequest(InputStream content) throws PAOSException {
    try {
        Document doc = m.str2doc(content);
        SOAPMessage msg = m.doc2soap(doc);
        // msg.getSOAPHeader().
        updateMessageID(msg);
        if (LOG.isDebugEnabled()) {
            try {
                LOG.debug("Message received:\n{}", m.doc2str(doc));
            } catch (TransformerException ex) {
                LOG.warn("Failed to log PAOS request message.", ex);
            }
        }
        return m.unmarshal(msg.getSOAPBody().getChildElements().get(0));
    } catch (MarshallingTypeException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new PAOSException(ex.getMessage(), ex);
    } catch (WSMarshallerException ex) {
        String msg = "Failed to read/process message from PAOS server.";
        LOG.error(msg, ex);
        throw new PAOSException(MARSHALLING_ERROR, ex);
    } catch (IOException | SAXException ex) {
        String msg = "Failed to read/process message from PAOS server.";
        LOG.error(msg, ex);
        throw new PAOSException(SOAP_MESSAGE_FAILURE, ex);
    }
}
Also used : MarshallingTypeException(org.openecard.ws.marshal.MarshallingTypeException) WSMarshallerException(org.openecard.ws.marshal.WSMarshallerException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SOAPMessage(org.openecard.ws.soap.SOAPMessage) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Aggregations

MarshallingTypeException (org.openecard.ws.marshal.MarshallingTypeException)9 Document (org.w3c.dom.Document)8 IOException (java.io.IOException)4 TransformerException (javax.xml.transform.TransformerException)4 JAXBElement (javax.xml.bind.JAXBElement)3 QName (javax.xml.namespace.QName)3 WSMarshallerException (org.openecard.ws.marshal.WSMarshallerException)3 SOAPException (org.openecard.ws.soap.SOAPException)3 SAXException (org.xml.sax.SAXException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 JAXBException (javax.xml.bind.JAXBException)2 DatatypeConfigurationException (javax.xml.datatype.DatatypeConfigurationException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 RuntimeCryptoException (org.openecard.bouncycastle.crypto.RuntimeCryptoException)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 InitializeFrameworkResponse (de.bund.bsi.ecard.api._1.InitializeFrameworkResponse)1 TCTokenType (generated.TCTokenType)1 AlgorithmInfoType (iso.std.iso_iec._24727.tech.schema.AlgorithmInfoType)1 BeginTransaction (iso.std.iso_iec._24727.tech.schema.BeginTransaction)1 BeginTransactionResponse (iso.std.iso_iec._24727.tech.schema.BeginTransactionResponse)1