use of com.helger.json.IJsonArray in project phase4 by phax.
the class PModeLegErrorHandlingJsonConverter method convertToNative.
@Nonnull
public static PModeLegErrorHandling convertToNative(@Nonnull final IJsonObject aElement) {
final IJsonArray aSender = aElement.getAsArray(REPORT_SENDER_ERRORS_TO);
final PModeAddressList aSenderAddresses = aSender == null ? null : PModeAddressListJsonConverter.convertToNative(aSender);
final IJsonArray aReceiver = aElement.getAsArray(REPORT_RECEIVER_ERRORS_TO);
final PModeAddressList aReceiverAddresses = aReceiver == null ? null : PModeAddressListJsonConverter.convertToNative(aReceiver);
final ETriState eReportAsResponse = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(REPORT_AS_RESPONSE), PModeLegSecurity.DEFAULT_PMODE_AUTHORIZE);
final ETriState eReportProcessErrorNotifyConsumer = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(REPORT_PROCESS_ERROR_NOTFIY_CONSUMER), PModeLegSecurity.DEFAULT_USERNAME_TOKEN_CREATED);
final ETriState eReportProcessErrorNotifyProducer = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(REPORT_PROCESS_ERROR_NOTFIY_PRODUCER), PModeLegSecurity.DEFAULT_USERNAME_TOKEN_DIGEST);
final ETriState eReportDeliveryFailuresNotifyProducer = AbstractPModeMicroTypeConverter.getTriState(aElement.getAsString(REPORT_DELIVERY_FAILURE_NOTFIY_PRODUCER), PModeLegSecurity.DEFAULT_SEND_RECEIPT);
return new PModeLegErrorHandling(aSenderAddresses, aReceiverAddresses, eReportAsResponse, eReportProcessErrorNotifyConsumer, eReportProcessErrorNotifyProducer, eReportDeliveryFailuresNotifyProducer);
}
use of com.helger.json.IJsonArray in project phase4 by phax.
the class PModeJsonConverter method setObjectFields.
public static void setObjectFields(@Nonnull final IBusinessObject aValue, @Nonnull final IJsonObject aElement) {
aElement.add(ATTR_ID, aValue.getID());
if (aValue.hasCreationDateTime())
aElement.add(ATTR_CREATIONLDT, PDTWebDateHelper.getAsStringXSD(aValue.getCreationDateTime()));
if (aValue.hasCreationUserID())
aElement.add(ATTR_CREATIONUSERID, aValue.getCreationUserID());
if (aValue.hasLastModificationDateTime())
aElement.add(ATTR_LASTMODLDT, PDTWebDateHelper.getAsStringXSD(aValue.getLastModificationDateTime()));
if (aValue.hasLastModificationUserID())
aElement.add(ATTR_LASTMODUSERID, aValue.getLastModificationUserID());
if (aValue.hasDeletionDateTime())
aElement.add(ATTR_DELETIONLDT, PDTWebDateHelper.getAsStringXSD(aValue.getDeletionDateTime()));
if (aValue.hasDeletionUserID())
aElement.add(ATTR_DELETIONUSERID, aValue.getDeletionUserID());
if (aValue.attrs().isNotEmpty()) {
final IJsonArray aCustomArray = new JsonArray();
for (final Map.Entry<String, String> aEntry : CollectionHelper.getSortedByKey(aValue.attrs()).entrySet()) {
final IJsonObject eCustom = new JsonObject();
eCustom.add(ATTR_ID, aEntry.getKey());
if (aEntry.getValue() != null)
eCustom.add(VALUE, aEntry.getValue());
aCustomArray.add(eCustom);
}
aElement.addJson(ELEMENT_CUSTOM, aCustomArray);
}
}
use of com.helger.json.IJsonArray in project phase4 by phax.
the class PModeJsonConverter method getStubObject.
@Nonnull
public static StubObject getStubObject(@Nonnull final IJsonObject aElement) {
// ID
final String sID = aElement.getAsString(ATTR_ID);
// Creation
final LocalDateTime aCreationLDT = PDTWebDateHelper.getLocalDateTimeFromXSD(aElement.getAsString(ATTR_CREATIONLDT));
final String sCreationUserID = aElement.getAsString(ATTR_CREATIONUSERID);
// Last modification
final LocalDateTime aLastModificationLDT = PDTWebDateHelper.getLocalDateTimeFromXSD(aElement.getAsString(ATTR_LASTMODLDT));
final String sLastModificationUserID = aElement.getAsString(ATTR_LASTMODUSERID);
// Deletion
final LocalDateTime aDeletionLDT = PDTWebDateHelper.getLocalDateTimeFromXSD(aElement.getAsString(ATTR_DELETIONLDT));
final String sDeletionUserID = aElement.getAsString(ATTR_DELETIONUSERID);
final ICommonsOrderedMap<String, String> aCustomAttrs = new CommonsLinkedHashMap<>();
final IJsonArray aCustom = aElement.getAsArray(ELEMENT_CUSTOM);
if (aCustom != null)
for (final IJsonObject eCustom : aCustom.iteratorObjects()) aCustomAttrs.put(eCustom.getAsString(ATTR_ID), eCustom.getAsString(VALUE));
return new StubObject(sID, aCreationLDT, sCreationUserID, aLastModificationLDT, sLastModificationUserID, aDeletionLDT, sDeletionUserID, aCustomAttrs);
}
use of com.helger.json.IJsonArray in project phoss-directory by phax.
the class PublicSearchXServletHandler method handleRequest.
public void handleRequest(@Nonnull final IRequestWebScopeWithoutResponse aRequestScope, @Nonnull final UnifiedResponse aUnifiedResponse) throws Exception {
if (SearchRateLimit.INSTANCE.rateLimiter() != null) {
final String sRateLimitKey = "ip:" + aRequestScope.getRemoteAddr();
final boolean bOverLimit = SearchRateLimit.INSTANCE.rateLimiter().overLimitWhenIncremented(sRateLimitKey);
if (bOverLimit) {
// Too Many Requests
if (LOGGER.isDebugEnabled())
LOGGER.debug("REST search rate limit exceeded for " + sRateLimitKey);
aUnifiedResponse.setStatus(CHttp.HTTP_TOO_MANY_REQUESTS);
return;
}
}
final IRequestParamContainer aParams = aRequestScope.params();
// http://127.0.0.1:8080/search -> null
// http://127.0.0.1:8080/search/ -> "/"
// http://127.0.0.1:8080/search/x -> "/x"
final String sPathInfo = StringHelper.getNotNull(aRequestScope.getPathInfo(), "");
final ESearchVersion eSearchVersion = ESearchVersion.getFromPathInfoOrNull(sPathInfo);
if (eSearchVersion == ESearchVersion.V1) {
// Version 1.0
// Determine output format
final ICommonsList<String> aParts = StringHelper.getExploded('/', sPathInfo.substring(1));
final String sFormat = aParts.getAtIndex(1);
final EPDOutputFormat eOutputFormat = EPDOutputFormat.getFromIDCaseInsensitiveOrDefault(sFormat, EPDOutputFormat.XML);
if (LOGGER.isDebugEnabled())
LOGGER.debug("Using REST query API 1.0 with output format " + eOutputFormat + " (" + sPathInfo + ") from '" + aRequestScope.getUserAgent().getAsString() + "'");
// Determine result offset and count
final int nResultPageIndex = aParams.getAsInt(PARAM_RESULT_PAGE_INDEX, aParams.getAsInt("rpi", DEFAULT_RESULT_PAGE_INDEX));
if (nResultPageIndex < 0) {
LOGGER.error("ResultPageIndex " + nResultPageIndex + " is invalid. It must be >= 0.");
aUnifiedResponse.setStatus(CHttp.HTTP_BAD_REQUEST);
return;
}
final int nResultPageCount = aParams.getAsInt(PARAM_RESULT_PAGE_COUNT, aParams.getAsInt("rpc", DEFAULT_RESULT_PAGE_COUNT));
if (nResultPageCount <= 0) {
LOGGER.error("ResultPageCount " + nResultPageCount + " is invalid. It must be > 0.");
aUnifiedResponse.setStatus(CHttp.HTTP_BAD_REQUEST);
return;
}
final int nFirstResultIndex = nResultPageIndex * nResultPageCount;
final int nLastResultIndex = (nResultPageIndex + 1) * nResultPageCount - 1;
if (nFirstResultIndex > MAX_RESULTS) {
LOGGER.error("The first result index " + nFirstResultIndex + " is invalid. It must be <= " + MAX_RESULTS + ".");
aUnifiedResponse.setStatus(CHttp.HTTP_BAD_REQUEST);
return;
}
if (nLastResultIndex > MAX_RESULTS) {
LOGGER.error("The last result index " + nLastResultIndex + " is invalid. It must be <= " + MAX_RESULTS + ".");
aUnifiedResponse.setStatus(CHttp.HTTP_BAD_REQUEST);
return;
}
// Format output?
final boolean bBeautify = aParams.getAsBoolean(PARAM_BEAUTIFY, false);
// Determine query terms
final StringBuilder aSBQueryString = new StringBuilder();
final ICommonsMap<EPDSearchField, ICommonsList<String>> aQueryValues = new CommonsHashMap<>();
for (final EPDSearchField eSF : EPDSearchField.values()) {
final String sFieldName = eSF.getFieldName();
// Check if one or more request parameters are present for the current
// search field
final ICommonsList<String> aValues = aParams.getAsStringList(sFieldName);
if (aValues != null && aValues.isNotEmpty()) {
aQueryValues.put(eSF, aValues);
for (final String sValue : aValues) {
if (aSBQueryString.length() > 0)
aSBQueryString.append('&');
aSBQueryString.append(sFieldName).append('=').append(sValue);
}
}
}
if (aQueryValues.isEmpty()) {
LOGGER.error("No valid query term provided!");
aUnifiedResponse.setStatus(CHttp.HTTP_BAD_REQUEST);
return;
}
if (LOGGER.isDebugEnabled())
LOGGER.debug("Using the following query terms: " + aQueryValues);
final ICommonsList<Query> aQueries = new CommonsArrayList<>();
for (final Map.Entry<EPDSearchField, ICommonsList<String>> aEntry : aQueryValues.entrySet()) {
final EPDSearchField eField = aEntry.getKey();
for (final String sQuery : aEntry.getValue()) {
final Query aQuery = eField.getQuery(sQuery);
if (aQuery != null)
aQueries.add(aQuery);
else
LOGGER.error("Failed to create query '" + sQuery + "' of field " + eField + " - ignoring term!");
}
}
if (aQueries.isEmpty()) {
LOGGER.error("No valid queries could be created!");
aUnifiedResponse.setStatus(CHttp.HTTP_BAD_REQUEST);
return;
}
// Build final query term
Query aLuceneQuery;
if (aQueries.size() == 1) {
aLuceneQuery = aQueries.getFirst();
} else {
// Connect all with "AND"
final BooleanQuery.Builder aBuilder = new BooleanQuery.Builder();
for (final Query aQuery : aQueries) aBuilder.add(aQuery, Occur.MUST);
aLuceneQuery = aBuilder.build();
}
// Only-non deleted
aLuceneQuery = PDQueryManager.andNotDeleted(aLuceneQuery);
// How many results to deliver at most
final int nMaxResults = nLastResultIndex + 1;
// Search all documents
final PDStorageManager aStorageMgr = PDMetaManager.getStorageMgr();
final ICommonsList<PDStoredBusinessEntity> aResultDocs = aStorageMgr.getAllDocuments(aLuceneQuery, nMaxResults);
// Also get the total hit count for UI display. May be < 0 in case of
// error
final int nTotalBEs = aStorageMgr.getCount(aLuceneQuery);
if (LOGGER.isDebugEnabled())
LOGGER.debug(" Result for <" + aLuceneQuery + "> (max=" + nMaxResults + ") " + (nTotalBEs == 1 ? "is 1 document" : "are " + nTotalBEs + " documents"));
// Filter by index/count
final int nEffectiveLastIndex = Math.min(nLastResultIndex, aResultDocs.size() - 1);
final List<PDStoredBusinessEntity> aResultView = nFirstResultIndex >= aResultDocs.size() ? Collections.emptyList() : aResultDocs.subList(nFirstResultIndex, nEffectiveLastIndex + 1);
// Group results by participant ID
final ICommonsMap<IParticipantIdentifier, ICommonsList<PDStoredBusinessEntity>> aGroupedDocs = PDStorageManager.getGroupedByParticipantID(aResultView);
final ZonedDateTime aNow = PDTFactory.getCurrentZonedDateTimeUTC();
// build result
switch(eOutputFormat) {
case XML:
{
final XMLWriterSettings aXWS = new XMLWriterSettings().setIndent(bBeautify ? EXMLSerializeIndent.INDENT_AND_ALIGN : EXMLSerializeIndent.NONE);
final IMicroDocument aDoc = new MicroDocument();
final IMicroElement eRoot = aDoc.appendElement("resultlist");
eRoot.setAttribute(RESPONSE_VERSION, eSearchVersion.getVersion());
eRoot.setAttribute(RESPONSE_TOTAL_RESULT_COUNT, nTotalBEs);
eRoot.setAttribute(RESPONSE_USED_RESULT_COUNT, aResultView.size());
eRoot.setAttribute(RESPONSE_RESULT_PAGE_INDEX, nResultPageIndex);
eRoot.setAttribute(RESPONSE_RESULT_PAGE_COUNT, nResultPageCount);
eRoot.setAttribute(RESPONSE_FIRST_RESULT_INDEX, nFirstResultIndex);
eRoot.setAttribute(RESPONSE_LAST_RESULT_INDEX, nEffectiveLastIndex);
eRoot.setAttribute(RESPONSE_QUERY_TERMS, aSBQueryString.toString());
eRoot.setAttribute(RESPONSE_CREATION_DT, PDTWebDateHelper.getAsStringXSD(aNow));
for (final ICommonsList<PDStoredBusinessEntity> aPerParticipant : aGroupedDocs.values()) {
final IMicroElement eItem = PDStoredBusinessEntity.getAsSearchResultMicroElement(aPerParticipant);
eRoot.appendChild(eItem);
}
if (false) {
// Demo validation
final CollectingSAXErrorHandler aErrHdl = new CollectingSAXErrorHandler();
final Validator v = new XMLSchemaCache(aErrHdl).getValidator(new ClassPathResource("/schema/directory-search-result-list-v1.xsd"));
v.validate(TransformSourceFactory.create(MicroWriter.getNodeAsBytes(aDoc, aXWS)));
for (final IError aError : aErrHdl.getErrorList()) LOGGER.error(aError.getAsString(AppCommonUI.DEFAULT_LOCALE));
}
aUnifiedResponse.disableCaching();
aUnifiedResponse.setMimeType(eOutputFormat.getMimeType());
aUnifiedResponse.setContent(MicroWriter.getNodeAsBytes(aDoc, aXWS));
break;
}
case JSON:
final JsonWriterSettings aJWS = new JsonWriterSettings().setIndentEnabled(bBeautify);
final IJsonObject aDoc = new JsonObject();
aDoc.add(RESPONSE_VERSION, eSearchVersion.getVersion());
aDoc.add(RESPONSE_TOTAL_RESULT_COUNT, nTotalBEs);
aDoc.add(RESPONSE_USED_RESULT_COUNT, aResultView.size());
aDoc.add(RESPONSE_RESULT_PAGE_INDEX, nResultPageIndex);
aDoc.add(RESPONSE_RESULT_PAGE_COUNT, nResultPageCount);
aDoc.add(RESPONSE_FIRST_RESULT_INDEX, nFirstResultIndex);
aDoc.add(RESPONSE_LAST_RESULT_INDEX, nEffectiveLastIndex);
aDoc.add(RESPONSE_QUERY_TERMS, aSBQueryString.toString());
aDoc.add(RESPONSE_CREATION_DT, PDTWebDateHelper.getAsStringXSD(aNow));
final IJsonArray aMatches = new JsonArray();
for (final ICommonsList<PDStoredBusinessEntity> aPerParticipant : aGroupedDocs.values()) {
final IJsonObject aItem = PDStoredBusinessEntity.getAsSearchResultJsonObject(aPerParticipant);
aMatches.add(aItem);
}
aDoc.addJson("matches", aMatches);
aUnifiedResponse.disableCaching();
aUnifiedResponse.setMimeType(eOutputFormat.getMimeType());
aUnifiedResponse.setContentAndCharset(aDoc.getAsJsonString(aJWS), StandardCharsets.UTF_8);
break;
default:
throw new IllegalStateException("Unsupported output format: " + eOutputFormat);
}
} else {
LOGGER.error("Unsupported version provided (" + sPathInfo + ")");
aUnifiedResponse.setStatus(CHttp.HTTP_NOT_FOUND);
}
}
use of com.helger.json.IJsonArray in project phoss-directory by phax.
the class PDStoredBusinessEntity method getAsSearchResultJsonObject.
@Nonnull
public static IJsonObject getAsSearchResultJsonObject(@Nonnull @Nonempty final ICommonsList<PDStoredBusinessEntity> aDocs) {
ValueEnforcer.notEmptyNoNullValue(aDocs, "Docs");
final PDStoredBusinessEntity aFirst = aDocs.getFirst();
final IJsonObject ret = new JsonObject();
ret.addJson("participantID", _getIDAsJson(aFirst.m_aParticipantID.getScheme(), aFirst.m_aParticipantID.getValue()));
// Add the items retrieved from SMP as well
final IJsonArray aDocTypes = new JsonArray();
for (final IDocumentTypeIdentifier aDocTypeID : aFirst.m_aDocumentTypeIDs) aDocTypes.add(_getIDAsJson(aDocTypeID.getScheme(), aDocTypeID.getValue()));
if (aDocTypes.isNotEmpty())
ret.addJson("docTypes", aDocTypes);
final IJsonArray aEntities = new JsonArray();
for (final PDStoredBusinessEntity aDoc : aDocs) {
final IJsonObject aEntity = new JsonObject();
// Multilingual names
final IJsonArray aMLNames = new JsonArray();
for (final PDStoredMLName aName : aDoc.m_aNames) aMLNames.add(_getMLNameAsJson(aName.getName(), aName.getLanguageCode()));
if (aMLNames.isNotEmpty())
aEntity.addJson("name", aMLNames);
aEntity.add("countryCode", aDoc.m_sCountryCode);
if (StringHelper.hasText(aDoc.m_sGeoInfo))
aEntity.add("geoInfo", aDoc.m_sGeoInfo);
final IJsonArray aIDs = new JsonArray();
for (final PDStoredIdentifier aID : aDoc.m_aIdentifiers) aIDs.add(_getIDAsJson(aID.getScheme(), aID.getValue()));
if (aIDs.isNotEmpty())
aEntity.addJson("identifiers", aIDs);
final IJsonArray aWebsites = new JsonArray();
for (final String sWebsite : aDoc.m_aWebsiteURIs) aWebsites.add(sWebsite);
if (aWebsites.isNotEmpty())
aEntity.addJson("websites", aWebsites);
final IJsonArray aContacts = new JsonArray();
for (final PDStoredContact aContact : aDoc.m_aContacts) aContacts.add(new JsonObject().addIfNotNull("type", aContact.getType()).addIfNotNull("name", aContact.getName()).addIfNotNull("phone", aContact.getPhone()).addIfNotNull("email", aContact.getEmail()));
if (aContacts.isNotEmpty())
aEntity.addJson("contacts", aContacts);
if (StringHelper.hasText(aDoc.m_sAdditionalInformation))
aEntity.add("additionalInfo", aDoc.m_sAdditionalInformation);
if (aDoc.m_aRegistrationDate != null)
aEntity.add("regDate", PDTWebDateHelper.getAsStringXSD(aDoc.m_aRegistrationDate));
aEntities.add(aEntity);
}
ret.addJson("entities", aEntities);
return ret;
}
Aggregations