Search in sources :

Example 6 with JsonObject

use of com.helger.json.JsonObject 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);
}
Also used : IJsonObject(com.helger.json.IJsonObject) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) X509Certificate(java.security.cert.X509Certificate)

Example 7 with JsonObject

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.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;
}
Also used : JsonArray(com.helger.json.JsonArray) IJsonArray(com.helger.json.IJsonArray) IJsonObject(com.helger.json.IJsonObject) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) IJsonArray(com.helger.json.IJsonArray) Nonnull(javax.annotation.Nonnull)

Example 8 with JsonObject

use of com.helger.json.JsonObject 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;
}
Also used : JsonArray(com.helger.json.JsonArray) IJsonArray(com.helger.json.IJsonArray) IJsonObject(com.helger.json.IJsonObject) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) IDocumentTypeIdentifier(com.helger.peppolid.IDocumentTypeIdentifier) IJsonArray(com.helger.json.IJsonArray) Map(java.util.Map) Nonnull(javax.annotation.Nonnull)

Example 9 with JsonObject

use of com.helger.json.JsonObject 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;
}
Also used : Element(org.w3c.dom.Element) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) Nullable(javax.annotation.Nullable)

Example 10 with JsonObject

use of com.helger.json.JsonObject in project phoss-smp by phax.

the class SMPStatusProvider method getDefaultStatusData.

