Search in sources :

Example 16 with IJsonArray

use of com.helger.json.IJsonArray in project phive by phax.

the class PhiveJsonHelper method applyGlobalError.

/**
 * Add one global error to the response. Afterwards no validation results
 * should be added. The layout of the response object is very similar to the
 * one created by
 * {@link #applyValidationResultList(IJsonObject, IValidationExecutorSet, List, Locale, long, MutableInt, MutableInt)}.
 * <br>
 *
 * <pre>
 * {
 *   "success" : boolean,
 *   "interrupted" : boolean,
 *   "mostSevereErrorLevel" : string,
 *   "results" : array {
 *     "success" : string,  // as defined by {@link #getJsonTriState(ETriState)}
 *     "artifactType" : string,
 *     "artifactPath" : string,
 *     "items" : array {
 *       error structure as in {@link #getJsonError(IError, Locale)}
 *     }
 *   },
 *   "durationMS" : number
 * }
 * </pre>
 *
 * @param aResponse
 *        The response JSON object to add to. May not be <code>null</code>.
 * @param sErrorMsg
 *        The error message to use. May not be <code>null</code>.
 * @param nDurationMilliseconds
 *        The duration of the validation in milliseconds. Must be &ge; 0.
 */
public static void applyGlobalError(@Nonnull final IJsonObject aResponse, @Nonnull final String sErrorMsg, @Nonnegative final long nDurationMilliseconds) {
    ValueEnforcer.notNull(aResponse, "Response");
    ValueEnforcer.notNull(sErrorMsg, "ErrorMsg");
    ValueEnforcer.isGE0(nDurationMilliseconds, "DurationMilliseconds");
    final IJsonArray aResultArray = new JsonArray();
    {
        aResultArray.add(new JsonObject().add(JSON_SUCCESS, getJsonTriState(false)).add(JSON_ARTIFACT_TYPE, ARTIFACT_TYPE_INPUT_PARAMETER).add(JSON_ARTIFACT_PATH, ARTIFACT_PATH_NONE).addJson(JSON_ITEMS, new JsonArray(jsonErrorBuilder().errorLevel(EErrorLevel.ERROR).errorText(sErrorMsg).build())));
    }
    aResponse.add(JSON_SUCCESS, false);
    aResponse.add(JSON_INTERRUPTED, false);
    aResponse.add(JSON_MOST_SEVERE_ERROR_LEVEL, getJsonErrorLevel(EErrorLevel.ERROR));
    aResponse.addJson(JSON_RESULTS, aResultArray);
    aResponse.add(JSON_DURATION_MS, nDurationMilliseconds);
}
Also used : JsonArray(com.helger.json.JsonArray) IJsonArray(com.helger.json.IJsonArray) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) IJsonArray(com.helger.json.IJsonArray)

Example 17 with IJsonArray

use of com.helger.json.IJsonArray in project phase4 by phax.

the class PModeLegSecurityJsonConverter method convertToNative.

/**
 * Convert the provided JSON to a {@link PModeLegSecurity} object.
 *
 * @param aElement
 *        The JSON object to be converted. May not be <code>null</code>.
 * @return A non-<code>null</code> {@link PModeLegSecurity}
 * @throws IllegalStateException
 *         In case of an unsupported value
 */
