use of com.helger.commons.http.HttpHeaderMap in project ph-web by phax.
the class UAProfileDatabaseTest method testUAProfSingleDiff.
@Test
public void testUAProfSingleDiff() {
final HttpHeaderMap aMap = new HttpHeaderMap();
aMap.addHeader("x-wap-profile", "\"1-diGacUwQ6eeOlaWB6cFEag==\"");
UAProfile aProfile = UAProfileDatabase.getUAProfileFromRequest(new MockProvider(aMap));
assertNull(aProfile);
aMap.addHeader("x-wap-profile-diff", "1;<?xml version=\"1.0\"?> <rdf:RDF\n" + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" \n" + "xmlns:prf=\"http://www.wapforum.org/profiles/UAPROF/ccppschema-20010430#\"> \n" + "<rdf:Description rdf:ID=\"MyDeviceProfile\">\n" + "<prf:component>\n" + "<rdf:Description rdf:ID=\"HardwarePlatform\">\n" + "<rdf:type\n" + "rdf:resource=\"http://www.wapforum.org/profiles/UAPROF/ccppschema-20010430#HardwarePlatform\"/> \n" + "<prf:SoundOutputCapable>No</prf:SoundOutputCapable> \n" + "</rdf:Description>\n" + "</prf:component>\n" + "</rdf:Description>\n" + "</rdf:RDF>");
aProfile = UAProfileDatabase.getUAProfileFromRequest(new MockProvider(aMap));
assertNotNull(aProfile);
assertTrue(aProfile.isSet());
assertNull(aProfile.getProfileURL());
assertEquals(1, aProfile.getDiffCount());
CommonsTestHelper.testDefaultImplementationWithEqualContentObject(aProfile, UAProfileDatabase.getUAProfileFromRequest(new MockProvider(aMap)));
}
use of com.helger.commons.http.HttpHeaderMap in project ph-web by phax.
the class UAProfileDatabaseTest method testUAProfMultiDiff.
@Test
public void testUAProfMultiDiff() {
final HttpHeaderMap aMap = new HttpHeaderMap();
aMap.addHeader("x-wap-profile", "\"1-diGacUwQ6eeOlaWB6cFEag==\", \"2-diGacUwQ6eeOlaWB6cFEag==\", \"3-diGacUwQ6eeOlaWB6cFEag==\"");
aMap.addHeader("x-wap-profile-diff", "1;<?xml version=\"1.0\"?><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:prf=\"http://www.wapforum.org/profiles/UAPROF/ccppschema-20010430#\">1</rdf:RDF>");
aMap.addHeader("x-wap-profile-diff", "2;<?xml version=\"1.0\"?><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:prf=\"http://www.wapforum.org/profiles/UAPROF/ccppschema-20010430#\">1</rdf:RDF>");
aMap.addHeader("x-wap-profile-diff", "3;<?xml version=\"1.0\"?><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:prf=\"http://www.wapforum.org/profiles/UAPROF/ccppschema-20010430#\">1</rdf:RDF>");
final UAProfile aProfile = UAProfileDatabase.getUAProfileFromRequest(new MockProvider(aMap));
assertNotNull(aProfile);
assertTrue(aProfile.isSet());
assertNull(aProfile.getProfileURL());
assertEquals(3, aProfile.getDiffCount());
CommonsTestHelper.testDefaultImplementationWithEqualContentObject(aProfile, UAProfileDatabase.getUAProfileFromRequest(new MockProvider(aMap)));
}
use of com.helger.commons.http.HttpHeaderMap in project phase4 by phax.
the class AS4RequestHandler method handleRequest.
/**
* This is the main handling routine when called from an abstract
* (non-Servlet) API
*
* @param aServletRequestIS
* The input stream with the request data. May not be
* <code>null</code>.
* @param aRequestHttpHeaders
* The HTTP headers of the request. May not be <code>null</code>.
* @param aHttpResponse
* The HTTP response to be filled. May not be <code>null</code>.
* @throws Phase4Exception
* in case the request is missing certain prerequisites. Since 0.9.11
* @throws IOException
* In case of IO errors
* @throws MessagingException
* MIME related errors
* @throws WSSecurityException
* In case of WSS4J errors
* @see #handleRequest(InputStream, HttpHeaderMap, IAS4ResponseAbstraction)
* for a more generic API
*/
public void handleRequest(@Nonnull @WillClose final InputStream aServletRequestIS, @Nonnull final HttpHeaderMap aRequestHttpHeaders, @Nonnull final IAS4ResponseAbstraction aHttpResponse) throws Phase4Exception, IOException, MessagingException, WSSecurityException {
final IAS4ParsedMessageCallback aCallback = (aHttpHeaders, aSoapDocument, eSoapVersion, aIncomingAttachments) -> {
// SOAP document and SOAP version are determined
// Collect all runtime errors
final ICommonsList<Ebms3Error> aErrorMessages = new CommonsArrayList<>();
final IAS4ResponseFactory aResponder = _handleSoapMessage(aHttpHeaders, aSoapDocument, eSoapVersion, aIncomingAttachments, aErrorMessages);
if (aResponder != null) {
// Response present -> send back
final IAS4OutgoingDumper aRealOutgoingDumper = m_aOutgoingDumper != null ? m_aOutgoingDumper : AS4DumpManager.getOutgoingDumper();
aResponder.applyToResponse(aHttpResponse, aRealOutgoingDumper);
} else {
// Success, HTTP No Content
aHttpResponse.setStatus(CHttp.HTTP_NO_CONTENT);
}
AS4HttpDebug.debug(() -> "RECEIVE-END with " + (aResponder != null ? "EBMS message" : "no content"));
};
AS4IncomingHandler.parseAS4Message(m_aIAF, m_aResHelper, m_aMessageMetadata, aServletRequestIS, aRequestHttpHeaders, aCallback, m_aIncomingDumper);
}
use of com.helger.commons.http.HttpHeaderMap in project phase4 by phax.
the class AS4RequestHandler method handleRequest.
/**
* This is the main handling routine when called from the Servlet API
*
* @param aRequestScope
* HTTP request. Never <code>null</code>.
* @param aHttpResponse
* HTTP response. Never <code>null</code>.
* @throws Phase4Exception
* in case the request is missing certain prerequisites. Since 0.9.11
* @throws IOException
* In case of IO errors
* @throws MessagingException
* MIME related errors
* @throws WSSecurityException
* In case of WSS4J errors
* @see #handleRequest(InputStream, HttpHeaderMap, IAS4ResponseAbstraction)
* for a more generic API
*/
public void handleRequest(@Nonnull final IRequestWebScopeWithoutResponse aRequestScope, @Nonnull final AS4UnifiedResponse aHttpResponse) throws Phase4Exception, IOException, MessagingException, WSSecurityException {
AS4HttpDebug.debug(() -> "RECEIVE-START at " + aRequestScope.getFullContextAndServletPath());
final ServletInputStream aServletRequestIS = aRequestScope.getRequest().getInputStream();
final HttpHeaderMap aHttpHeaders = aRequestScope.headers().getClone();
final IAS4ResponseAbstraction aResponse = IAS4ResponseAbstraction.wrap(aHttpResponse);
handleRequest(aServletRequestIS, aHttpHeaders, aResponse);
}
use of com.helger.commons.http.HttpHeaderMap in project phase4 by phax.
the class AS4RequestHandler method _handleSoapMessage.
@Nullable
private IAS4ResponseFactory _handleSoapMessage(@Nonnull final HttpHeaderMap aHttpHeaders, @Nonnull final Document aSoapDocument, @Nonnull final ESoapVersion eSoapVersion, @Nonnull final ICommonsList<WSS4JAttachment> aIncomingAttachments, @Nonnull final ICommonsList<Ebms3Error> aErrorMessagesTarget) throws WSSecurityException, MessagingException, Phase4Exception {
final SOAPHeaderElementProcessorRegistry aRegistry = SOAPHeaderElementProcessorRegistry.createDefault(m_aPModeResolver, m_aCryptoFactory, (IPMode) null);
final IAS4MessageState aState = AS4IncomingHandler.processEbmsMessage(m_aResHelper, m_aLocale, aRegistry, aHttpHeaders, aSoapDocument, eSoapVersion, aIncomingAttachments, m_aIncomingProfileSelector, aErrorMessagesTarget);
final IPMode aPMode = aState.getPMode();
final PModeLeg aEffectiveLeg = aState.getEffectivePModeLeg();
final String sMessageID = aState.getMessageID();
final ICommonsList<WSS4JAttachment> aDecryptedAttachments = aState.hasDecryptedAttachments() ? aState.getDecryptedAttachments() : aState.getOriginalAttachments();
final Node aPayloadNode = aState.getSoapBodyPayloadNode();
final Ebms3UserMessage aEbmsUserMessage = aState.getEbmsUserMessage();
final Ebms3SignalMessage aEbmsSignalMessage = aState.getEbmsSignalMessage();
if (aState.isSoapHeaderElementProcessingSuccessful()) {
final String sProfileID = aState.getProfileID();
if (LOGGER.isDebugEnabled())
LOGGER.debug("No checking for duplicate message with message ID '" + sMessageID + "' and profile ID '" + sProfileID + "'");
final boolean bIsDuplicate = MetaAS4Manager.getIncomingDuplicateMgr().registerAndCheck(sMessageID, sProfileID, aPMode == null ? null : aPMode.getID()).isBreak();
if (bIsDuplicate) {
LOGGER.error("Not invoking SPIs, because message with Message ID '" + sMessageID + "' was already handled!");
aErrorMessagesTarget.add(EEbmsError.EBMS_OTHER.getAsEbms3Error(m_aLocale, sMessageID, "Another message with the same Message ID '" + sMessageID + "' was already received!"));
} else {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Message is not a duplicate");
}
}
final SPIInvocationResult aSPIResult = new SPIInvocationResult();
// Storing for two-way response messages
final ICommonsList<WSS4JAttachment> aResponseAttachments = new CommonsArrayList<>();
// Invoke SPIs if
// * No errors so far (sign, encrypt, ...)
// * Valid PMode
// * Exactly one UserMessage or SignalMessage
// * No ping/test message
// * No Duplicate message ID
final boolean bCanInvokeSPIs = aErrorMessagesTarget.isEmpty() && !aState.isPingMessage();
if (bCanInvokeSPIs) {
// PMode may be null for receipts
if (aPMode == null || aPMode.getMEPBinding().isSynchronous() || aPMode.getMEPBinding().isAsynchronousInitiator() || aState.getEffectivePModeLegNumber() != 1) {
// Call synchronous
// Might add to aErrorMessages
// Might add to aResponseAttachments
// Might add to m_aPullReturnUserMsg
_invokeSPIsForIncoming(aHttpHeaders, aEbmsUserMessage, aEbmsSignalMessage, aPayloadNode, aDecryptedAttachments, aPMode, aState, aErrorMessagesTarget, aResponseAttachments, aSPIResult);
if (aSPIResult.isFailure())
LOGGER.warn("Error invoking synchronous SPIs");
else if (LOGGER.isDebugEnabled())
LOGGER.debug("Successfully invoked synchronous SPIs");
if (m_aSoapProcessingFinalizedCB != null)
m_aSoapProcessingFinalizedCB.onProcessingFinalized(true);
} else {
// Call asynchronous
// Only leg1 can be async!
final IThrowingRunnable<Exception> r = () -> {
// Start async
final ICommonsList<Ebms3Error> aLocalErrorMessages = new CommonsArrayList<>();
final ICommonsList<WSS4JAttachment> aLocalResponseAttachments = new CommonsArrayList<>();
final SPIInvocationResult aAsyncSPIResult = new SPIInvocationResult();
_invokeSPIsForIncoming(aHttpHeaders, aEbmsUserMessage, aEbmsSignalMessage, aPayloadNode, aDecryptedAttachments, aPMode, aState, aLocalErrorMessages, aLocalResponseAttachments, aAsyncSPIResult);
final IAS4ResponseFactory aAsyncResponseFactory;
final String sResponseMessageID;
if (aAsyncSPIResult.isSuccess()) {
// SPI processing succeeded
assert aLocalErrorMessages.isEmpty();
// The response user message has no explicit payload. All data of
// the response user message is in the local attachments
sResponseMessageID = MessageHelperMethods.createRandomMessageID();
final AS4UserMessage aResponseUserMsg = _createReversedUserMessage(eSoapVersion, sResponseMessageID, aEbmsUserMessage, aLocalResponseAttachments);
// Send UserMessage
final AS4SigningParams aSigningParams = new AS4SigningParams().setFromPMode(aEffectiveLeg.getSecurity());
// Use the original receiver ID as the alias into the keystore for
// encrypting the response message
final String sEncryptionAlias = aEbmsUserMessage.getPartyInfo().getTo().getPartyIdAtIndex(0).getValue();
final AS4CryptParams aCryptParams = new AS4CryptParams().setFromPMode(aEffectiveLeg.getSecurity()).setAlias(sEncryptionAlias);
aAsyncResponseFactory = _createResponseUserMessage(aState, aEffectiveLeg.getProtocol().getSoapVersion(), aResponseUserMsg, aResponseAttachments, aSigningParams, aCryptParams);
} else {
// SPI processing failed
// Send ErrorMessage Undefined - see
// https://github.com/phax/phase4/issues/4
final AS4ErrorMessage aResponseErrorMsg = AS4ErrorMessage.create(eSoapVersion, aState.getMessageID(), aLocalErrorMessages);
sResponseMessageID = aResponseErrorMsg.getEbms3SignalMessage().getMessageInfo().getMessageId();
// Pass error messages to the outside
if (m_aErrorConsumer != null && aLocalErrorMessages.isNotEmpty())
m_aErrorConsumer.onAS4ErrorMessage(aState, aLocalErrorMessages, aResponseErrorMsg);
aAsyncResponseFactory = new AS4ResponseFactoryXML(m_aMessageMetadata, aState, sResponseMessageID, aResponseErrorMsg.getAsSoapDocument(), eSoapVersion.getMimeType());
}
// where to send it back (must be determined by SPI!)
final String sAsyncResponseURL = aAsyncSPIResult.getAsyncResponseURL();
if (StringHelper.hasNoText(sAsyncResponseURL))
throw new IllegalStateException("No asynchronous response URL present - please check your SPI implementation");
if (LOGGER.isDebugEnabled())
LOGGER.debug("Responding asynchronous to: " + sAsyncResponseURL);
// Ensure HttpEntity is repeatable
HttpEntity aHttpEntity = aAsyncResponseFactory.getHttpEntityForSending(eSoapVersion.getMimeType());
aHttpEntity = m_aResHelper.createRepeatableHttpEntity(aHttpEntity);
// Use the prebuilt entity for dumping
_invokeSPIsForResponse(aState, aAsyncResponseFactory, aHttpEntity, eSoapVersion.getMimeType(), sResponseMessageID);
// invoke client with new document
final BasicHttpPoster aSender = new BasicHttpPoster();
final Document aAsyncResponse;
if (true) {
final HttpHeaderMap aResponseHttpHeaders = null;
// TODO make async send parameters customizable
final HttpRetrySettings aRetrySettings = new HttpRetrySettings();
aAsyncResponse = aSender.sendGenericMessageWithRetries(sAsyncResponseURL, aResponseHttpHeaders, aHttpEntity, sMessageID, aRetrySettings, new ResponseHandlerXml(), m_aOutgoingDumper, m_aRetryCallback);
} else {
aAsyncResponse = aSender.sendGenericMessage(sAsyncResponseURL, null, aHttpEntity, new ResponseHandlerXml());
}
AS4HttpDebug.debug(() -> "SEND-RESPONSE [async sent] received: " + XMLWriter.getNodeAsString(aAsyncResponse, AS4HttpDebug.getDebugXMLWriterSettings()));
};
final CompletableFuture<Void> aFuture = PhotonWorkerPool.getInstance().runThrowing(CAS4.LIB_NAME + " async processing", r);
if (m_aSoapProcessingFinalizedCB != null) {
// Give the outside world the possibility to get notified when the
// processing is done
aFuture.thenRun(() -> m_aSoapProcessingFinalizedCB.onProcessingFinalized(false));
}
}
}
// Try building error message
final String sResponseMessageID;
final IAS4ResponseFactory ret;
if (!aState.isSoapHeaderElementProcessingSuccessful() || aState.getEbmsError() == null) {
// Not an incoming Ebms Error Message
if (aErrorMessagesTarget.isNotEmpty()) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Creating AS4 error message with these " + aErrorMessagesTarget.size() + " errors: " + aErrorMessagesTarget.getAllMapped(Ebms3Error::getDescriptionValue));
final AS4ErrorMessage aResponseErrorMsg = AS4ErrorMessage.create(eSoapVersion, aState.getMessageID(), aErrorMessagesTarget);
// Call optional consumer
if (m_aErrorConsumer != null)
m_aErrorConsumer.onAS4ErrorMessage(aState, aErrorMessagesTarget, aResponseErrorMsg);
// When aLeg == null, the response is true
if (_isSendErrorAsResponse(aEffectiveLeg)) {
sResponseMessageID = aResponseErrorMsg.getEbms3SignalMessage().getMessageInfo().getMessageId();
ret = new AS4ResponseFactoryXML(m_aMessageMetadata, aState, sResponseMessageID, aResponseErrorMsg.getAsSoapDocument(), eSoapVersion.getMimeType());
} else {
LOGGER.warn("Not sending back the error, because sending error response is prohibited in PMode");
sResponseMessageID = null;
ret = null;
}
} else {
// Do not respond to receipt (except with error message - see above)
if (aEbmsSignalMessage == null || aEbmsSignalMessage.getReceipt() == null) {
// So now the incoming message is a user message or a pull request
if (aPMode.getMEP().isOneWay() || aPMode.getMEPBinding().isAsynchronous()) {
// the pull phase
if (aPMode.getMEPBinding().equals(EMEPBinding.PULL) || (aPMode.getMEPBinding().equals(EMEPBinding.PULL_PUSH) && aSPIResult.hasPullReturnUserMsg()) || (aPMode.getMEPBinding().equals(EMEPBinding.PUSH_PULL) && aSPIResult.hasPullReturnUserMsg())) {
// TODO would be nice to have attachments here I guess
final AS4UserMessage aResponseUserMsg = new AS4UserMessage(eSoapVersion, aSPIResult.getPullReturnUserMsg());
sResponseMessageID = aResponseUserMsg.getEbms3UserMessage().getMessageInfo().getMessageId();
ret = new AS4ResponseFactoryXML(m_aMessageMetadata, aState, sResponseMessageID, aResponseUserMsg.getAsSoapDocument(), eSoapVersion.getMimeType());
} else if (aEbmsUserMessage != null) {
// We received an incoming user message and no errors occurred
final boolean bSendReceiptAsResponse = _isSendReceiptAsResponse(aEffectiveLeg);
if (bSendReceiptAsResponse) {
sResponseMessageID = MessageHelperMethods.createRandomMessageID();
ret = _createResponseReceiptMessage(aState, aSoapDocument, eSoapVersion, sResponseMessageID, aEffectiveLeg, aEbmsUserMessage, aResponseAttachments);
} else {
// TODO what shall we send back here?
LOGGER.info("Not sending back the Receipt response, because sending Receipt response is prohibited in PMode");
sResponseMessageID = null;
ret = null;
}
} else {
sResponseMessageID = null;
ret = null;
}
} else {
// synchronous TWO - WAY (= "SYNC")
final PModeLeg aLeg2 = aPMode.getLeg2();
if (aLeg2 == null)
throw new Phase4Exception("PMode has no leg2!");
if (MEPHelper.isValidResponseTypeLeg2(aPMode.getMEP(), aPMode.getMEPBinding(), EAS4MessageType.USER_MESSAGE)) {
sResponseMessageID = MessageHelperMethods.createRandomMessageID();
final AS4UserMessage aResponseUserMsg = _createReversedUserMessage(eSoapVersion, sResponseMessageID, aEbmsUserMessage, aResponseAttachments);
final AS4SigningParams aSigningParams = new AS4SigningParams().setFromPMode(aLeg2.getSecurity());
final String sEncryptionAlias = aEbmsUserMessage.getPartyInfo().getTo().getPartyIdAtIndex(0).getValue();
final AS4CryptParams aCryptParams = new AS4CryptParams().setFromPMode(aLeg2.getSecurity()).setAlias(sEncryptionAlias);
ret = _createResponseUserMessage(aState, aLeg2.getProtocol().getSoapVersion(), aResponseUserMsg, aResponseAttachments, aSigningParams, aCryptParams);
} else {
sResponseMessageID = null;
ret = null;
}
}
} else {
sResponseMessageID = null;
ret = null;
}
}
} else {
sResponseMessageID = null;
ret = null;
}
// Create the HttpEntity on demand
_invokeSPIsForResponse(aState, ret, null, eSoapVersion.getMimeType(), sResponseMessageID);
return ret;
}
Aggregations