use of com.helger.phoss.smp.domain.serviceinfo.SMPEndpoint in project phoss-smp by phax.
the class BDXR1ServerAPI method saveServiceRegistration.
@Nonnull
public ESuccess saveServiceRegistration(@Nonnull final String sPathServiceGroupID, @Nonnull final String sPathDocumentTypeID, @Nonnull final ServiceMetadataType aServiceMetadata, @Nonnull final BasicAuthClientCredentials aCredentials) throws SMPServerException {
final String sLog = LOG_PREFIX + "PUT /" + sPathServiceGroupID + "/services/" + sPathDocumentTypeID;
final String sAction = "saveServiceRegistration";
if (LOGGER.isInfoEnabled())
LOGGER.info(sLog + " ==> " + aServiceMetadata);
STATS_COUNTER_INVOCATION.increment(sAction);
try {
// Parse provided identifiers
final IIdentifierFactory aIdentifierFactory = SMPMetaManager.getIdentifierFactory();
final IParticipantIdentifier aPathServiceGroupID = aIdentifierFactory.parseParticipantIdentifier(sPathServiceGroupID);
if (aPathServiceGroupID == null) {
// Invalid identifier
throw SMPBadRequestException.failedToParseSG(sPathServiceGroupID, m_aAPIDataProvider.getCurrentURI());
}
final IDocumentTypeIdentifier aPathDocTypeID = aIdentifierFactory.parseDocumentTypeIdentifier(sPathDocumentTypeID);
if (aPathDocTypeID == null) {
// Invalid identifier
throw SMPBadRequestException.failedToParseDocType(sPathDocumentTypeID, m_aAPIDataProvider.getCurrentURI());
}
// May be null for a Redirect!
final ServiceInformationType aServiceInformation = aServiceMetadata.getServiceInformation();
if (aServiceInformation != null) {
// metadata (body) must equal path
if (aServiceInformation.getParticipantIdentifier() == null) {
throw new SMPBadRequestException("Save Service Metadata has inconsistent values.\n" + "Service Information Participant ID: <none>\n" + "URL Parameter value: '" + aPathServiceGroupID.getURIEncoded() + "'", m_aAPIDataProvider.getCurrentURI());
}
final IParticipantIdentifier aPayloadServiceGroupID;
if (aServiceInformation.getParticipantIdentifier() == null) {
// Can happen when tampering with the input data
aPayloadServiceGroupID = null;
} else {
aPayloadServiceGroupID = aIdentifierFactory.createParticipantIdentifier(aServiceInformation.getParticipantIdentifier().getScheme(), aServiceInformation.getParticipantIdentifier().getValue());
}
if (!aPathServiceGroupID.hasSameContent(aPayloadServiceGroupID)) {
// Participant ID in URL must match the one in XML structure
throw new SMPBadRequestException("Save Service Metadata was called with inconsistent values.\n" + "Service Infoformation Participant ID: " + (aPayloadServiceGroupID == null ? "<none>" : "'" + aPayloadServiceGroupID.getURIEncoded() + "'") + "\n" + "URL parameter value: '" + aPathServiceGroupID.getURIEncoded() + "'", m_aAPIDataProvider.getCurrentURI());
}
if (aServiceInformation.getDocumentIdentifier() == null) {
throw new SMPBadRequestException("Save Service Metadata was called with inconsistent values.\n" + "Service Information Document Type ID: <none>\n" + "URL parameter value: '" + aPathDocTypeID.getURIEncoded() + "'", m_aAPIDataProvider.getCurrentURI());
}
final IDocumentTypeIdentifier aPayloadDocTypeID = aIdentifierFactory.createDocumentTypeIdentifier(aServiceInformation.getDocumentIdentifier().getScheme(), aServiceInformation.getDocumentIdentifier().getValue());
if (!aPathDocTypeID.hasSameContent(aPayloadDocTypeID)) {
// Document type ID in URL must match the one in XML structure
throw new SMPBadRequestException("Save Service Metadata was called with inconsistent values.\n" + "Service Information Document Type ID: '" + aPayloadDocTypeID.getURIEncoded() + "'\n" + "URL parameter value: '" + aPathDocTypeID.getURIEncoded() + "'", m_aAPIDataProvider.getCurrentURI());
}
}
// Main save
final IUser aDataUser = SMPUserManagerPhoton.validateUserCredentials(aCredentials);
SMPUserManagerPhoton.verifyOwnership(aPathServiceGroupID, aDataUser);
final ISMPServiceGroupManager aServiceGroupMgr = SMPMetaManager.getServiceGroupMgr();
final ISMPServiceGroup aPathServiceGroup = aServiceGroupMgr.getSMPServiceGroupOfID(aPathServiceGroupID);
if (aPathServiceGroup == null) {
// Service group not found
throw new SMPNotFoundException("Service Group '" + sPathServiceGroupID + "' is not on this SMP", m_aAPIDataProvider.getCurrentURI());
}
if (aServiceMetadata.getRedirect() != null) {
// Handle redirect
final ISMPRedirectManager aRedirectMgr = SMPMetaManager.getRedirectMgr();
// not available in OASIS BDXR SMP v1 mode
final X509Certificate aCertificate = null;
if (aRedirectMgr.createOrUpdateSMPRedirect(aPathServiceGroup, aPathDocTypeID, aServiceMetadata.getRedirect().getHref(), aServiceMetadata.getRedirect().getCertificateUID(), aCertificate, BDXR1ExtensionConverter.convertToString(aServiceMetadata.getRedirect().getExtension())) == null) {
if (LOGGER.isErrorEnabled())
LOGGER.error(sLog + " - ERROR - Redirect");
STATS_COUNTER_ERROR.increment(sAction);
return ESuccess.FAILURE;
}
if (LOGGER.isInfoEnabled())
LOGGER.info(sLog + " SUCCESS - Redirect");
} else if (aServiceInformation != null) {
// Handle service information
final ProcessListType aJAXBProcesses = aServiceInformation.getProcessList();
final ICommonsList<SMPProcess> aProcesses = new CommonsArrayList<>();
for (final ProcessType aJAXBProcess : aJAXBProcesses.getProcess()) {
final ICommonsList<SMPEndpoint> aEndpoints = new CommonsArrayList<>();
for (final EndpointType aJAXBEndpoint : aJAXBProcess.getServiceEndpointList().getEndpoint()) {
final SMPEndpoint aEndpoint = new SMPEndpoint(aJAXBEndpoint.getTransportProfile(), aJAXBEndpoint.getEndpointURI(), BooleanHelper.getBooleanValue(aJAXBEndpoint.isRequireBusinessLevelSignature(), false), aJAXBEndpoint.getMinimumAuthenticationLevel(), aJAXBEndpoint.getServiceActivationDate(), aJAXBEndpoint.getServiceExpirationDate(), Base64.encodeBytes(aJAXBEndpoint.getCertificate()), aJAXBEndpoint.getServiceDescription(), aJAXBEndpoint.getTechnicalContactUrl(), aJAXBEndpoint.getTechnicalInformationUrl(), BDXR1ExtensionConverter.convertToString(aJAXBEndpoint.getExtension()));
aEndpoints.add(aEndpoint);
}
final SMPProcess aProcess = new SMPProcess(SimpleProcessIdentifier.wrap(aJAXBProcess.getProcessIdentifier()), aEndpoints, BDXR1ExtensionConverter.convertToString(aJAXBProcess.getExtension()));
aProcesses.add(aProcess);
}
final ISMPServiceInformationManager aServiceInfoMgr = SMPMetaManager.getServiceInformationMgr();
final String sExtensionXML = BDXR1ExtensionConverter.convertToString(aServiceInformation.getExtension());
if (aServiceInfoMgr.mergeSMPServiceInformation(new SMPServiceInformation(aPathServiceGroup, aPathDocTypeID, aProcesses, sExtensionXML)).isFailure()) {
if (LOGGER.isErrorEnabled())
LOGGER.error(sLog + " - ERROR - ServiceInformation");
STATS_COUNTER_ERROR.increment(sAction);
return ESuccess.FAILURE;
}
if (LOGGER.isInfoEnabled())
LOGGER.info(sLog + " SUCCESS - ServiceInformation");
} else {
throw new SMPBadRequestException("Save Service Metadata was called with neither a Redirect nor a ServiceInformation", m_aAPIDataProvider.getCurrentURI());
}
if (false)
if (LOGGER.isInfoEnabled())
LOGGER.info(sLog + " SUCCESS");
STATS_COUNTER_SUCCESS.increment(sAction);
return ESuccess.SUCCESS;
} catch (final SMPServerException ex) {
if (LOGGER.isWarnEnabled())
LOGGER.warn(sLog + " ERROR - " + ex.getMessage());
STATS_COUNTER_ERROR.increment(sAction);
throw ex;
}
}
use of com.helger.phoss.smp.domain.serviceinfo.SMPEndpoint in project phoss-smp by phax.
the class SMPServiceInformationManagerJDBC method getAllSMPServiceInformation.
@Nonnull
@ReturnsMutableCopy
public ICommonsList<ISMPServiceInformation> getAllSMPServiceInformation() {
final ICommonsList<ISMPServiceInformation> ret = new CommonsArrayList<>();
final ICommonsList<DBResultRow> aDBResult = newExecutor().queryAll("SELECT sm.businessIdentifierScheme, sm.businessIdentifier, sm.documentIdentifierScheme, sm.documentIdentifier, sm.extension," + " sp.processIdentifierType, sp.processIdentifier, sp.extension," + " se.transportProfile, se.endpointReference, se.requireBusinessLevelSignature, se.minimumAuthenticationLevel," + " se.serviceActivationDate, se.serviceExpirationDate, se.certificate, se.serviceDescription," + " se.technicalContactUrl, se.technicalInformationUrl, se.extension" + " FROM smp_service_metadata AS sm" + " INNER JOIN smp_process AS sp" + " ON sm.businessIdentifierScheme=sp.businessIdentifierScheme AND sm.businessIdentifier=sp.businessIdentifier" + " AND sm.documentIdentifierScheme=sp.documentIdentifierScheme AND sm.documentIdentifier=sp.documentIdentifier" + " INNER JOIN smp_endpoint AS se" + " ON sp.businessIdentifierScheme=se.businessIdentifierScheme AND sp.businessIdentifier=se.businessIdentifier" + " AND sp.documentIdentifierScheme=se.documentIdentifierScheme AND sp.documentIdentifier=se.documentIdentifier" + " AND sp.processIdentifierType=se.processIdentifierType AND sp.processIdentifier=se.processIdentifier");
final ICommonsMap<IParticipantIdentifier, ICommonsMap<DocTypeAndExtension, ICommonsMap<SMPProcess, ICommonsList<SMPEndpoint>>>> aGrouping = new CommonsHashMap<>();
if (aDBResult != null)
for (final DBResultRow aDBRow : aDBResult) {
// Participant ID
final IParticipantIdentifier aParticipantID = new SimpleParticipantIdentifier(aDBRow.getAsString(0), aDBRow.getAsString(1));
// Document type ID and extension
final IDocumentTypeIdentifier aDocTypeID = new SimpleDocumentTypeIdentifier(aDBRow.getAsString(2), aDBRow.getAsString(3));
final String sServiceInformationExtension = aDBRow.getAsString(4);
// Process without endpoints
final SMPProcess aProcess = new SMPProcess(new SimpleProcessIdentifier(aDBRow.getAsString(5), aDBRow.getAsString(6)), null, aDBRow.getAsString(7));
// Don't add endpoint to process, because that impacts
// SMPProcess.equals/hashcode
final SMPEndpoint aEndpoint = new SMPEndpoint(aDBRow.getAsString(8), aDBRow.getAsString(9), aDBRow.getAsBoolean(10, SMPEndpoint.DEFAULT_REQUIRES_BUSINESS_LEVEL_SIGNATURE), aDBRow.getAsString(11), aDBRow.getAsXMLOffsetDateTime(12), aDBRow.getAsXMLOffsetDateTime(13), aDBRow.getAsString(14), aDBRow.getAsString(15), aDBRow.getAsString(16), aDBRow.getAsString(17), aDBRow.getAsString(18));
aGrouping.computeIfAbsent(aParticipantID, k -> new CommonsHashMap<>()).computeIfAbsent(new DocTypeAndExtension(aDocTypeID, sServiceInformationExtension), k -> new CommonsHashMap<>()).computeIfAbsent(aProcess, k -> new CommonsArrayList<>()).add(aEndpoint);
}
// Per participant ID
for (final Map.Entry<IParticipantIdentifier, ICommonsMap<DocTypeAndExtension, ICommonsMap<SMPProcess, ICommonsList<SMPEndpoint>>>> aEntry : aGrouping.entrySet()) {
final ISMPServiceGroup aServiceGroup = m_aServiceGroupMgr.getSMPServiceGroupOfID(aEntry.getKey());
if (aServiceGroup == null)
throw new IllegalStateException("Failed to resolve service group for participant ID '" + aEntry.getKey().getURIEncoded() + "'");
// Per document type ID
for (final Map.Entry<DocTypeAndExtension, ICommonsMap<SMPProcess, ICommonsList<SMPEndpoint>>> aEntry2 : aEntry.getValue().entrySet()) {
// Flatten list
final ICommonsList<SMPProcess> aProcesses = new CommonsArrayList<>();
for (final Map.Entry<SMPProcess, ICommonsList<SMPEndpoint>> aEntry3 : aEntry2.getValue().entrySet()) {
final SMPProcess aProcess = aEntry3.getKey();
aProcess.addEndpoints(aEntry3.getValue());
aProcesses.add(aProcess);
}
final DocTypeAndExtension aDE = aEntry2.getKey();
ret.add(new SMPServiceInformation(aServiceGroup, aDE.m_aDocTypeID, aProcesses, aDE.m_sExt));
}
}
return ret;
}
use of com.helger.phoss.smp.domain.serviceinfo.SMPEndpoint in project phoss-smp by phax.
the class SMPServiceInformationManagerJDBC method getSMPServiceInformationOfServiceGroupAndDocumentType.
@Nullable
public ISMPServiceInformation getSMPServiceInformationOfServiceGroupAndDocumentType(@Nullable final ISMPServiceGroup aServiceGroup, @Nullable final IDocumentTypeIdentifier aDocTypeID) {
if (aServiceGroup == null)
return null;
if (aDocTypeID == null)
return null;
final IParticipantIdentifier aPID = aServiceGroup.getParticipantIdentifier();
final ICommonsList<DBResultRow> aDBResult = newExecutor().queryAll("SELECT sm.extension," + " sp.processIdentifierType, sp.processIdentifier, sp.extension," + " se.transportProfile, se.endpointReference, se.requireBusinessLevelSignature, se.minimumAuthenticationLevel," + " se.serviceActivationDate, se.serviceExpirationDate, se.certificate, se.serviceDescription," + " se.technicalContactUrl, se.technicalInformationUrl, se.extension" + " FROM smp_service_metadata AS sm" + " INNER JOIN smp_process AS sp" + " ON sm.businessIdentifierScheme=sp.businessIdentifierScheme AND sm.businessIdentifier=sp.businessIdentifier" + " AND sm.documentIdentifierScheme=sp.documentIdentifierScheme AND sm.documentIdentifier=sp.documentIdentifier" + " INNER JOIN smp_endpoint AS se" + " ON sp.businessIdentifierScheme=se.businessIdentifierScheme AND sp.businessIdentifier=se.businessIdentifier" + " AND sp.documentIdentifierScheme=se.documentIdentifierScheme AND sp.documentIdentifier=se.documentIdentifier" + " AND sp.processIdentifierType=se.processIdentifierType AND sp.processIdentifier=se.processIdentifier" + " WHERE sm.businessIdentifierScheme=? AND sm.businessIdentifier=? AND sm.documentIdentifierScheme=? AND sm.documentIdentifier=?", new ConstantPreparedStatementDataProvider(aPID.getScheme(), aPID.getValue(), aDocTypeID.getScheme(), aDocTypeID.getValue()));
if (aDBResult != null && aDBResult.isNotEmpty()) {
final String sServiceInformationExtension = aDBResult.getFirst().getAsString(0);
final ICommonsMap<SMPProcess, ICommonsList<SMPEndpoint>> aEndpoints = new CommonsHashMap<>();
for (final DBResultRow aDBRow : aDBResult) {
// Process without endpoints as key
final SMPProcess aProcess = new SMPProcess(new SimpleProcessIdentifier(aDBRow.getAsString(1), aDBRow.getAsString(2)), null, aDBRow.getAsString(3));
final SMPEndpoint aEndpoint = new SMPEndpoint(aDBRow.getAsString(4), aDBRow.getAsString(5), aDBRow.getAsBoolean(6, SMPEndpoint.DEFAULT_REQUIRES_BUSINESS_LEVEL_SIGNATURE), aDBRow.getAsString(7), aDBRow.getAsXMLOffsetDateTime(8), aDBRow.getAsXMLOffsetDateTime(9), aDBRow.getAsString(10), aDBRow.getAsString(11), aDBRow.getAsString(12), aDBRow.getAsString(13), aDBRow.getAsString(14));
aEndpoints.computeIfAbsent(aProcess, k -> new CommonsArrayList<>()).add(aEndpoint);
}
// Flatten list
final ICommonsList<SMPProcess> aProcesses = new CommonsArrayList<>();
for (final Map.Entry<SMPProcess, ICommonsList<SMPEndpoint>> aEntry : aEndpoints.entrySet()) {
final SMPProcess aProcess = aEntry.getKey();
aProcess.addEndpoints(aEntry.getValue());
aProcesses.add(aProcess);
}
return new SMPServiceInformation(aServiceGroup, aDocTypeID, aProcesses, sServiceInformationExtension);
}
return null;
}
use of com.helger.phoss.smp.domain.serviceinfo.SMPEndpoint in project phoss-smp by phax.
the class SMPServiceInformationManagerXMLTest method testServiceRegistration.
@Test
public void testServiceRegistration() throws SMPServerException {
// Ensure the user is present
final IUser aTestUser = PhotonSecurityManager.getUserMgr().getUserOfID(CSecurity.USER_ADMINISTRATOR_ID);
assertNotNull(aTestUser);
final IIdentifierFactory aIdentifierFactory = SMPMetaManager.getIdentifierFactory();
final ISMPServiceGroupManager aServiceGroupMgr = SMPMetaManager.getServiceGroupMgr();
final ISMPServiceInformationManager aServiceInformationMgr = SMPMetaManager.getServiceInformationMgr();
assertEquals(0, aServiceInformationMgr.getSMPServiceInformationCount());
// Delete existing service group
final IParticipantIdentifier aPI = aIdentifierFactory.createParticipantIdentifier(PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME, "0088:dummy");
aServiceGroupMgr.deleteSMPServiceGroupNoEx(aPI, true);
final ISMPServiceGroup aSG = aServiceGroupMgr.createSMPServiceGroup(aTestUser.getID(), aPI, null, true);
assertNotNull(aSG);
try {
final XMLOffsetDateTime aStartDT = PDTFactory.getCurrentXMLOffsetDateTime();
final XMLOffsetDateTime aEndDT = aStartDT.plusYears(1);
final IProcessIdentifier aProcessID = aIdentifierFactory.createProcessIdentifier(PeppolIdentifierHelper.DEFAULT_PROCESS_SCHEME, "testproc");
final IDocumentTypeIdentifier aDocTypeID = aIdentifierFactory.createDocumentTypeIdentifier(PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS, "testdoctype");
{
// Create a new service information
final SMPEndpoint aEP = new SMPEndpoint("tp", "http://localhost/as2", false, "minauth", aStartDT, aEndDT, "cert", "sd", "tc", "ti", "<extep />");
final SMPProcess aProcess = new SMPProcess(aProcessID, new CommonsArrayList<>(aEP), "<extproc />");
assertTrue(aServiceInformationMgr.mergeSMPServiceInformation(new SMPServiceInformation(aSG, aDocTypeID, new CommonsArrayList<>(aProcess), "<extsi />")).isSuccess());
assertEquals(1, aServiceInformationMgr.getSMPServiceInformationCount());
assertEquals(1, CollectionHelper.getFirstElement(aServiceInformationMgr.getAllSMPServiceInformation()).getProcessCount());
assertEquals(1, CollectionHelper.getFirstElement(aServiceInformationMgr.getAllSMPServiceInformation()).getAllProcesses().get(0).getEndpointCount());
}
{
// Replace endpoint URL with equal transport profile -> replace
final ISMPServiceInformation aSI = aServiceInformationMgr.getSMPServiceInformationOfServiceGroupAndDocumentType(aSG, aDocTypeID);
assertNotNull(aSI);
final ISMPProcess aProcess = aSI.getProcessOfID(aProcessID);
assertNotNull(aProcess);
aProcess.setEndpoint(new SMPEndpoint("tp", "http://localhost/as2-ver2", false, "minauth", aStartDT, aEndDT, "cert", "sd", "tc", "ti", "<extep />"));
assertTrue(aServiceInformationMgr.mergeSMPServiceInformation(aSI).isSuccess());
assertEquals(1, aServiceInformationMgr.getSMPServiceInformationCount());
assertEquals(1, CollectionHelper.getFirstElement(aServiceInformationMgr.getAllSMPServiceInformation()).getProcessCount());
assertEquals(1, CollectionHelper.getFirstElement(aServiceInformationMgr.getAllSMPServiceInformation()).getAllProcesses().get(0).getEndpointCount());
assertEquals("http://localhost/as2-ver2", CollectionHelper.getFirstElement(aServiceInformationMgr.getAllSMPServiceInformation()).getAllProcesses().get(0).getAllEndpoints().get(0).getEndpointReference());
}
{
// Add endpoint with different transport profile -> added to existing
// process
final ISMPServiceInformation aSI = aServiceInformationMgr.getSMPServiceInformationOfServiceGroupAndDocumentType(aSG, aDocTypeID);
assertNotNull(aSI);
final ISMPProcess aProcess = aSI.getProcessOfID(aProcessID);
assertNotNull(aProcess);
aProcess.addEndpoint(new SMPEndpoint("tp2", "http://localhost/as2-tp2", false, "minauth", aStartDT, aEndDT, "cert", "sd", "tc", "ti", "<extep />"));
assertTrue(aServiceInformationMgr.mergeSMPServiceInformation(aSI).isSuccess());
assertEquals(1, aServiceInformationMgr.getSMPServiceInformationCount());
assertEquals(1, CollectionHelper.getFirstElement(aServiceInformationMgr.getAllSMPServiceInformation()).getProcessCount());
assertEquals(2, CollectionHelper.getFirstElement(aServiceInformationMgr.getAllSMPServiceInformation()).getAllProcesses().get(0).getEndpointCount());
}
{
// Add endpoint with different process - add to existing
// serviceGroup+docType part
final ISMPServiceInformation aSI = aServiceInformationMgr.getSMPServiceInformationOfServiceGroupAndDocumentType(aSG, aDocTypeID);
assertNotNull(aSI);
final SMPEndpoint aEP = new SMPEndpoint("tp", "http://localhost/as2", false, "minauth", aStartDT, aEndDT, "cert", "sd", "tc", "ti", "<extep />");
aSI.addProcess(new SMPProcess(PeppolIdentifierFactory.INSTANCE.createProcessIdentifierWithDefaultScheme("testproc2"), new CommonsArrayList<>(aEP), "<extproc />"));
assertTrue(aServiceInformationMgr.mergeSMPServiceInformation(aSI).isSuccess());
assertEquals(1, aServiceInformationMgr.getSMPServiceInformationCount());
assertEquals(2, CollectionHelper.getFirstElement(aServiceInformationMgr.getAllSMPServiceInformation()).getProcessCount());
assertEquals(2, CollectionHelper.getFirstElement(aServiceInformationMgr.getAllSMPServiceInformation()).getAllProcesses().get(0).getEndpointCount());
assertEquals(1, CollectionHelper.getFirstElement(aServiceInformationMgr.getAllSMPServiceInformation()).getAllProcesses().get(1).getEndpointCount());
}
} finally {
aServiceGroupMgr.deleteSMPServiceGroup(aPI, true);
}
}
Aggregations