List of usage examples for com.rabbitmq.client RpcClient stringCall
@SuppressWarnings("unused") public String stringCall(String message) throws IOException, ShutdownSignalException, TimeoutException
From source file:it.txt.ens.client.impl.BasicENSClient.java
License:Apache License
@Override public void connect() throws ENSClientException, ENSConnectionException { //create and configure the RabbitMQ Connection Factory factory = new ConnectionFactory(); factory.setUsername(authzServiceConnParams.getSubjectID()); factory.setPassword(authzServiceConnParams.getAccessToken()); factory.setPort(authzServiceConnParams.getBrokerPort()); factory.setHost(authzServiceConnParams.getBrokerHost()); factory.setVirtualHost(authzServiceConnParams.getVirtualHost()); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "authorisation.step1", new Object[] { capabilityDirectory.getAbsoluteFile(), subjectID, targetResource.getURI().toString(), operation.toString() }); }// w w w . j a v a 2s . c om //START CAPABILITY SEARCH //retrieve the best suitable capabilities CapabilityXQuerySaxURISearch capabilityFinder = new CapabilityXQuerySaxURISearch(capabilityDirectory); URIResourceIDSections uriSections = new URIResourceIDSections(); uriSections.setAuthority(targetResource.getHost()); uriSections.setScheme(ENSResource.URI_SCHEME); uriSections.setNamespace(targetResource.getNamespace()); uriSections.setPattern(targetResource.getPattern()); uriSections.setService(targetResource.getPath()); List<CapabilitySearchReturn> capabilities; try { capabilities = capabilityFinder.doSearchCapability(subjectID, uriSections.toURI().toString(), operation.toString()); } catch (UnsupportedEncodingException e) { ENSClientException ece = new ENSClientException( ENSClientExceptionCodes.TARGET_RESOURCE_URI_CREATION_ERROR, e); LOGGER.log(Level.SEVERE, ece.getMessage(), e); throw ece; } catch (URISyntaxException e) { ENSClientException ece = new ENSClientException( ENSClientExceptionCodes.TARGET_RESOURCE_URI_CREATION_ERROR, e); LOGGER.log(Level.SEVERE, ece.getMessage(), e); throw ece; } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "capabilitySearch.selectedCapabilities", capabilities.size()); } if (capabilities.isEmpty()) throw new ENSClientException(ENSClientExceptionCodes.NO_SUITABLE_CAPABILITY_FOUND); if (capabilities.size() > 1) Collections.sort(capabilities, new Comparator<CapabilitySearchReturn>() { public int compare(CapabilitySearchReturn o1, CapabilitySearchReturn o2) { XMLGregorianCalendar issueDate1 = o1.getCapabilityIssueDateToXmlGregorianCalendar(); XMLGregorianCalendar isssueDate2 = o2.getCapabilityIssueDateToXmlGregorianCalendar(); return issueDate1.compare(isssueDate2); } }); CapabilitySearchReturn selectedCapability = capabilities.get(0); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "authorisation.step1OK", new Object[] { selectedCapability.getCapabilityID(), selectedCapability .getCapabilityIssueDateToXmlGregorianCalendar().toGregorianCalendar().getTime(), selectedCapability.getCapabilityFile().getAbsolutePath() }); LOGGER.log(Level.FINE, "authorisation.step2"); } //STOP CAPABILITY SEARCH //create a JAXB request object FileInputStream capabilityStream = null; RequestType requestType = null; try { capabilityStream = new FileInputStream(selectedCapability.getCapabilityFile()); requestType = requestFactory.create(targetResource.getURI(), subjectID, operation.toString(), sessionExpiration, capabilityStream); } catch (FileNotFoundException e) { if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, MessageFormat.format(LOG_MESSAGES.getString("capabilitySearch.missingExistingFile"), selectedCapability.getCapabilityFile().getAbsolutePath()), e); throw new ENSClientException(ENSClientExceptionCodes.MISSING_SELECTED_CAPABILITY, e); } catch (ENSRequestFactoryException e) { ENSClientException clientExc = new ENSClientException(ENSClientExceptionCodes.REQUEST_CREATION, e); if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, clientExc.getMessage(), e); throw clientExc; } //here we are sure that the request type has been instantiated Document requestDOM = null; try { requestDOM = requestFactory.marshal(requestType); } catch (ENSRequestFactoryException e) { ENSClientException clientExc = new ENSClientException(ENSClientExceptionCodes.REQUEST_MARSHALLING_ERROR, e); if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, clientExc.getMessage(), e); throw clientExc; } //we are sure that the request DOM has been instantiated if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "authorisation.step2OK", XMLPrinter.printDocumentElement(requestDOM.getDocumentElement(), true)); LOGGER.log(Level.FINE, "authorisation.step3"); } X509DocumentSignerInfo signerInfo = new X509DocumentSignerInfo(); signerInfo.setKeystorePath(keystoreParams.getKeystorePath().getAbsolutePath()); signerInfo.setKeystorePwd(keystoreParams.getKeystorePassword()); signerInfo.setPrivateKeyPwd(certParams.getPrivateKeyPassword()); signerInfo.setSignerID(certParams.getX509CertificateSubject()); try { X509CertificateKeyValues keyValues = X509DocumentSigner.getCertificateKeyValues(signerInfo); X509DocumentSigner.signXMLElement(requestDOM.getDocumentElement(), keyValues, ENSAuthorisationRequestFactory.SIGN_DOCUMENT_AFTER_NODE); } catch (GeneralSecurityException e) { ENSClientException clientExc = new ENSClientException( ENSClientExceptionCodes.DIGITAL_SIGNATURE_CREATION_ERROR, e); if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, clientExc.getMessage(), e); throw clientExc; } if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, "authorisation.step3OK", XMLPrinter.printDocumentElement(requestDOM.getDocumentElement(), true)); //transformation of the digitally signed XML DOM into an array of byte ByteArrayOutputStream xmlos; try { xmlos = (ByteArrayOutputStream) XMLPrinter .convertDOMIntoByteStream(requestDOM, false, null, new ByteArrayOutputStream()) .getOutputStream(); } catch (TransformerException e) { ENSClientException clientExc = new ENSClientException(ENSClientExceptionCodes.SIGNATURE_TO_BYTES_ERROR, e); if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, clientExc.getMessage(), e); throw clientExc; } if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, "authorisation.step4"); Connection authorisationConnection = null; Channel authorisationChannel = null; String rawResponse = null; try { //initialise the connection to the ENS access request broker authorisationConnection = factory.newConnection(); authorisationChannel = authorisationConnection.createChannel(); //create an RPC Client //FIXME SHOULD WE INDICATE THE EXCHANGE?? RpcClient client = new RpcClient(authorisationChannel, "", authzServiceConnParams.getDestinationName(), TIMEOUT); rawResponse = client.stringCall(xmlos.toString(ENCODING)); // rawResponse = client.stringCall(xmlos.toString()); } catch (IOException e) { ENSConnectionException connExc = new ENSConnectionException( ENSConnectionExceptionCodes.ACCESS_REQUEST_BROKER_CONNECTION_ERROR, e); if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, connExc.getMessage(), e); throw connExc; } catch (ShutdownSignalException e) { ENSConnectionException connExc = new ENSConnectionException( ENSConnectionExceptionCodes.ACCESS_REQUEST_BROKER_CONNECTION_ERROR, e); if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, connExc.getMessage(), e); throw connExc; } catch (TimeoutException e) { ENSConnectionException connExc = new ENSConnectionException( ENSConnectionExceptionCodes.ACCESS_REQUEST_BROKER_CONNECTION_ERROR, e); if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, connExc.getMessage(), e); throw connExc; } finally { if (authorisationChannel != null) try { authorisationChannel.close(); } catch (IOException e) { } if (authorisationConnection != null) try { authorisationConnection.close(); } catch (IOException e) { } } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "authorisation.step4OK", rawResponse); } if (ENSStatus.FailureCodes.INTERNAL_ERROR.equalsIgnoreCase(rawResponse)) { throw new ENSClientException(ENSClientExceptionCodes.INTERNAL_SERVER_ERROR); } ResponseType responseObject = null; ByteArrayInputStream inputStream = null; try { inputStream = new ByteArrayInputStream(rawResponse.getBytes(ENCODING)); // inputStream = new ByteArrayInputStream(rawResponse.getBytes()); responseObject = responseFactory.parseInputStream(inputStream); Document responseDOM = responseFactory.marshal(responseObject); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "authorisation.step5"); } boolean isSignatureVerified = X509DocumentSigner.verifyXMLElementSign(responseDOM.getDocumentElement(), new X509CertificateSubjectInfo()); if (!isSignatureVerified) { throw new ENSClientException(ENSClientExceptionCodes.TAMPERED_RESPONSE); } } catch (UnsupportedEncodingException e) { ENSClientException ece = new ENSClientException(ENSClientExceptionCodes.UNSUPPORTED_ENCODING, e, ENCODING, debugID); if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, ece.getMessage(), e); throw ece; } catch (ENSResponseFactoryException e) { ENSClientException ece = new ENSClientException(ENSClientExceptionCodes.RESPONSE_MARSHALLING_ERROR, e); if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, ece.getMessage(), e); throw ece; } catch (GeneralSecurityException e) { ENSClientException ece = new ENSClientException( ENSClientExceptionCodes.DIGITAL_SIGNATURE_VERIFICATION_ERROR, e); if (LOGGER.isLoggable(Level.SEVERE)) LOGGER.log(Level.SEVERE, ece.getMessage(), e); throw ece; } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "authorisation.step5OK"); LOGGER.log(Level.FINE, "authorisation.step6"); } //analysis of the response if (responseObject.isResult()) { SuccessResponseType success = responseObject.getResultDetails().getSuccessResponse(); ENSBrokerConnectionParameters operativeBrokerConnParams = connParamsFactory.create( success.getSubject().getSubjectID(), success.getAccessToken(), success.getBrokerHost(), success.getBrokerPort().intValue(), success.getVirtualHost(), success.getQueueName(), success.isUseTLS(), success.getSessionExpiration().toGregorianCalendar().getTime(), success.getSessionToken()); if (LOGGER.isLoggable(Level.INFO)) { LOGGER.log(Level.INFO, "authorisation.step6OK", new String[] { debugID, operativeBrokerConnParams.toString() }); } factory = new ConnectionFactory(); factory.setHost(operativeBrokerConnParams.getBrokerHost()); factory.setPassword(operativeBrokerConnParams.getAccessToken()); factory.setPort(operativeBrokerConnParams.getBrokerPort()); factory.setUsername(operativeBrokerConnParams.getSubjectID()); factory.setVirtualHost(operativeBrokerConnParams.getVirtualHost()); useENSBrokerConnectionParameters(operativeBrokerConnParams); try { connection = factory.newConnection(); } catch (IOException e) { ENSClientException ce = new ENSClientException(ENSClientExceptionCodes.OPERATIVE_CONNECTION_ERROR, e, factory.getVirtualHost(), factory.getHost()); LOGGER.log(Level.SEVERE, ce.getMessage(), e); throw ce; } } else { FailureResponseType failure = responseObject.getResultDetails().getFailureResponse(); ENSClientException failureException = new ENSClientException(failure); if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.log(Level.SEVERE, "authorisation.step6FAILURE", new String[] { failure.getErrorCode(), failure.getErrorReason() }); } throw failureException; } }