@Nonnull
@ReturnsMutableCopy
public static IJsonObject getDefaultStatusData(final boolean bDisableLongRunningOperations) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Building status data");
    final StopWatch aSW = StopWatch.createdStarted();
    final ISMPSettings aSettings = SMPMetaManager.getSettings();
    final LocalDateTime aNow = PDTFactory.getCurrentLocalDateTime();
    final ISMLInfo aSMLInfo = aSettings.getSMLInfo();
    final IJsonObject aStatusData = new JsonObject();
    // Since 5.0.7
    aStatusData.add("build.timestamp", CSMPServer.getBuildTimestamp());
    // Since 5.3.3
    aStatusData.addIfNotNull("startup.datetime", PDTWebDateHelper.getAsStringXSD(SMPWebAppListener.getStartupDateTime()));
    aStatusData.add("status.datetime", PDTWebDateHelper.getAsStringXSD(PDTFactory.getCurrentOffsetDateTimeUTC()));
    aStatusData.add("version.smp", CSMPServer.getVersionNumber());
    aStatusData.add("version.java", SystemProperties.getJavaVersion());
    aStatusData.add("global.debug", GlobalDebug.isDebugMode());
    aStatusData.add("global.production", GlobalDebug.isProductionMode());
    aStatusData.add("smp.backend", SMPServerConfiguration.getBackend());
    aStatusData.add("smp.mode", SMPWebAppConfiguration.isTestVersion() ? "test" : "production");
    aStatusData.add("smp.resttype", SMPServerConfiguration.getRESTType().getID());
    aStatusData.add("smp.identifiertype", SMPServerConfiguration.getIdentifierType().getID());
    aStatusData.add("smp.id", SMPServerConfiguration.getSMLSMPID());
    aStatusData.add("smp.writable-rest-api.enabled", !aSettings.isRESTWritableAPIDisabled());
    // New in 5.1.0
    aStatusData.add("smp.publicurl", SMPServerConfiguration.getPublicServerURL());
    // New in 5.1.0
    aStatusData.add("smp.forceroot", SMPServerConfiguration.isForceRoot());
    // New in 5.2.0
    aStatusData.add("smp.rest.log-exceptions", SMPServerConfiguration.isRESTLogExceptions());
    // New in 5.2.1
    aStatusData.add("smp.rest.payload-on-error", SMPServerConfiguration.isRESTPayloadOnError());
    // SML information
    aStatusData.add("smp.sml.enabled", aSettings.isSMLEnabled());
    aStatusData.add("smp.sml.needed", aSettings.isSMLRequired());
    if (aSMLInfo != null) {
        aStatusData.add("smp.sml.url", aSMLInfo.getManagementServiceURL());
        aStatusData.add("smp.sml.dnszone", aSMLInfo.getDNSZone());
    }
    aStatusData.addIfNotNull("smp.sml.connection-timeout-ms", SMPServerConfiguration.getSMLConnectionTimeoutMS());
    aStatusData.add("smp.sml.request-timeout-ms", SMPServerConfiguration.getSMLRequestTimeoutMS());
    // Directory information
    aStatusData.add("smp.pd.enabled", aSettings.isDirectoryIntegrationEnabled());
    // New in 5.1.0
    aStatusData.add("smp.pd.needed", aSettings.isDirectoryIntegrationRequired());
    aStatusData.add("smp.pd.auto-update", aSettings.isDirectoryIntegrationAutoUpdate());
    aStatusData.add("smp.pd.hostname", aSettings.getDirectoryHostName());
    // Certificate information
    final boolean bCertConfigOk = SMPKeyManager.isKeyStoreValid();
    aStatusData.add("smp.certificate.configuration-valid", bCertConfigOk);
    if (bCertConfigOk) {
        final SMPKeyManager aKeyMgr = SMPKeyManager.getInstance();
        final PrivateKeyEntry aKeyEntry = aKeyMgr.getPrivateKeyEntry();
        if (aKeyEntry != null) {
            final Certificate[] aChain = aKeyEntry.getCertificateChain();
            if (aChain.length > 0 && aChain[0] instanceof X509Certificate) {
                final X509Certificate aX509Cert = (X509Certificate) aChain[0];
                aStatusData.add("smp.certificate.issuer", aX509Cert.getIssuerX500Principal().getName());
                aStatusData.add("smp.certificate.subject", aX509Cert.getSubjectX500Principal().getName());
                final LocalDateTime aNotAfter = PDTFactory.createLocalDateTime(aX509Cert.getNotAfter());
                final boolean bIsExpired = aNow.isAfter(aNotAfter);
                aStatusData.add("smp.certificate.expired", bIsExpired);
            }
        }
    }
    // Proxy configuration (since 5.2.0)
    aStatusData.add("proxy.http.configured", SMPServerConfiguration.getAsHttpProxySettings() != null);
    aStatusData.add("proxy.https.configured", SMPServerConfiguration.getAsHttpsProxySettings() != null);
    aStatusData.add("proxy.username.configured", StringHelper.hasText(SMPServerConfiguration.getProxyUsername()));
    // CSP configuration (since 5.2.6)
    aStatusData.add("csp.enabled", SMPWebAppConfiguration.isCSPEnabled());
    aStatusData.add("csp.reporting.only", SMPWebAppConfiguration.isCSPReportingOnly());
    aStatusData.add("csp.reporting.enabled", SMPWebAppConfiguration.isCSPReportingEnabled());
    // Add SPI data as well
    for (final ISMPStatusProviderExtensionSPI aImpl : LIST) {
        final ICommonsOrderedMap<String, ?> aMap = aImpl.getAdditionalStatusData(bDisableLongRunningOperations);
        aStatusData.addAll(aMap);
    }
    final long nMillis = aSW.stopAndGetMillis();
    if (nMillis > 100)
        LOGGER.info("Finished building status data after " + nMillis + " milliseconds which is considered to be too long");
    else if (LOGGER.isDebugEnabled())
        LOGGER.debug("Finished building status data");
    return aStatusData;
}
Also used : LocalDateTime(java.time.LocalDateTime) ISMLInfo(com.helger.peppol.sml.ISMLInfo) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) X509Certificate(java.security.cert.X509Certificate) StopWatch(com.helger.commons.timing.StopWatch) SMPKeyManager(com.helger.phoss.smp.security.SMPKeyManager) ISMPSettings(com.helger.phoss.smp.settings.ISMPSettings) IJsonObject(com.helger.json.IJsonObject) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Aggregations

JsonObject (com.helger.json.JsonObject)40 IJsonObject (com.helger.json.IJsonObject)38 Nonnull (javax.annotation.Nonnull)27 JsonArray (com.helger.json.JsonArray)16 IJsonArray (com.helger.json.IJsonArray)13 Map (java.util.Map)5 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 ICommonsList (com.helger.commons.collection.impl.ICommonsList)3 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)3 StopWatch (com.helger.commons.timing.StopWatch)3 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)3 ZonedDateTime (java.time.ZonedDateTime)3 Locale (java.util.Locale)3 Nonempty (com.helger.commons.annotation.Nonempty)2 CommonsTreeMap (com.helger.commons.collection.impl.CommonsTreeMap)2 IError (com.helger.commons.error.IError)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 IHCNode (com.helger.html.hc.IHCNode)2 JsonWriter (com.helger.json.serialize.JsonWriter)2