use of com.helger.peppol.domain.ISMLConfiguration in project peppol-practical by phax.
the class PageSecureSMLConfiguration method validateAndSaveInputParameters.
@Override
protected void validateAndSaveInputParameters(@Nonnull final WebPageExecutionContext aWPEC, @Nullable final ISMLConfiguration aSelectedObject, @Nonnull final FormErrorList aFormErrors, @Nonnull final EWebPageFormAction eFormAction) {
final boolean bEdit = eFormAction.isEdit();
final ISMLConfigurationManager aSMLConfigurationMgr = PPMetaManager.getSMLConfigurationMgr();
final String sID = aWPEC.params().getAsString(FIELD_ID);
final String sDisplayName = aWPEC.params().getAsString(FIELD_DISPLAY_NAME);
final String sDNSZone = aWPEC.params().getAsString(FIELD_DNS_ZONE);
final String sManagementAddressURL = aWPEC.params().getAsString(FIELD_MANAGEMENT_ADDRESS_URL);
final boolean bClientCertificateRequired = aWPEC.params().isCheckBoxChecked(FIELD_CLIENT_CERTIFICATE_REQUIRED, DEFAULT_CLIENT_CERTIFICATE_REQUIRED);
final String sSMPAPIType = aWPEC.params().getAsString(FIELD_SMP_API_TYPE);
final ESMPAPIType eSMPAPIType = ESMPAPIType.getFromIDOrNull(sSMPAPIType);
final String sSMPIdentifierType = aWPEC.params().getAsString(FIELD_SMP_ID_TYPE);
final ESMPIdentifierType eSMPIdentifierType = ESMPIdentifierType.getFromIDOrNull(sSMPIdentifierType);
final boolean bProduction = aWPEC.params().isCheckBoxChecked(FIELD_PRODUCTION, false);
// validations
if (StringHelper.hasNoText(sID))
aFormErrors.addFieldError(FIELD_ID, "The SML configuration ID must not be empty!");
else if (ISMLConfigurationManager.ID_AUTO_DETECT.equals(sID))
aFormErrors.addFieldError(FIELD_ID, "This SML configuration ID is reserved!");
else {
final ISMLConfiguration aExisting = aSMLConfigurationMgr.getSMLInfoOfID(sID);
if (bEdit) {
// Expect aExistring == aSelectedObject
if (aExisting == null)
aFormErrors.addFieldError(FIELD_ID, "Invalid SML configuration ID provided!");
else if (aExisting != aSelectedObject)
aFormErrors.addFieldError(FIELD_ID, "Another SML configuration with the same ID already exists!");
} else {
if (aExisting != null)
aFormErrors.addFieldError(FIELD_ID, "Another SML configuration with the same ID already exists!");
}
}
if (StringHelper.hasNoText(sDisplayName))
aFormErrors.addFieldError(FIELD_DISPLAY_NAME, "The SML configuration name must not be empty!");
if (StringHelper.hasNoText(sDNSZone))
aFormErrors.addFieldError(FIELD_DNS_ZONE, "The DNS Zone must not be empty!");
if (StringHelper.hasNoText(sManagementAddressURL))
aFormErrors.addFieldError(FIELD_MANAGEMENT_ADDRESS_URL, "The Management Address URL must not be empty!");
else {
final URL aURL = URLHelper.getAsURL(sManagementAddressURL);
if (aURL == null)
aFormErrors.addFieldError(FIELD_MANAGEMENT_ADDRESS_URL, "The Management Address URL is not a valid URL!");
else if (!"https".equals(aURL.getProtocol()) && !"http".equals(aURL.getProtocol()))
aFormErrors.addFieldError(FIELD_MANAGEMENT_ADDRESS_URL, "The Management Address URL should only be use the 'http' or the 'https' protocol!");
}
if (StringHelper.hasNoText(sSMPAPIType))
aFormErrors.addFieldError(FIELD_SMP_API_TYPE, "An SMP API type must be selected!");
else if (eSMPAPIType == null)
aFormErrors.addFieldError(FIELD_SMP_API_TYPE, "A valid SMP API type must be selected!");
if (StringHelper.hasNoText(sSMPIdentifierType))
aFormErrors.addFieldError(FIELD_SMP_ID_TYPE, "An SMP identifier type must be selected!");
else if (eSMPIdentifierType == null)
aFormErrors.addFieldError(FIELD_SMP_ID_TYPE, "A valid SMP identifier type must be selected!");
if (aFormErrors.isEmpty()) {
// Lowercase with the US locale - not display locale specific
final String sDNSZoneLC = sDNSZone.toLowerCase(Locale.US);
if (bEdit) {
aSMLConfigurationMgr.updateSMLInfo(aSelectedObject.getID(), sDisplayName, sDNSZoneLC, sManagementAddressURL, bClientCertificateRequired, eSMPAPIType, eSMPIdentifierType, bProduction);
aWPEC.postRedirectGetInternal(success("The SML configuration '" + sDisplayName + "' was successfully edited."));
} else {
aSMLConfigurationMgr.createSMLInfo(sID, sDisplayName, sDNSZoneLC, sManagementAddressURL, bClientCertificateRequired, eSMPAPIType, eSMPIdentifierType, bProduction);
aWPEC.postRedirectGetInternal(success("The new SML configuration '" + sDisplayName + "' was successfully created."));
}
}
}
use of com.helger.peppol.domain.ISMLConfiguration in project peppol-practical by phax.
the class TestEndpointMicroTypeConverter method convertToNative.
@Nonnull
public TestEndpoint convertToNative(@Nonnull final IMicroElement eValue) {
final StubObject aStubObject = getStubObject(eValue);
final String sCompanyName = eValue.getAttributeValue(ATTR_COMPANY_NAME);
final String sContactPerson = eValue.getAttributeValue(ATTR_CONTACT_PERSON);
final String sParticipantIDScheme = eValue.getAttributeValue(ATTR_PARTICIPANT_ID_ISSUER);
final String sParticipantIDValue = eValue.getAttributeValue(ATTR_PARTICIPANT_ID_VALUE);
final String sTransportProfile = eValue.getAttributeValue(ATTR_TRANSPORT_PROFILE);
final ESMPTransportProfile eTransportProfile = ESMPTransportProfile.getFromIDOrNull(sTransportProfile);
final String sSMLID = eValue.getAttributeValue(ATTR_SML);
final ISMLConfiguration aSMLInfo = PPMetaManager.getSMLConfigurationMgr().getSMLInfoOfID(sSMLID);
if (aSMLInfo == null)
throw new IllegalStateException("Failed to resolve SML with ID '" + sSMLID + "'");
// Create object
return new TestEndpoint(aStubObject, sCompanyName, sContactPerson, sParticipantIDScheme, sParticipantIDValue, eTransportProfile, aSMLInfo);
}
use of com.helger.peppol.domain.ISMLConfiguration in project peppol-practical by phax.
the class APISMPQueryGetDocTypes method rateLimitedInvokeAPI.
@Override
protected void rateLimitedInvokeAPI(@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 ISMLConfigurationManager aSMLConfigurationMgr = PPMetaManager.getSMLConfigurationMgr();
final String sSMLID = aPathVariables.get(PPAPI.PARAM_SML_ID);
final boolean bSMLAutoDetect = ISMLConfigurationManager.ID_AUTO_DETECT.equals(sSMLID);
ISMLConfiguration aSML = aSMLConfigurationMgr.getSMLInfoOfID(sSMLID);
if (aSML == null && !bSMLAutoDetect)
throw new APIParamException("Unsupported SML ID '" + sSMLID + "' provided.");
final String sParticipantID = aPathVariables.get(PPAPI.PARAM_PARTICIPANT_ID);
final IParticipantIdentifier aPID = SimpleIdentifierFactory.INSTANCE.parseParticipantIdentifier(sParticipantID);
if (aPID == null)
throw new APIParamException("Invalid participant ID '" + sParticipantID + "' provided.");
final boolean bQueryBusinessCard = aRequestScope.params().getAsBoolean("businessCard", false);
final boolean bXMLSchemaValidation = aRequestScope.params().getAsBoolean("xmlSchemaValidation", true);
final boolean bVerifySignature = aRequestScope.params().getAsBoolean("verifySignature", true);
final ZonedDateTime aQueryDT = PDTFactory.getCurrentZonedDateTimeUTC();
final StopWatch aSW = StopWatch.createdStarted();
SMPQueryParams aQueryParams = null;
if (bSMLAutoDetect) {
for (final ISMLConfiguration aCurSML : aSMLConfigurationMgr.getAllSorted()) {
aQueryParams = SMPQueryParams.createForSML(aCurSML, aPID.getScheme(), aPID.getValue(), false);
if (aQueryParams == null)
continue;
try {
InetAddress.getByName(aQueryParams.getSMPHostURI().getHost());
// Found it
aSML = aCurSML;
break;
} catch (final UnknownHostException ex) {
// continue
}
}
// Ensure to go into the exception handler
if (aSML == null)
throw new HttpResponseException(CHttp.HTTP_NOT_FOUND, "The participant identifier '" + sParticipantID + "' could not be found in any SML.");
} else {
aQueryParams = SMPQueryParams.createForSML(aSML, aPID.getScheme(), aPID.getValue(), true);
}
if (aQueryParams == null)
throw new APIParamException("Failed to resolve participant ID '" + sParticipantID + "' for the provided SML '" + aSML.getID() + "'");
final IParticipantIdentifier aParticipantID = aQueryParams.getParticipantID();
LOGGER.info("[API] Document types of '" + aParticipantID.getURIEncoded() + "' are queried using SMP API '" + aQueryParams.getSMPAPIType() + "' from '" + aQueryParams.getSMPHostURI() + "' using SML '" + aSML.getID() + "'; XSD validation=" + bXMLSchemaValidation + "; signature verification=" + bVerifySignature);
ICommonsSortedMap<String, String> aSGHrefs = null;
switch(aQueryParams.getSMPAPIType()) {
case PEPPOL:
{
final SMPClientReadOnly aSMPClient = new SMPClientReadOnly(aQueryParams.getSMPHostURI());
aSMPClient.setXMLSchemaValidation(bXMLSchemaValidation);
aSMPClient.setVerifySignature(bVerifySignature);
// 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("[API] 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);
aBDXR1Client.setVerifySignature(bVerifySignature);
// 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("[API] The ServiceGroup list contains the duplicate URL '" + sHref + "'");
}
}
break;
}
}
IJsonObject aJson = null;
if (aSGHrefs != null)
aJson = SMPJsonResponseExt.convert(aQueryParams.getSMPAPIType(), aParticipantID, aSGHrefs, aQueryParams.getIF());
if (bQueryBusinessCard) {
final String sBCURL = aQueryParams.getSMPHostURI().toString() + "/businesscard/" + aParticipantID.getURIEncoded();
LOGGER.info("[API] 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("[API] No Business Card is available for that participant.");
else {
final PDBusinessCard aBC = PDBusinessCardHelper.parseBusinessCard(aData, StandardCharsets.UTF_8);
if (aBC == null) {
LOGGER.error("[API] Failed to parse BC:\n" + new String(aData));
} else {
// Business Card found
if (aJson == null)
aJson = new JsonObject();
aJson.addJson("businessCard", aBC.getAsJson());
}
}
}
aSW.stop();
if (aJson == null) {
LOGGER.error("[API] Failed to perform the SMP lookup");
aUnifiedResponse.setStatus(CHttp.HTTP_NOT_FOUND);
} else {
LOGGER.info("[API] 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(3 * CGlobal.SECONDS_PER_HOUR);
}
}
use of com.helger.peppol.domain.ISMLConfiguration in project peppol-practical by phax.
the class APISMPQueryGetServiceInformation method rateLimitedInvokeAPI.
@Override
protected void rateLimitedInvokeAPI(@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 ISMLConfigurationManager aSMLConfigurationMgr = PPMetaManager.getSMLConfigurationMgr();
final String sSMLID = aPathVariables.get(PPAPI.PARAM_SML_ID);
final boolean bSMLAutoDetect = ISMLConfigurationManager.ID_AUTO_DETECT.equals(sSMLID);
ISMLConfiguration aSML = aSMLConfigurationMgr.getSMLInfoOfID(sSMLID);
if (aSML == null && !bSMLAutoDetect)
throw new APIParamException("Unsupported SML ID '" + sSMLID + "' provided.");
final String sParticipantID = aPathVariables.get(PPAPI.PARAM_PARTICIPANT_ID);
final IParticipantIdentifier aPID = SimpleIdentifierFactory.INSTANCE.parseParticipantIdentifier(sParticipantID);
if (aPID == null)
throw new APIParamException("Invalid participant ID '" + sParticipantID + "' provided.");
final String sDocTypeID = aPathVariables.get(PPAPI.PARAM_DOCTYPE_ID);
final IDocumentTypeIdentifier aDTID = SimpleIdentifierFactory.INSTANCE.parseDocumentTypeIdentifier(sDocTypeID);
if (aDTID == null)
throw new APIParamException("Invalid document type ID '" + sDocTypeID + "' provided.");
final boolean bXMLSchemaValidation = aRequestScope.params().getAsBoolean("xmlSchemaValidation", true);
final boolean bVerifySignature = aRequestScope.params().getAsBoolean("verifySignature", true);
final ZonedDateTime aQueryDT = PDTFactory.getCurrentZonedDateTimeUTC();
final StopWatch aSW = StopWatch.createdStarted();
SMPQueryParams aQueryParams = null;
if (bSMLAutoDetect) {
for (final ISMLConfiguration aCurSML : aSMLConfigurationMgr.getAllSorted()) {
aQueryParams = SMPQueryParams.createForSML(aCurSML, aPID.getScheme(), aPID.getValue(), false);
if (aQueryParams == null)
continue;
try {
InetAddress.getByName(aQueryParams.getSMPHostURI().getHost());
// Found it
aSML = aCurSML;
break;
} catch (final UnknownHostException ex) {
// continue
}
}
// Ensure to go into the exception handler
if (aSML == null)
throw new HttpResponseException(CHttp.HTTP_NOT_FOUND, "The participant identifier '" + sParticipantID + "' could not be found in any SML.");
} else {
aQueryParams = SMPQueryParams.createForSML(aSML, aPID.getScheme(), aPID.getValue(), true);
}
if (aQueryParams == null)
throw new APIParamException("Failed to resolve participant ID '" + sParticipantID + "' for the provided SML '" + aSML.getID() + "'");
final IParticipantIdentifier aParticipantID = aQueryParams.getParticipantID();
final IDocumentTypeIdentifier aDocTypeID = aQueryParams.getIF().createDocumentTypeIdentifier(aDTID.getScheme(), aDTID.getValue());
if (aDocTypeID == null)
throw new APIParamException("Invalid document type ID '" + sDocTypeID + "' provided.");
LOGGER.info("[API] Participant information of '" + aParticipantID.getURIEncoded() + "' is queried using SMP API '" + aQueryParams.getSMPAPIType() + "' from '" + aQueryParams.getSMPHostURI() + "' using SML '" + aSML.getID() + "' for document type '" + aDocTypeID.getURIEncoded() + "'; XSD validation=" + bXMLSchemaValidation + "; signature verification=" + bVerifySignature);
IJsonObject aJson = null;
switch(aQueryParams.getSMPAPIType()) {
case PEPPOL:
{
final SMPClientReadOnly aSMPClient = new SMPClientReadOnly(aQueryParams.getSMPHostURI());
aSMPClient.setXMLSchemaValidation(bXMLSchemaValidation);
aSMPClient.setVerifySignature(bVerifySignature);
final com.helger.xsds.peppol.smp1.SignedServiceMetadataType aSSM = aSMPClient.getServiceMetadataOrNull(aParticipantID, aDocTypeID);
if (aSSM != null) {
final com.helger.xsds.peppol.smp1.ServiceMetadataType aSM = aSSM.getServiceMetadata();
aJson = SMPJsonResponse.convert(aParticipantID, aDocTypeID, aSM);
}
break;
}
case OASIS_BDXR_V1:
{
final BDXRClientReadOnly aBDXR1Client = new BDXRClientReadOnly(aQueryParams.getSMPHostURI());
aBDXR1Client.setXMLSchemaValidation(bXMLSchemaValidation);
aBDXR1Client.setVerifySignature(bVerifySignature);
final com.helger.xsds.bdxr.smp1.SignedServiceMetadataType aSSM = aBDXR1Client.getServiceMetadataOrNull(aParticipantID, aDocTypeID);
if (aSSM != null) {
final com.helger.xsds.bdxr.smp1.ServiceMetadataType aSM = aSSM.getServiceMetadata();
aJson = SMPJsonResponse.convert(aParticipantID, aDocTypeID, aSM);
}
break;
}
}
aSW.stop();
if (aJson == null) {
LOGGER.error("[API] Failed to perform the SMP lookup");
aUnifiedResponse.setStatus(CHttp.HTTP_NOT_FOUND);
} else {
LOGGER.info("[API] 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(3 * CGlobal.SECONDS_PER_HOUR);
}
}
Aggregations