use of com.helger.json.JsonObject in project phase4 by phax.
the class AS4MessagingHelper method getIncomingMetadataAsJson.
/**
* Convert an {@link IAS4IncomingMessageMetadata} structure to a JSON
* representation.
*
* @param aMessageMetadata
* The message metadata to convert. May not be <code>null</code>.
* @return A non-<code>null</code> JSON object.
*/
@Nonnull
@Nonempty
public static IJsonObject getIncomingMetadataAsJson(@Nonnull final IAS4IncomingMessageMetadata aMessageMetadata) {
final IJsonObject aMap = new JsonObject();
aMap.add("IncomingUniqueID", aMessageMetadata.getIncomingUniqueID());
aMap.add("IncomingDT", PDTWebDateHelper.getAsStringXSD(aMessageMetadata.getIncomingDT()));
aMap.add("Mode", aMessageMetadata.getMode().getID());
if (aMessageMetadata.hasRemoteAddr())
aMap.add("RemoteAddr", aMessageMetadata.getRemoteAddr());
if (aMessageMetadata.hasRemoteHost())
aMap.add("RemoteHost", aMessageMetadata.getRemoteHost());
if (aMessageMetadata.hasRemotePort())
aMap.add("RemotePort", aMessageMetadata.getRemotePort());
if (aMessageMetadata.hasRemoteUser())
aMap.add("RemoteUser", aMessageMetadata.getRemoteUser());
{
final IJsonArray aArray = new JsonArray();
for (final Cookie aCookie : aMessageMetadata.cookies()) {
final IJsonObject aCookieObj = new JsonObject();
if (StringHelper.hasText(aCookie.getDomain()))
aCookieObj.add("Domain", aCookie.getDomain());
if (StringHelper.hasText(aCookie.getPath()))
aCookieObj.add("Path", aCookie.getPath());
aCookieObj.add("Secure", aCookie.getSecure());
aCookieObj.add("HttpOnly", aCookie.isHttpOnly());
aCookieObj.add("Name", aCookie.getName());
aCookieObj.add("Value", aCookie.getValue());
aCookieObj.add("Version", aCookie.getVersion());
aCookieObj.add("MaxAge", aCookie.getMaxAge());
if (StringHelper.hasText(aCookie.getComment()))
aCookieObj.add("Comment", aCookie.getComment());
aArray.add(aCookieObj);
}
if (aArray.isNotEmpty())
aMap.addJson("Cookies", aArray);
}
return aMap;
}
use of com.helger.json.JsonObject in project phoss-smp by phax.
the class APIExecutorImportXMLVer1 method invokeAPI.
public void invokeAPI(@Nonnull final IAPIDescriptor aAPIDescriptor, @Nonnull @Nonempty final String sPath, @Nonnull final Map<String, String> aPathVariables, @Nonnull final IRequestWebScopeWithoutResponse aRequestScope, @Nonnull final UnifiedResponse aUnifiedResponse) throws Exception {
final ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, null);
// Is the writable API disabled?
if (SMPMetaManager.getSettings().isRESTWritableAPIDisabled()) {
throw new SMPPreconditionFailedException("The writable REST API is disabled. importServiceGroups will not be executed", aDataProvider.getCurrentURI());
}
final String sLogPrefix = "[REST API Import-XML-V1] ";
final String sPathUserLoginName = aPathVariables.get(SMPRestFilter.PARAM_USER_ID);
LOGGER.info(sLogPrefix + "Starting Import");
// Only authenticated user may do so
final BasicAuthClientCredentials aBasicAuth = getMandatoryAuth(aRequestScope.headers());
SMPUserManagerPhoton.validateUserCredentials(aBasicAuth);
// Start action after authentication
final ISMPServiceGroupManager aServiceGroupMgr = SMPMetaManager.getServiceGroupMgr();
final ISMPBusinessCardManager aBusinessCardMgr = SMPMetaManager.getBusinessCardMgr();
final IUserManager aUserMgr = PhotonSecurityManager.getUserMgr();
final ICommonsSet<String> aAllServiceGroupIDs = aServiceGroupMgr.getAllSMPServiceGroupIDs();
final ICommonsSet<String> aAllBusinessCardIDs = aBusinessCardMgr.getAllSMPBusinessCardIDs();
// Try to use ID or login name
IUser aDefaultOwner = aUserMgr.getUserOfID(sPathUserLoginName);
if (aDefaultOwner == null)
aDefaultOwner = aUserMgr.getUserOfLoginName(sPathUserLoginName);
if (aDefaultOwner == null || aDefaultOwner.isDeleted()) {
// Setting the owner to a disabled user might make sense
throw new SMPBadRequestException(sLogPrefix + "The user ID or login name '" + sPathUserLoginName + "' does not exist", aDataProvider.getCurrentURI());
}
LOGGER.info(sLogPrefix + "Using '" + aDefaultOwner.getID() + "' / '" + aDefaultOwner.getLoginName() + "' as the default owner");
final boolean bOverwriteExisting = aRequestScope.params().getAsBoolean(PARAM_OVERVWRITE_EXISTING, DEFAULT_OVERWRITE_EXISTING);
final byte[] aPayload = StreamHelper.getAllBytes(aRequestScope.getRequest().getInputStream());
final IMicroDocument aDoc = MicroReader.readMicroXML(aPayload);
if (aDoc == null || aDoc.getDocumentElement() == null) {
// Cannot parse
throw new SMPBadRequestException("Failed to parse XML payload", aDataProvider.getCurrentURI());
}
final String sVersion = aDoc.getDocumentElement().getAttributeValue(CSMPExchange.ATTR_VERSION);
if (!CSMPExchange.VERSION_10.equals(sVersion)) {
throw new SMPBadRequestException("The provided payload is not an XML file version 1.0", aDataProvider.getCurrentURI());
}
// Version 1.0
LOGGER.info(sLogPrefix + "The provided payload is an XML file version 1.0");
final ZonedDateTime aQueryDT = PDTFactory.getCurrentZonedDateTimeUTC();
final StopWatch aSW = StopWatch.createdStarted();
// Start the import
final ICommonsList<ImportActionItem> aActionList = new CommonsArrayList<>();
final ImportSummary aImportSummary = new ImportSummary();
ServiceGroupImport.importXMLVer10(aDoc.getDocumentElement(), bOverwriteExisting, aDefaultOwner, aAllServiceGroupIDs, aAllBusinessCardIDs, aActionList, aImportSummary);
aSW.stop();
LOGGER.info(sLogPrefix + "Finished import after " + aSW.getMillis() + " milliseconds");
// Everything added to the action list is already logged
final boolean bResponseAsXML = true;
if (bResponseAsXML) {
// Create XML version
final IMicroDocument aResponseDoc = new MicroDocument();
final IMicroElement eRoot = aResponseDoc.appendElement("importResult");
eRoot.setAttribute("version", "1");
eRoot.setAttribute("importStartDateTime", PDTWebDateHelper.getAsStringXSD(aQueryDT));
final IMicroElement eSettings = eRoot.appendElement("settings");
eSettings.setAttribute("overwriteExisting", bOverwriteExisting);
eSettings.setAttribute("defaultOwnerID", aDefaultOwner.getID());
eSettings.setAttribute("defaultOwnerLoginName", aDefaultOwner.getLoginName());
final ICommonsMap<String, MutableInt> aErrorLevelCount = new CommonsTreeMap<>();
for (final ImportActionItem aAction : aActionList) {
eRoot.appendChild(aAction.getAsMicroElement("action"));
aErrorLevelCount.computeIfAbsent(aAction.getErrorLevelName(), k -> new MutableInt(0)).inc();
}
{
final IMicroElement eSummary = eRoot.appendElement("summary");
eSummary.setAttribute("durationMillis", aSW.getMillis());
for (final Map.Entry<String, MutableInt> aEntry : aErrorLevelCount.entrySet()) eSummary.appendElement("errorlevel").setAttribute("id", aEntry.getKey()).setAttribute("count", aEntry.getValue().intValue());
aImportSummary.appendTo(eSummary);
}
final XMLWriterSettings aXWS = new XMLWriterSettings().setIndent(EXMLSerializeIndent.INDENT_AND_ALIGN);
aUnifiedResponse.setContentAndCharset(MicroWriter.getNodeAsString(aResponseDoc, aXWS), aXWS.getCharset()).setMimeType(new MimeType(CMimeType.APPLICATION_XML).addParameter(CMimeType.PARAMETER_NAME_CHARSET, aXWS.getCharset().name()));
} else {
// Create JSON version
final IJsonObject aJson = new JsonObject();
aJson.add("version", "1");
aJson.add("importStartDateTime", DateTimeFormatter.ISO_ZONED_DATE_TIME.format(aQueryDT));
aJson.addJson("settings", new JsonObject().add("overwriteExisting", bOverwriteExisting).add("defaultOwnerID", aDefaultOwner.getID()).add("defaultOwnerLoginName", aDefaultOwner.getLoginName()));
final IJsonArray aActions = new JsonArray();
final ICommonsMap<String, MutableInt> aLevelCount = new CommonsTreeMap<>();
for (final ImportActionItem aAction : aActionList) {
aActions.add(aAction.getAsJsonObject());
aLevelCount.computeIfAbsent(aAction.getErrorLevelName(), k -> new MutableInt(0)).inc();
}
aJson.addJson("actions", aActions);
{
final IJsonObject aSummary = new JsonObject();
aSummary.add("durationMillis", aSW.getMillis());
final IJsonArray aLevels = new JsonArray();
for (final Map.Entry<String, MutableInt> aEntry : aLevelCount.entrySet()) aLevels.add(new JsonObject().add("id", aEntry.getKey()).add("count", aEntry.getValue().intValue()));
aSummary.addJson("errorlevels", aLevels);
aImportSummary.appendTo(aSummary);
aJson.addJson("summary", aSummary);
}
final String sRet = new JsonWriter(JsonWriterSettings.DEFAULT_SETTINGS_FORMATTED).writeAsString(aJson);
aUnifiedResponse.setContentAndCharset(sRet, StandardCharsets.UTF_8).setMimeType(CMimeType.APPLICATION_JSON);
}
aUnifiedResponse.disableCaching();
}
use of com.helger.json.JsonObject in project phoss-smp by phax.
the class SMPStatusProvider method getStatusDisabledData.
@Nonnull
@ReturnsMutableCopy
public static IJsonObject getStatusDisabledData() {
final IJsonObject aStatusData = new JsonObject();
aStatusData.add("status.enabled", false);
return aStatusData;
}
use of com.helger.json.JsonObject in project phoss-smp by phax.
the class APIExecutorQueryGetDocTypes method invokeAPI.
public void invokeAPI(@Nonnull final IAPIDescriptor aAPIDescriptor, @Nonnull @Nonempty final String sPath, @Nonnull final Map<String, String> aPathVariables, @Nonnull final IRequestWebScopeWithoutResponse aRequestScope, @Nonnull final UnifiedResponse aUnifiedResponse) throws Exception {
final String sPathServiceGroupID = aPathVariables.get(SMPRestFilter.PARAM_SERVICE_GROUP_ID);
final ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, sPathServiceGroupID);
// Is the remote query API disabled?
if (SMPServerConfiguration.isRestRemoteQueryAPIDisabled()) {
throw new SMPPreconditionFailedException("The remote query API is disabled. getRemoteDocTypes will not be executed", aDataProvider.getCurrentURI());
}
final IIdentifierFactory aIF = SMPMetaManager.getIdentifierFactory();
final ESMPAPIType eAPIType = SMPServerConfiguration.getRESTType().getAPIType();
final IParticipantIdentifier aParticipantID = aIF.parseParticipantIdentifier(sPathServiceGroupID);
if (aParticipantID == null) {
throw SMPBadRequestException.failedToParseSG(sPathServiceGroupID, aDataProvider.getCurrentURI());
}
final SMPQueryParams aQueryParams = SMPQueryParams.create(eAPIType, aParticipantID);
final boolean bQueryBusinessCard = aRequestScope.params().getAsBoolean("businessCard", false);
final boolean bXMLSchemaValidation = aRequestScope.params().getAsBoolean("xmlSchemaValidation", true);
final ZonedDateTime aQueryDT = PDTFactory.getCurrentZonedDateTimeUTC();
final StopWatch aSW = StopWatch.createdStarted();
final String sLogPrefix = "[QueryAPI] ";
LOGGER.info(sLogPrefix + "Document types of '" + aParticipantID.getURIEncoded() + "' are queried using SMP API '" + eAPIType + "' from '" + aQueryParams.getSMPHostURI() + "'; XSD validation=" + bXMLSchemaValidation);
ICommonsSortedMap<String, String> aSGHrefs = null;
switch(eAPIType) {
case PEPPOL:
{
final SMPClientReadOnly aSMPClient = new SMPClientReadOnly(aQueryParams.getSMPHostURI());
aSMPClient.setXMLSchemaValidation(bXMLSchemaValidation);
// Get all HRefs and sort them by decoded URL
final com.helger.xsds.peppol.smp1.ServiceGroupType aSG = aSMPClient.getServiceGroupOrNull(aParticipantID);
// Map from cleaned URL to original URL
if (aSG != null && aSG.getServiceMetadataReferenceCollection() != null) {
aSGHrefs = new CommonsTreeMap<>();
for (final com.helger.xsds.peppol.smp1.ServiceMetadataReferenceType aSMR : aSG.getServiceMetadataReferenceCollection().getServiceMetadataReference()) {
// Decoded href is important for unification
final String sHref = CIdentifier.createPercentDecoded(aSMR.getHref());
if (aSGHrefs.put(sHref, aSMR.getHref()) != null)
LOGGER.warn(sLogPrefix + "The ServiceGroup list contains the duplicate URL '" + sHref + "'");
}
}
break;
}
case OASIS_BDXR_V1:
{
aSGHrefs = new CommonsTreeMap<>();
final BDXRClientReadOnly aBDXR1Client = new BDXRClientReadOnly(aQueryParams.getSMPHostURI());
aBDXR1Client.setXMLSchemaValidation(bXMLSchemaValidation);
// Get all HRefs and sort them by decoded URL
final com.helger.xsds.bdxr.smp1.ServiceGroupType aSG = aBDXR1Client.getServiceGroupOrNull(aParticipantID);
// Map from cleaned URL to original URL
if (aSG != null && aSG.getServiceMetadataReferenceCollection() != null) {
aSGHrefs = new CommonsTreeMap<>();
for (final com.helger.xsds.bdxr.smp1.ServiceMetadataReferenceType aSMR : aSG.getServiceMetadataReferenceCollection().getServiceMetadataReference()) {
// Decoded href is important for unification
final String sHref = CIdentifier.createPercentDecoded(aSMR.getHref());
if (aSGHrefs.put(sHref, aSMR.getHref()) != null)
LOGGER.warn(sLogPrefix + "The ServiceGroup list contains the duplicate URL '" + sHref + "'");
}
}
break;
}
}
IJsonObject aJson = null;
if (aSGHrefs != null)
aJson = SMPJsonResponse.convert(eAPIType, aParticipantID, aSGHrefs, aIF);
if (bQueryBusinessCard) {
final String sBCURL = aQueryParams.getSMPHostURI().toString() + "/businesscard/" + aParticipantID.getURIEncoded();
LOGGER.info(sLogPrefix + "Querying BC from '" + sBCURL + "'");
byte[] aData;
try (HttpClientManager aHttpClientMgr = new HttpClientManager()) {
final HttpGet aGet = new HttpGet(sBCURL);
aData = aHttpClientMgr.execute(aGet, new ResponseHandlerByteArray());
} catch (final Exception ex) {
aData = null;
}
if (aData == null)
LOGGER.warn(sLogPrefix + "No Business Card is available for that participant.");
else {
final PDBusinessCard aBC = PDBusinessCardHelper.parseBusinessCard(aData, (Charset) null);
if (aBC == null) {
LOGGER.error(sLogPrefix + "Failed to parse BC:\n" + new String(aData, StandardCharsets.UTF_8));
} else {
// Business Card found
if (aJson == null)
aJson = new JsonObject();
aJson.addJson("businessCard", aBC.getAsJson());
}
}
}
aSW.stop();
if (aJson == null) {
LOGGER.error(sLogPrefix + "Failed to perform the SMP lookup");
aUnifiedResponse.setStatus(CHttp.HTTP_NOT_FOUND);
} else {
LOGGER.info(sLogPrefix + "Succesfully finished lookup lookup after " + aSW.getMillis() + " milliseconds");
aJson.add("queryDateTime", DateTimeFormatter.ISO_ZONED_DATE_TIME.format(aQueryDT));
aJson.add("queryDurationMillis", aSW.getMillis());
final String sRet = new JsonWriter(JsonWriterSettings.DEFAULT_SETTINGS_FORMATTED).writeAsString(aJson);
aUnifiedResponse.setContentAndCharset(sRet, StandardCharsets.UTF_8).setMimeType(CMimeType.APPLICATION_JSON).enableCaching(1 * CGlobal.SECONDS_PER_HOUR);
}
}
use of com.helger.json.JsonObject in project peppol-commons by phax.
the class SMPJsonResponse method convert.
@Nonnull
public static IJsonObject convert(@Nonnull final IParticipantIdentifier aParticipantID, @Nonnull final IDocumentTypeIdentifier aDocTypeID, @Nonnull final com.helger.xsds.peppol.smp1.ServiceMetadataType aSM) {
ValueEnforcer.notNull(aParticipantID, "ParticipantID");
ValueEnforcer.notNull(aDocTypeID, "DocTypeID");
ValueEnforcer.notNull(aSM, "SM");
final IJsonObject ret = new JsonObject();
ret.add(JSON_SMPTYPE, ESMPAPIType.PEPPOL.getID());
ret.add(JSON_PARTICIPANT_ID, aParticipantID.getURIEncoded());
ret.add(JSON_DOCUMENT_TYPE_ID, aDocTypeID.getURIEncoded());
final com.helger.xsds.peppol.smp1.RedirectType aRedirect = aSM.getRedirect();
if (aRedirect != null) {
final IJsonObject aJsonRedirect = new JsonObject().add(JSON_HREF, aRedirect.getHref()).add(JSON_CERTIFICATE_UID, aRedirect.getCertificateUID()).add(JSON_EXTENSION, SMPExtensionConverter.convertToString(aRedirect.getExtension()));
ret.addJson(JSON_REDIRECT, aJsonRedirect);
} else {
final com.helger.xsds.peppol.smp1.ServiceInformationType aSI = aSM.getServiceInformation();
final IJsonObject aJsonSI = new JsonObject();
{
final IJsonArray aJsonProcs = new JsonArray();
// For all processes
if (aSI.getProcessList() != null)
for (final com.helger.xsds.peppol.smp1.ProcessType aProcess : aSI.getProcessList().getProcess()) if (aProcess.getProcessIdentifier() != null) {
final IJsonObject aJsonProc = new JsonObject().add(JSON_PROCESS_ID, CIdentifier.getURIEncoded(aProcess.getProcessIdentifier()));
final IJsonArray aJsonEPs = new JsonArray();
// For all endpoints
if (aProcess.getServiceEndpointList() != null)
for (final com.helger.xsds.peppol.smp1.EndpointType aEndpoint : aProcess.getServiceEndpointList().getEndpoint()) {
aJsonEPs.add(convertEndpoint(aEndpoint));
}
aJsonProc.addJson(JSON_ENDPOINTS, aJsonEPs).add(JSON_EXTENSION, SMPExtensionConverter.convertToString(aProcess.getExtension()));
aJsonProcs.add(aJsonProc);
}
aJsonSI.addJson(JSON_PROCESSES, aJsonProcs).add(JSON_EXTENSION, SMPExtensionConverter.convertToString(aSI.getExtension()));
}
ret.addJson(JSON_SERVICEINFO, aJsonSI);
}
return ret;
}
Aggregations