use of javax.annotation.WillClose 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 javax.annotation.WillClose in project ph-commons by phax.
the class SettingsPersistenceJson method readSettings.
@Nonnull
public ISettings readSettings(@Nonnull @WillClose final InputStream aIS) {
ValueEnforcer.notNull(aIS, "InputStream");
// Create the settings object
final ISettings aSettings = m_aSettingsFactory.apply(getReadSettingsName());
// Read the properties file from the input stream
final IJsonObject aProps = JsonReader.builder().source(aIS, m_aCharset).customizeCallback(aParser -> {
aParser.setRequireStringQuotes(false);
aParser.setAlwaysUseBigNumber(true);
}).readAsObject();
if (aProps != null)
for (final Map.Entry<String, IJson> aEntry : aProps) _recursiveReadSettings(aEntry.getKey(), aEntry.getValue(), aSettings);
return aSettings;
}
use of javax.annotation.WillClose in project ph-commons by phax.
the class DOMReader method readXMLDOM.
@Nullable
public static Document readXMLDOM(@WillClose @Nonnull final InputSource aInputSource, @Nonnull final IDOMReaderSettings aSettings) {
ValueEnforcer.notNull(aInputSource, "InputSource");
ValueEnforcer.notNull(aSettings, "Settings");
Document aDoc = null;
try {
final StopWatch aSW = StopWatch.createdStarted();
final DocumentBuilder aDocumentBuilder;
boolean bFromPool = false;
if (aSettings.requiresNewXMLParser()) {
// We need to create a new DocumentBuilderFactory
final DocumentBuilderFactory aDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
// Apply the settings on the DocumentBuilderFactory
aSettings.applyToDocumentBuilderFactory(aDocumentBuilderFactory);
// Ready to create document builder
aDocumentBuilder = aDocumentBuilderFactory.newDocumentBuilder();
} else {
// Use one from the pool
aDocumentBuilder = POOL.borrowObject();
bFromPool = true;
}
try {
// Apply settings on DocumentBuilder
aSettings.applyToDocumentBuilder(aDocumentBuilder);
// Ensure a collecting error handler is present
final CollectingSAXErrorHandler aCEH;
final ErrorHandler aCustomErrorHandler = aSettings.getErrorHandler();
if (aCustomErrorHandler instanceof CollectingSAXErrorHandler)
aCEH = (CollectingSAXErrorHandler) aCustomErrorHandler;
else {
aCEH = new CollectingSAXErrorHandler();
aDocumentBuilder.setErrorHandler(aCEH.andThen(aCustomErrorHandler));
}
// Main parsing
aDoc = aDocumentBuilder.parse(aInputSource);
// Statistics update
if (aSettings.getSchema() == null)
STARS_DOM_TIMER.addTime(aSW.stopAndGetMillis());
else
STATS_DOM_SCHEMA_TIMER.addTime(aSW.stopAndGetMillis());
// collected errors
if (aCEH.containsAtLeastOneError())
return null;
} finally {
if (bFromPool) {
// Return to the pool
POOL.returnObject(aDocumentBuilder);
}
}
} catch (final Exception ex) {
aSettings.exceptionCallbacks().forEach(x -> x.onException(ex));
STATS_DOM_ERROR_COUNTER.increment();
} finally {
// Close both byte stream and character stream, as we don't know which one
// was used
StreamHelper.close(aInputSource.getByteStream());
StreamHelper.close(aInputSource.getCharacterStream());
}
return aDoc;
}
use of javax.annotation.WillClose in project ph-commons by phax.
the class SAXReader method readXMLSAX.
/**
* Read an XML document via a SAX handler. The streams are closed after
* reading.
*
* @param aIS
* The input source to read from. Automatically closed upon success or
* error. May not be <code>null</code>.
* {@link com.helger.xml.sax.InputSourceFactory} may be used to create
* {@link InputSource} objects from different input types.
* @param aSettings
* Reader settings. At least a content handler should be set. May be
* <code>null</code>.
* @return {@link ESuccess#SUCCESS} if reading succeeded,
* {@link ESuccess#FAILURE} otherwise
*/
@Nonnull
public static ESuccess readXMLSAX(@WillClose @Nonnull final InputSource aIS, @Nonnull final ISAXReaderSettings aSettings) {
ValueEnforcer.notNull(aIS, "InputStream");
ValueEnforcer.notNull(aSettings, "Settings");
try {
boolean bFromPool = false;
org.xml.sax.XMLReader aParser;
if (aSettings.requiresNewXMLParser()) {
aParser = SAXReaderFactory.createXMLReader();
} else {
// use parser from pool
aParser = POOL.borrowObject();
bFromPool = true;
}
try {
final StopWatch aSW = StopWatch.createdStarted();
// Apply settings
aSettings.applyToSAXReader(aParser);
// Start parsing
aParser.parse(aIS);
// Statistics
STATS_SAX_SUCCESS_COUNTER.increment();
STATS_SAX_TIMER.addTime(aSW.stopAndGetMillis());
return ESuccess.SUCCESS;
} finally {
if (bFromPool) {
// Return parser to pool
POOL.returnObject(aParser);
}
}
} catch (final SAXParseException ex) {
boolean bHandled = false;
if (aSettings.getErrorHandler() != null)
try {
aSettings.getErrorHandler().fatalError(ex);
bHandled = true;
} catch (final SAXException ex2) {
// fall-through
}
if (!bHandled)
aSettings.exceptionCallbacks().forEach(x -> x.onException(ex));
} catch (final Exception ex) {
aSettings.exceptionCallbacks().forEach(x -> x.onException(ex));
} finally {
// Close both byte stream and character stream, as we don't know which one
// was used
StreamHelper.close(aIS.getByteStream());
StreamHelper.close(aIS.getCharacterStream());
}
STATS_SAX_ERROR_COUNTER.increment();
return ESuccess.FAILURE;
}
Aggregations