use of com.helger.json.IJsonObject in project peppol-commons by phax.
the class SMPJsonResponse method convertCertificate.
public static void convertCertificate(@Nonnull final IJsonObject aTarget, @Nonnull final String sCert) {
ValueEnforcer.notNull(aTarget, "Target");
ValueEnforcer.notNull(sCert, "Cert");
aTarget.add(JSON_CERTIFICATE, sCert);
final X509Certificate aCert = CertificateHelper.convertStringToCertficateOrNull(sCert);
final IJsonObject aDetails = new JsonObject();
aDetails.add("parsable", aCert != null);
if (aCert != null) {
aDetails.addJson("subject", getJsonPrincipal(aCert.getSubjectX500Principal()));
aDetails.addJson("issuer", getJsonPrincipal(aCert.getIssuerX500Principal()));
aDetails.add("serial10", aCert.getSerialNumber());
aDetails.add("serial16", aCert.getSerialNumber().toString(16));
aDetails.addIfNotNull("notBefore", getLDT(PDTFactory.createOffsetDateTime(aCert.getNotBefore())));
aDetails.addIfNotNull("notAfter", getLDT(PDTFactory.createOffsetDateTime(aCert.getNotAfter())));
aDetails.add("validByDate", CertificateHelper.isCertificateValidPerNow(aCert));
aDetails.add("sigAlgName", aCert.getSigAlgName());
}
aTarget.addJson(JSON_CERTIFICATE_DETAILS, aDetails);
}
use of com.helger.json.IJsonObject 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.bdxr.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.OASIS_BDXR_V1.getID());
ret.add(JSON_PARTICIPANT_ID, aParticipantID.getURIEncoded());
ret.add(JSON_DOCUMENT_TYPE_ID, aDocTypeID.getURIEncoded());
final com.helger.xsds.bdxr.smp1.RedirectType aRedirect = aSM.getRedirect();
if (aRedirect != null) {
final IJsonObject aJsonRedirect = new JsonObject().add(JSON_HREF, aRedirect.getHref()).add(JSON_CERTIFICATE_UID, aRedirect.getCertificateUID()).addIfNotNull(JSON_EXTENSION, BDXR1ExtensionConverter.convertToJson(aRedirect.getExtension()));
ret.addJson(JSON_REDIRECT, aJsonRedirect);
} else {
final com.helger.xsds.bdxr.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.bdxr.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.bdxr.smp1.EndpointType aEndpoint : aProcess.getServiceEndpointList().getEndpoint()) {
aJsonEPs.add(convertEndpoint(aEndpoint));
}
aJsonProc.addJson(JSON_ENDPOINTS, aJsonEPs).addIfNotNull(JSON_EXTENSION, BDXR1ExtensionConverter.convertToJson(aProcess.getExtension()));
aJsonProcs.add(aJsonProc);
}
aJsonSI.addJson(JSON_PROCESSES, aJsonProcs).addIfNotNull(JSON_EXTENSION, BDXR1ExtensionConverter.convertToJson(aSI.getExtension()));
}
ret.addJson(JSON_SERVICEINFO, aJsonSI);
}
return ret;
}
use of com.helger.json.IJsonObject in project peppol-commons by phax.
the class SMPJsonResponse method convert.
@Nonnull
public static IJsonObject convert(@Nonnull final ESMPAPIType eSMPAPIType, @Nonnull final IParticipantIdentifier aParticipantID, @Nonnull final Map<String, String> aSGHrefs, @Nonnull final IIdentifierFactory aIF) {
ValueEnforcer.notNull(eSMPAPIType, "SMPAPIType");
ValueEnforcer.notNull(aParticipantID, "ParticipantID");
ValueEnforcer.notNull(aSGHrefs, "SGHrefs");
ValueEnforcer.notNull(aIF, "IF");
final IJsonObject aJson = new JsonObject();
aJson.add(JSON_SMPTYPE, eSMPAPIType.getID());
aJson.add(JSON_PARTICIPANT_ID, aParticipantID.getURIEncoded());
final String sPathStart = "/" + aParticipantID.getURIEncoded() + '/' + BDXRClientReadOnly.URL_PART_SERVICES + '/';
final IJsonArray aURLsArray = new JsonArray();
// Show all ServiceGroup hrefs
for (final Map.Entry<String, String> aEntry : aSGHrefs.entrySet()) {
final String sHref = aEntry.getKey();
final String sOriginalHref = aEntry.getValue();
final IJsonObject aUrlEntry = new JsonObject().add(JSON_HREF, sOriginalHref);
// Should be case insensitive "indexOf" here
final int nPathStart = sHref.toLowerCase(Locale.US).indexOf(sPathStart.toLowerCase(Locale.US));
if (nPathStart >= 0) {
final String sDocType = sHref.substring(nPathStart + sPathStart.length());
aUrlEntry.add(JSON_DOCUMENT_TYPE_ID, sDocType);
final IDocumentTypeIdentifier aDocType = aIF.parseDocumentTypeIdentifier(sDocType);
if (aDocType == null) {
aUrlEntry.add(JSON_ERROR, "The document type ID could not be interpreted as a structured document type!");
}
} else {
aUrlEntry.add(JSON_ERROR, "Contained href does not match the rules. Expected path part: '" + sPathStart + "'");
}
aURLsArray.add(aUrlEntry);
}
aJson.addJson(JSON_URLS, aURLsArray);
return aJson;
}
use of com.helger.json.IJsonObject in project peppol-commons by phax.
the class BDXR1ExtensionConverter method convertToJson.
/**
* Convert the passed extension type to a JSON representation.
*
* @param aExtension
* The extension to be converted. May be <code>null</code>.
* @return <code>null</code> if no extension or an empty extension was passed
* - the JSON of the extension otherwise.
*/
@Nullable
public static IJsonObject convertToJson(@Nullable final ExtensionType aExtension) {
// If there is no extension present, nothing to convert
if (aExtension != null && aExtension.getAny() != null) {
Object aAny = aExtension.getAny();
if (aAny instanceof Element) {
// Convert XML to String
aAny = XMLWriter.getNodeAsString((Element) aAny, XWS);
}
final JsonObject ret = new JsonObject();
if (aExtension.getExtensionID() != null)
ret.add(JSON_ID, aExtension.getExtensionID());
if (aExtension.getExtensionName() != null)
ret.add(JSON_NAME, aExtension.getExtensionName());
if (aExtension.getExtensionAgencyID() != null)
ret.add(JSON_AGENCY_ID, aExtension.getExtensionAgencyID());
if (aExtension.getExtensionAgencyName() != null)
ret.add(JSON_AGENCY_NAME, aExtension.getExtensionAgencyName());
if (aExtension.getExtensionAgencyURI() != null)
ret.add(JSON_AGENCY_URI, aExtension.getExtensionAgencyURI());
if (aExtension.getExtensionVersionID() != null)
ret.add(JSON_VERSION_ID, aExtension.getExtensionVersionID());
if (aExtension.getExtensionURI() != null)
ret.add(JSON_URI, aExtension.getExtensionURI());
if (aExtension.getExtensionReasonCode() != null)
ret.add(JSON_REASON_CODE, aExtension.getExtensionReasonCode());
if (aExtension.getExtensionReason() != null)
ret.add(JSON_REASON, aExtension.getExtensionReason());
if (aAny != null)
ret.add(JSON_ANY, aAny);
return ret;
}
return null;
}
use of com.helger.json.IJsonObject in project peppol-commons by phax.
the class BDXR1ExtensionConverter method convert.
/**
* Convert the passed Json string to a list of SMP extensions.
*
* @param sJson
* the Json representation to be converted.
* @return <code>null</code> if the passed string is empty or cannot be
* interpreted as JSON.
*/
@Nullable
public static ICommonsList<ExtensionType> convert(@Nullable final String sJson) {
if (StringHelper.hasText(sJson)) {
// Try to interpret as JSON
final IJson aJson = JsonReader.readFromString(sJson);
if (aJson == null || !aJson.isArray()) {
LOGGER.warn("Error in parsing extension JSON '" + sJson + "'");
} else {
final ICommonsList<ExtensionType> ret = new CommonsArrayList<>();
aJson.getAsArray().forEach(aChild -> {
final IJsonObject aObject = aChild.getAsObject();
final ExtensionType aExt = new ExtensionType();
aExt.setExtensionID(aObject.getAsString(JSON_ID));
aExt.setExtensionName(aObject.getAsString(JSON_NAME));
aExt.setExtensionAgencyID(aObject.getAsString(JSON_AGENCY_ID));
aExt.setExtensionAgencyName(aObject.getAsString(JSON_AGENCY_NAME));
aExt.setExtensionAgencyURI(aObject.getAsString(JSON_AGENCY_URI));
aExt.setExtensionVersionID(aObject.getAsString(JSON_VERSION_ID));
aExt.setExtensionURI(aObject.getAsString(JSON_URI));
aExt.setExtensionReasonCode(aObject.getAsString(JSON_REASON_CODE));
aExt.setExtensionReason(aObject.getAsString(JSON_REASON));
final String sAny = aObject.getAsString(JSON_ANY);
if (StringHelper.hasText(sAny)) {
final Document aDoc = DOMReader.readXMLDOM(sAny);
if (aDoc != null)
aExt.setAny(aDoc.getDocumentElement());
}
ret.add(aExt);
});
return ret;
}
}
return null;
}
Aggregations