Skip to content

Commit 71aae78

Browse files
committed
Example code with a custom AS4 servlet
1 parent 1cdd027 commit 71aae78

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
&lt;servlet&gt;
43+
&lt;servlet-name&gt;AS4Servlet&lt;/servlet-name&gt;
44+
&lt;servlet-class&gt;com.helger.phase4.servlet.AS4Servlet&lt;/servlet-class&gt;
45+
&lt;/servlet&gt;
46+
&lt;servlet-mapping&gt;
47+
&lt;servlet-name&gt;AS4Servlet&lt;/servlet-name&gt;
48+
&lt;url-pattern&gt;/as4&lt;/url-pattern&gt;
49+
&lt;/servlet-mapping&gt;
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+
}

phase4-peppol-server-webapp/src/main/webapp/WEB-INF/web.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
<servlet>
4545
<servlet-name>Phase4PeppolServlet</servlet-name>
46-
<servlet-class>com.helger.phase4.servlet.AS4Servlet</servlet-class>
46+
<servlet-class>com.helger.phase4.peppol.server.servlet.CustomAS4Servlet</servlet-class>
4747
</servlet>
4848
<servlet-mapping>
4949
<servlet-name>Phase4PeppolServlet</servlet-name>

0 commit comments

Comments
 (0)