|
| 1 | +/* |
| 2 | + * Copyright (C) 2015-2025 Philip Helger (www.helger.com) |
| 3 | + * philip[at]helger[dot]com |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.helger.phase4.peppol.server.servlet; |
| 18 | + |
| 19 | +import org.jspecify.annotations.NonNull; |
| 20 | +import org.slf4j.Logger; |
| 21 | + |
| 22 | +import com.helger.http.EHttpMethod; |
| 23 | +import com.helger.phase4.incoming.AS4IncomingMessageMetadata; |
| 24 | +import com.helger.phase4.incoming.AS4RequestHandler; |
| 25 | +import com.helger.phase4.logging.Phase4LogCustomizer; |
| 26 | +import com.helger.phase4.logging.Phase4LoggerFactory; |
| 27 | +import com.helger.phase4.servlet.AS4UnifiedResponse; |
| 28 | +import com.helger.phase4.servlet.AS4XServletHandler; |
| 29 | +import com.helger.phase4.servlet.IAS4ServletRequestHandlerCustomizer; |
| 30 | +import com.helger.web.scope.IRequestWebScopeWithoutResponse; |
| 31 | +import com.helger.xservlet.AbstractXServlet; |
| 32 | + |
| 33 | +/** |
| 34 | + * AS4 receiving servlet.<br> |
| 35 | + * This servlet works only, if a single AS4 profile is present. If multiple AS4 profiles should be |
| 36 | + * served, it is recommended to provide two different servlets and customize the |
| 37 | + * {@link AS4XServletHandler} accordingly. See |
| 38 | + * https://github.com/phax/phase4/wiki/Multi-Profile-Handling for a more detailed description.<br> |
| 39 | + * Use a configuration like the following in your <code>WEB-INF/web.xm</code> file: |
| 40 | + * |
| 41 | + * <pre> |
| 42 | +<servlet> |
| 43 | + <servlet-name>AS4Servlet</servlet-name> |
| 44 | + <servlet-class>com.helger.phase4.servlet.AS4Servlet</servlet-class> |
| 45 | +</servlet> |
| 46 | +<servlet-mapping> |
| 47 | + <servlet-name>AS4Servlet</servlet-name> |
| 48 | + <url-pattern>/as4</url-pattern> |
| 49 | +</servlet-mapping> |
| 50 | + * </pre> |
| 51 | + * |
| 52 | + * @author Philip Helger |
| 53 | + */ |
| 54 | +public class CustomAS4Servlet extends AbstractXServlet |
| 55 | +{ |
| 56 | + private static final Logger LOGGER = Phase4LoggerFactory.getLogger (CustomAS4Servlet.class); |
| 57 | + |
| 58 | + public CustomAS4Servlet () |
| 59 | + { |
| 60 | + // Multipart is handled specifically inside |
| 61 | + settings ().setMultipartEnabled (false); |
| 62 | + |
| 63 | + // HTTP POST only |
| 64 | + final IAS4ServletRequestHandlerCustomizer aCustomizer = new IAS4ServletRequestHandlerCustomizer () |
| 65 | + { |
| 66 | + public void customizeBeforeHandling (@NonNull final IRequestWebScopeWithoutResponse aRequestScope, |
| 67 | + @NonNull final AS4UnifiedResponse aUnifiedResponse, |
| 68 | + @NonNull final AS4RequestHandler aRequestHandler) |
| 69 | + { |
| 70 | + // In case you want a custom Incoming Unique ID |
| 71 | + if (false) |
| 72 | + ((AS4IncomingMessageMetadata) aRequestHandler.getMessageMetadata ()).setIncomingUniqueID ("bla bla bla"); |
| 73 | + |
| 74 | + // Just to show how it works - thread specific prefix |
| 75 | + Phase4LogCustomizer.setThreadLocalLogPrefix ("@" + |
| 76 | + aRequestHandler.getMessageMetadata ().getIncomingUniqueID () + |
| 77 | + " - "); |
| 78 | + |
| 79 | + // And this shows how to access AS4 Error Messages returned |
| 80 | + aRequestHandler.setErrorConsumer ( (aIncomingState, aEbmsErrors, aAS4ErrorMsg) -> { |
| 81 | + LOGGER.error ("!!! An AS4 error occured: " + aAS4ErrorMsg); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + public void customizeAfterHandling (@NonNull final IRequestWebScopeWithoutResponse aRequestScope, |
| 86 | + @NonNull final AS4UnifiedResponse aUnifiedResponse, |
| 87 | + @NonNull final AS4RequestHandler aRequestHandler) |
| 88 | + { |
| 89 | + Phase4LogCustomizer.clearThreadLocals (); |
| 90 | + } |
| 91 | + }; |
| 92 | + handlerRegistry ().registerHandler (EHttpMethod.POST, |
| 93 | + new AS4XServletHandler ().setRequestHandlerCustomizer (aCustomizer)); |
| 94 | + } |
| 95 | +} |
0 commit comments