@Nonnull
public static PModeLegSecurity convertToNative(@Nonnull final IJsonObject aElement) {
    final String sWSSVersion = aElement.getAsString(ATTR_WSS_VERSION);
    final EWSSVersion eWSSVersion = EWSSVersion.getFromVersionOrNull(sWSSVersion);
    if (eWSSVersion == null && sWSSVersion != null)
        throw new IllegalStateException("Invalid WSS version '" + sWSSVersion + "'");
    final ICommonsList<String> aX509SignElements = new CommonsArrayList<>();
    final IJsonArray aSignElement = aElement.getAsArray(ELEMENT_X509_SIGN_ELEMENT);
    if (aSignElement != null)
        for (final IJsonValue aItem : aSignElement.iteratorValues()) aX509SignElements.add(aItem.getAsString());
    final ICommonsList<String> aX509SignAttachments = new CommonsArrayList<>();
    final IJsonArray aSignAttachment = aElement.getAsArray(ELEMENT_X509_SIGN_ATTACHMENT);
    if (aSignAttachment != null)
        for (final IJsonValue aItem : aSignAttachment.iteratorValues()) aX509SignAttachments.add(aItem.getAsString());
    final String sX509SignatureCertificate = aElement.getAsString(ELEMENT_X509_SIGNATURE_CERTIFICATE);
    final String sX509SignatureHashFunction = aElement.getAsString(ATTR_X509_SIGNATURE_HASH_FUNCTION);
    final ECryptoAlgorithmSignDigest eX509SignatureHashFunction = ECryptoAlgorithmSignDigest.getFromIDOrNull(sX509SignatureHashFunction);
    if (eX509SignatureHashFunction == null && sX509SignatureHashFunction != null)
        throw new IllegalStateException("Invalid signature hash function '" + sX509SignatureHashFunction + "'");
    final String sX509SignatureAlgorithm = aElement.getAsString(ATTR_X509_SIGNATURE_ALGORITHM);
    final ECryptoAlgorithmSign eX509SignatureAlgorithm = ECryptoAlgorithmSign.getFromIDOrNull(sX509SignatureAlgorithm);
    if (eX509SignatureAlgorithm == null && sX509SignatureAlgorithm != null)
        throw new IllegalStateException("Invalid signature algorithm '" + sX509SignatureAlgorithm + "'");
    final ICommonsList<String> aX509EncryptionElements = new CommonsArrayList<>();
    final IJsonArray aEncryptElement = aElement.getAsArray(ELEMENT_X509_ENCRYPTION_ENCRYPT_ELEMENT);
    if (aEncryptElement != null)
        for (final IJsonValue aItem : aEncryptElement.iteratorValues()) aX509EncryptionElements.add(aItem.getAsString());
    final ICommonsList<String> aX509EncryptionAttachments = new CommonsArrayList<>();
    final IJsonArray aEncryptAttachment = aElement.getAsArray(ELEMENT_X509_ENCRYPTION_ENCRYPT_ATTACHMENT);
    if (aEncryptAttachment != null)
        for (final IJsonValue aItem : aEncryptAttachment.iteratorValues()) aX509EncryptionAttachments.add(aItem.getAsString());
    final String sX509EncryptionCertificate = aElement.getAsString(ELEMENT_X509_ENCRYPTION_CERTIFICATE);
    final String sX509EncryptionAlgorithm = aElement.getAsString(ATTR_X509_ENCRYPTION_ALGORITHM);
    final ECryptoAlgorithmCrypt eX509EncryptionAlgorithm = ECryptoAlgorithmCrypt.getFromIDOrNull(sX509EncryptionAlgorithm);
    if (eX509EncryptionAlgorithm == null && sX509EncryptionAlgorithm != null)
        throw new IllegalStateException("Invalid encrypt algorithm '" + sX509EncryptionAlgorithm + "'");
    final Integer aX509EncryptionMinimumStrength = aElement.getAsIntObj(ATTR_X509_ENCRYPTION_MINIMUM_STRENGTH);
    final String sUsernameTokenUsername = aElement.getAsString(ATTR_USERNAME_TOKEN_USERNAME);
    final String sUsernameTokenPassword = aElement.getAsString(ATTR_USERNAME_TOKEN_PASSWORD);
    final ETriState eUsernameTokenDigest = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(ATTR_USERNAME_TOKEN_DIGEST), PModeLegSecurity.DEFAULT_USERNAME_TOKEN_DIGEST);
    final ETriState eUsernameTokenNonce = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(ATTR_USERNAME_TOKEN_NONCE), PModeLegSecurity.DEFAULT_USERNAME_TOKEN_NONCE);
    final ETriState eUsernameTokenCreated = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(ATTR_USERNAME_TOKEN_CREATED), PModeLegSecurity.DEFAULT_USERNAME_TOKEN_CREATED);
    final ETriState ePModeAuthorize = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(ATTR_PMODE_AUTHORIZE), PModeLegSecurity.DEFAULT_PMODE_AUTHORIZE);
    final ETriState eSendReceipt = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(ATTR_SEND_RECEIPT), PModeLegSecurity.DEFAULT_SEND_RECEIPT);
    final String sSendReceiptReplyPattern = aElement.getAsString(ATTR_SEND_RECEIPT_REPLY_PATTERN);
    final EPModeSendReceiptReplyPattern eSendReceiptReplyPattern = EPModeSendReceiptReplyPattern.getFromIDOrNull(sSendReceiptReplyPattern);
    if (eSendReceiptReplyPattern == null && sSendReceiptReplyPattern != null)
        throw new IllegalStateException("Invalid SendReceipt ReplyPattern version '" + sSendReceiptReplyPattern + "'");
    final ETriState eSendReceiptNonRepudiation = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(ATTR_SEND_RECEIPT_NON_REPUDIATION), PModeLegSecurity.DEFAULT_SEND_RECEIPT_NON_REPUDIATION);
    return new PModeLegSecurity(eWSSVersion, aX509SignElements, aX509SignAttachments, sX509SignatureCertificate, eX509SignatureHashFunction, eX509SignatureAlgorithm, aX509EncryptionElements, aX509EncryptionAttachments, sX509EncryptionCertificate, eX509EncryptionAlgorithm, aX509EncryptionMinimumStrength, sUsernameTokenUsername, sUsernameTokenPassword, eUsernameTokenDigest, eUsernameTokenNonce, eUsernameTokenCreated, ePModeAuthorize, eSendReceipt, eSendReceiptReplyPattern, eSendReceiptNonRepudiation);
}
Also used : ETriState(com.helger.commons.state.ETriState) ECryptoAlgorithmCrypt(com.helger.phase4.crypto.ECryptoAlgorithmCrypt) IJsonValue(com.helger.json.IJsonValue) ECryptoAlgorithmSignDigest(com.helger.phase4.crypto.ECryptoAlgorithmSignDigest) IJsonArray(com.helger.json.IJsonArray) EWSSVersion(com.helger.phase4.wss.EWSSVersion) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ECryptoAlgorithmSign(com.helger.phase4.crypto.ECryptoAlgorithmSign) Nonnull(javax.annotation.Nonnull)

