use of oasis.names.tc.dss._1_0.core.schema.ResponseBaseType in project open-ecard by ecsec.
the class WSHelper method checkResult.
public static <T extends ResponseBaseType> T checkResult(@Nonnull T response) throws WSException {
Result r = response.getResult();
if (r.getResultMajor().equals(ECardConstants.Major.ERROR)) {
if (response instanceof TransmitResponse) {
TransmitResponse tr = (TransmitResponse) response;
List<byte[]> rApdus = tr.getOutputAPDU();
if (rApdus.size() < 1) {
throw new WSException(r);
} else {
byte[] apdu = CardResponseAPDU.getTrailer(rApdus.get(rApdus.size() - 1));
String msg = CardCommandStatus.getMessage(apdu);
throw new WSException(msg);
}
} else {
throw new WSException(r);
}
}
return response;
}
use of oasis.names.tc.dss._1_0.core.schema.ResponseBaseType 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);
}
}
}
Aggregations