Example 18 with IJsonArray

use of com.helger.json.IJsonArray in project phase4 by phax.

the class PModeLegReliabilityJsonConverter method convertToNative.

@Nonnull
public static PModeLegReliability convertToNative(@Nonnull final IJsonObject aElement) {
    final ETriState eAtLeastOnceContract = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(AT_LEAST_ONCE_CONTRACT), PModeLegReliability.DEFAULT_AT_LEAST_ONCE_CONTRACT);
    final ETriState eAtLeastOnceAckOnDelivery = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(AT_LEAST_ONCE_ACK_ON_DELIVERY), PModeLegReliability.DEFAULT_AT_LEAST_ONCE_ACK_ON_DELIVERY);
    final String sAtLeastOnceContractAcksTo = aElement.getAsString(AT_LEAST_ONCE_CONTRACT_ACK_TO);
    final ETriState eAtLeastOnceContractAckResponse = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(AT_LEAST_ONCE_CONTRACT_ACK_RESPONSE), PModeLegReliability.DEFAULT_AT_LEAST_ONCE_CONTRACT_ACK_RESPONSE);
    final String sAtLeastOnceReplyPattern = aElement.getAsString(AT_LEAST_ONCE_REPLY_PATTERN);
    final ETriState eAtMostOnceContract = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(AT_MOST_ONCE_CONTRACT), PModeLegReliability.DEFAULT_AT_MOST_ONCE_CONTRACT);
    final ETriState eInOrderContract = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(IN_ORDER_CONTRACT), PModeLegReliability.DEFAULT_IN_ORDER_CONTACT);
    final ETriState eStartGroup = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(START_GROUP), PModeLegReliability.DEFAULT_START_GROUP);
    final ICommonsList<String> aCorrelationStrings = new CommonsArrayList<>();
    final IJsonArray aCorrelation = aElement.getAsArray(CORRELATION);
    if (aCorrelation != null)
        for (final IJsonValue aCorrelationElement : aCorrelation.iteratorValues()) aCorrelationStrings.add(aCorrelationElement.getAsString());
    final ETriState eTerminateGroup = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(TERMINATE_GROUP), PModeLegReliability.DEFAULT_TERMINATE_GROUP);
    return new PModeLegReliability(eAtLeastOnceContract, eAtLeastOnceAckOnDelivery, sAtLeastOnceContractAcksTo, eAtLeastOnceContractAckResponse, sAtLeastOnceReplyPattern, eAtMostOnceContract, eInOrderContract, eStartGroup, aCorrelationStrings, eTerminateGroup);
}
Also used : IJsonValue(com.helger.json.IJsonValue) ETriState(com.helger.commons.state.ETriState) IJsonArray(com.helger.json.IJsonArray) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nonnull(javax.annotation.Nonnull)

Example 19 with IJsonArray

use of com.helger.json.IJsonArray in project phase4 by phax.

the class PModeLegBusinessInformationJsonConverter method convertToNative.

@Nonnull
public static PModeLegBusinessInformation convertToNative(@Nonnull final IJsonObject aElement) {
    final String sService = aElement.getAsString(SERVICE);
    final String sServiceType = aElement.getAsString(SERVICE_TYPE);
    final String sAction = aElement.getAsString(ACTION);
    final ICommonsOrderedMap<String, PModeProperty> aProperties = new CommonsLinkedHashMap<>();
    final IJsonArray aProps = aElement.getAsArray(PROPERTIES);
    if (aProps != null)
        for (final IJsonObject aPropertyElement : aProps.iteratorObjects()) {
            final PModeProperty aProperty = PModePropertyJsonConverter.convertToNative(aPropertyElement);
            aProperties.put(aProperty.getName(), aProperty);
        }
    final ICommonsOrderedMap<String, PModePayloadProfile> aPayloadProfiles = new CommonsLinkedHashMap<>();
    final IJsonArray aPayloadProfs = aElement.getAsArray(PAYLOAD_PROFILE);
    if (aPayloadProfs != null)
        for (final IJsonObject aPayloadElement : aPayloadProfs.iteratorObjects()) {
            final PModePayloadProfile aPayloadProfile = PModePayloadProfileJsonConverter.convertToNative(aPayloadElement);
            aPayloadProfiles.put(aPayloadProfile.getName(), aPayloadProfile);
        }
    final Long aPayloadProfileMaxKB = aElement.getAsLongObj(PAYLOAD_PROFILE_MAX_KB);
    final String sMPCID = aElement.getAsString(MPCID);
    return new PModeLegBusinessInformation(sService, sServiceType, sAction, aProperties, aPayloadProfiles, aPayloadProfileMaxKB, sMPCID);
}
Also used : CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) IJsonObject(com.helger.json.IJsonObject) IJsonArray(com.helger.json.IJsonArray) Nonnull(javax.annotation.Nonnull)

Example 20 with IJsonArray

use of com.helger.json.IJsonArray 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;
}
Also used : IJsonArray(com.helger.json.IJsonArray) JsonArray(com.helger.json.JsonArray) Cookie(javax.servlet.http.Cookie) IJsonObject(com.helger.json.IJsonObject) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) IJsonArray(com.helger.json.IJsonArray) Nonnull(javax.annotation.Nonnull) Nonempty(com.helger.commons.annotation.Nonempty)

Aggregations

IJsonArray (com.helger.json.IJsonArray)23 IJsonObject (com.helger.json.IJsonObject)20 JsonArray (com.helger.json.JsonArray)15 JsonObject (com.helger.json.JsonObject)15 Nonnull (javax.annotation.Nonnull)15 Map (java.util.Map)7 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)4 IError (com.helger.commons.error.IError)4 ETriState (com.helger.commons.state.ETriState)4 IJson (com.helger.json.IJson)4 CommonsLinkedHashMap (com.helger.commons.collection.impl.CommonsLinkedHashMap)3 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)3 IJsonValue (com.helger.json.IJsonValue)3 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)3 ZonedDateTime (java.time.ZonedDateTime)3 Query (org.apache.lucene.search.Query)3 Nonempty (com.helger.commons.annotation.Nonempty)2 CommonsTreeSet (com.helger.commons.collection.impl.CommonsTreeSet)2 ICommonsList (com.helger.commons.collection.impl.ICommonsList)2 ErrorList (com.helger.commons.error.list.ErrorList)2