use of com.helger.peppol.smlclient.ManageParticipantIdentifierServiceCaller in project peppol-commons by phax.
the class SMLFuncTest method testDnsCreation.
@Test
@Ignore("Requires a local SML for testing")
public void testDnsCreation() throws Exception {
final ManageParticipantIdentifierServiceCaller aPIClient = new ManageParticipantIdentifierServiceCaller(SML_INFO);
aPIClient.setSSLSocketFactory(createConfiguredSSLSocketFactory(SML_INFO, false));
final IParticipantIdentifier aPI = PeppolIdentifierFactory.INSTANCE.createParticipantIdentifierWithDefaultScheme(TEST_BUSINESS_IDENTIFIER1);
try {
aPIClient.delete(SMP_ID, aPI);
} catch (final com.helger.peppol.smlclient.participant.NotFoundFault e) {
// Do nothing since we just want to make sure it doesn't exists.
}
final String sDNSString = "http://" + TEST_BUSINESS_IDENTIFIER1 + ".0010.ubis.sml.smloc.org/";
InetAddress aAddress;
try {
aAddress = InetAddress.getByName(sDNSString);
fail("The hostname shouldn't exist");
} catch (final UnknownHostException e) {
// This should happen!
}
aPIClient.create(m_aServiceMetadataPublisher.getServiceMetadataPublisherID(), aPI);
aAddress = InetAddress.getByName(sDNSString);
assertEquals(P_ENDPOINTADDRESS, aAddress.getHostAddress());
}
use of com.helger.peppol.smlclient.ManageParticipantIdentifierServiceCaller in project phoss-smp by phax.
the class APIExecutorMigrationOutboundStartPut method invokeAPI.
public void invokeAPI(@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 String sServiceGroupID = aPathVariables.get(SMPRestFilter.PARAM_SERVICE_GROUP_ID);
final ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, sServiceGroupID);
// Is the writable API disabled?
if (SMPMetaManager.getSettings().isRESTWritableAPIDisabled()) {
throw new SMPPreconditionFailedException("The writable REST API is disabled. migrationOutboundStart will not be executed", aDataProvider.getCurrentURI());
}
final String sLogPrefix = "[REST API Migration-Outbound-Start] ";
LOGGER.info(sLogPrefix + "Starting outbound migration for Service Group ID '" + sServiceGroupID + "'");
// Only authenticated user may do so
final BasicAuthClientCredentials aBasicAuth = getMandatoryAuth(aRequestScope.headers());
SMPUserManagerPhoton.validateUserCredentials(aBasicAuth);
final ISMPSettings aSettings = SMPMetaManager.getSettings();
final ISMLInfo aSMLInfo = aSettings.getSMLInfo();
final IIdentifierFactory aIdentifierFactory = SMPMetaManager.getIdentifierFactory();
final ISMPServiceGroupManager aServiceGroupMgr = SMPMetaManager.getServiceGroupMgr();
final ISMPParticipantMigrationManager aParticipantMigrationMgr = SMPMetaManager.getParticipantMigrationMgr();
if (aSMLInfo == null) {
throw new SMPPreconditionFailedException("Currently no SML is available. Please select it in the UI at the 'SMP Settings' page", aDataProvider.getCurrentURI());
}
if (!aSettings.isSMLEnabled()) {
throw new SMPPreconditionFailedException("SML Connection is not enabled hence no participant can be migrated", aDataProvider.getCurrentURI());
}
final IParticipantIdentifier aServiceGroupID = aIdentifierFactory.parseParticipantIdentifier(sServiceGroupID);
if (aServiceGroupID == null) {
// Invalid identifier
throw SMPBadRequestException.failedToParseSG(sServiceGroupID, aDataProvider.getCurrentURI());
}
// Check that service group exists
if (!aServiceGroupMgr.containsSMPServiceGroupWithID(aServiceGroupID)) {
throw new SMPBadRequestException("The Service Group '" + sServiceGroupID + "' does not exist", aDataProvider.getCurrentURI());
}
// Ensure no existing migration is in process
if (aParticipantMigrationMgr.containsOutboundMigrationInProgress(aServiceGroupID)) {
throw new SMPBadRequestException("The outbound Participant Migration of the Service Group '" + sServiceGroupID + "' is already in progress", aDataProvider.getCurrentURI());
}
String sMigrationKey = null;
try {
final ManageParticipantIdentifierServiceCaller aCaller = new ManageParticipantIdentifierServiceCaller(aSMLInfo);
aCaller.setSSLSocketFactory(SMPKeyManager.getInstance().createSSLContext().getSocketFactory());
// Create a random migration key,
// Than call SML
sMigrationKey = aCaller.prepareToMigrate(aServiceGroupID, SMPServerConfiguration.getSMLSMPID());
LOGGER.info(sLogPrefix + "Successfully called prepareToMigrate on SML. Created migration key is '" + sMigrationKey + "'");
} catch (final BadRequestFault | InternalErrorFault | NotFoundFault | UnauthorizedFault | ClientTransportException ex) {
throw new SMPSMLException("Failed to call prepareToMigrate on SML for Service Group '" + sServiceGroupID + "'", ex);
}
// Remember internally
final ISMPParticipantMigration aMigration = aParticipantMigrationMgr.createOutboundParticipantMigration(aServiceGroupID, sMigrationKey);
if (aMigration == null) {
throw new SMPInternalErrorException("Failed to create outbound Participant Migration for '" + sServiceGroupID + "' internally");
}
LOGGER.info(sLogPrefix + "Successfully created outbound Participant Migration with ID '" + aMigration.getID() + "' internally.");
// Build result
final IMicroDocument aResponseDoc = new MicroDocument();
final IMicroElement eRoot = aResponseDoc.appendElement("migrationOutboundResponse");
eRoot.setAttribute("success", true);
eRoot.appendElement(XML_ELEMENT_PARTICIPANT_ID).appendText(sServiceGroupID);
eRoot.appendElement(XML_ELEMENT_MIGRATION_KEY).appendText(sMigrationKey);
final XMLWriterSettings aXWS = new XMLWriterSettings().setIndent(EXMLSerializeIndent.INDENT_AND_ALIGN);
aUnifiedResponse.setContentAndCharset(MicroWriter.getNodeAsString(aResponseDoc, aXWS), aXWS.getCharset()).setMimeType(new MimeType(CMimeType.APPLICATION_XML).addParameter(CMimeType.PARAMETER_NAME_CHARSET, aXWS.getCharset().name())).disableCaching();
}
use of com.helger.peppol.smlclient.ManageParticipantIdentifierServiceCaller in project phoss-smp by phax.
the class PageSecureServiceGroupMigrationInbound method validateAndSaveInputParameters.
@Override
protected void validateAndSaveInputParameters(@Nonnull final WebPageExecutionContext aWPEC, @Nullable final ISMPParticipantMigration aSelectedObject, @Nonnull final FormErrorList aFormErrors, @Nonnull final EWebPageFormAction eFormAction) {
final ISMPServiceGroupManager aServiceGroupMgr = SMPMetaManager.getServiceGroupMgr();
final IIdentifierFactory aIdentifierFactory = SMPMetaManager.getIdentifierFactory();
final ISMPSettings aSettings = SMPMetaManager.getSettings();
final ISMPParticipantMigrationManager aParticipantMigrationMgr = SMPMetaManager.getParticipantMigrationMgr();
final String sMigrationKey = aWPEC.params().getAsStringTrimmed(FIELD_MIGRATION_KEY);
final String sParticipantIDScheme = aWPEC.params().getAsStringTrimmed(FIELD_PARTICIPANT_ID_SCHEME);
final String sParticipantIDValue = aWPEC.params().getAsStringTrimmed(FIELD_PARTICIPANT_ID_VALUE);
IParticipantIdentifier aParticipantID = null;
final String sOwningUserID = aWPEC.params().getAsStringTrimmed(FIELD_OWNING_USER_ID);
final IUser aOwningUser = PhotonSecurityManager.getUserMgr().getUserOfID(sOwningUserID);
final String sExtension = aWPEC.params().getAsStringTrimmed(FIELD_EXTENSION);
// validations
if (StringHelper.hasNoText(sMigrationKey))
aFormErrors.addFieldError(FIELD_MIGRATION_KEY, "The migration key must not be empty!");
else if (!SMPParticipantMigration.isValidMigrationKey(sMigrationKey))
aFormErrors.addFieldError(FIELD_MIGRATION_KEY, "The migration key is not valid. Please verify the received code is correct.");
if (aIdentifierFactory.isParticipantIdentifierSchemeMandatory() && StringHelper.hasNoText(sParticipantIDScheme))
aFormErrors.addFieldError(FIELD_PARTICIPANT_ID_SCHEME, "Participant ID scheme must not be empty!");
else if (StringHelper.hasNoText(sParticipantIDValue))
aFormErrors.addFieldError(FIELD_PARTICIPANT_ID_VALUE, "Participant ID value must not be empty!");
else {
aParticipantID = aIdentifierFactory.createParticipantIdentifier(sParticipantIDScheme, sParticipantIDValue);
if (aParticipantID == null)
aFormErrors.addFieldError(FIELD_PARTICIPANT_ID_VALUE, "The provided participant ID has an invalid syntax!");
else if (aServiceGroupMgr.getSMPServiceGroupOfID(aParticipantID) != null)
aFormErrors.addFieldError(FIELD_PARTICIPANT_ID_VALUE, "Another service group for the same participant ID is already present (may be case insensitive)!");
}
if (StringHelper.hasNoText(sOwningUserID))
aFormErrors.addFieldError(FIELD_OWNING_USER_ID, "Owning User must not be empty!");
else if (aOwningUser == null)
aFormErrors.addFieldError(FIELD_OWNING_USER_ID, "Provided owning user does not exist!");
if (StringHelper.hasText(sExtension)) {
final IMicroDocument aDoc = MicroReader.readMicroXML(sExtension);
if (aDoc == null)
aFormErrors.addFieldError(FIELD_EXTENSION, "The extension must be XML content.");
}
final HCNodeList aRedirectNotes = new HCNodeList();
if (aFormErrors.isEmpty()) {
// create the Service Group locally
try {
final ManageParticipantIdentifierServiceCaller aCaller = new ManageParticipantIdentifierServiceCaller(aSettings.getSMLInfo());
aCaller.setSSLSocketFactory(SMPKeyManager.getInstance().createSSLContext().getSocketFactory());
// SML call
aCaller.migrate(aParticipantID, sMigrationKey, SMPServerConfiguration.getSMLSMPID());
LOGGER.info("Successfully migrated '" + aParticipantID.getURIEncoded() + "' in the SML to this SMP using migration key '" + sMigrationKey + "'");
aRedirectNotes.addChild(success("Successfully migrated '" + aParticipantID.getURIEncoded() + "' in SML to this SMP using migration key ").addChild(code(sMigrationKey)));
} catch (final Exception ex) {
LOGGER.error("Error invoking migrate on SML", ex);
// Use a global field error here, to avoid that users must enter the
// values over and over in case of error
aFormErrors.add(SingleError.builderError().errorText("Failed to confirm the migration for participant '" + aParticipantID.getURIEncoded() + "' in SML, hence the migration failed." + " Please check the participant identifier and the migration key.\n" + BootstrapTechnicalUI.getTechnicalDetailsString(ex, CSMPServer.DEFAULT_LOCALE)).build());
if (false)
aWPEC.postRedirectGetInternal(error("Failed to confirm the migration for participant '" + aParticipantID.getURIEncoded() + "' in SML, hence the migration failed." + " Please check the participant identifier and the migration key.").addChild(SMPCommonUI.getTechnicalDetailsUI(ex)));
}
}
if (aFormErrors.isEmpty()) {
// Now create the service group locally (it was already checked that the
// PID is available on this SMP)
ISMPServiceGroup aSG = null;
Exception aCaughtEx = null;
try {
// Do NOT create in SMK/SML
aSG = aServiceGroupMgr.createSMPServiceGroup(aOwningUser.getID(), aParticipantID, sExtension, false);
} catch (final Exception ex) {
aCaughtEx = ex;
}
if (aSG != null) {
aRedirectNotes.addChild(success("The new SMP Service Group for participant '" + aParticipantID.getURIEncoded() + "' was successfully created."));
} else {
aRedirectNotes.addChild(error("Error creating the new SMP Service Group for participant '" + aParticipantID.getURIEncoded() + "'.").addChild(SMPCommonUI.getTechnicalDetailsUI(aCaughtEx)));
}
// Remember internally
if (aParticipantMigrationMgr.createInboundParticipantMigration(aParticipantID, sMigrationKey) != null) {
aRedirectNotes.addChild(success().addChild(div("The participant migration for '" + aParticipantID.getURIEncoded() + "' with migration key ").addChild(code(sMigrationKey)).addChild(" was successfully performed.")).addChild(div("Please inform the source SMP that the migration was successful.")));
} else {
aRedirectNotes.addChild(error("Failed to store the participant migration for '" + aParticipantID.getURIEncoded() + "'."));
}
aWPEC.postRedirectGetInternal(aRedirectNotes);
}
}
use of com.helger.peppol.smlclient.ManageParticipantIdentifierServiceCaller in project phoss-smp by phax.
the class RegistrationHookWriteToSML method _createSMLCaller.
@Nonnull
private static ManageParticipantIdentifierServiceCaller _createSMLCaller() {
// SML endpoint (incl. the service name)
final ISMLInfo aSMLInfo = SMPMetaManager.getSettings().getSMLInfo();
if (aSMLInfo == null)
throw new IllegalStateException("Failed to get SML manage participant endpoint URL");
final URL aSMLEndpointURL = aSMLInfo.getManageParticipantIdentifierEndpointAddress();
final String sEndpointURL = aSMLEndpointURL.toExternalForm();
final String sLowerURL = sEndpointURL.toLowerCase(Locale.US);
if (LOGGER.isInfoEnabled())
LOGGER.info("Performing SML query to '" + sEndpointURL + "'");
// SSL socket factory
final SSLSocketFactory aSocketFactory;
if (sLowerURL.startsWith("https://")) {
// https connection
if (!SMPKeyManager.isKeyStoreValid())
throw new InitializationException("Cannot init registration hook to SML, because private key/certificate setup has errors: " + SMPKeyManager.getInitializationError());
try {
aSocketFactory = SMPKeyManager.getInstance().createSSLContext().getSocketFactory();
} catch (final Exception ex) {
throw new IllegalStateException("Failed to init SSLContext for SML access", ex);
}
} else {
// Local, http only access - no socket factory
aSocketFactory = null;
}
// Hostname verifier
final HostnameVerifier aHostnameVerifier;
if (sLowerURL.contains("//localhost") || sLowerURL.contains("//127.0.0.1")) {
// Accept all hostnames
aHostnameVerifier = new HostnameVerifierVerifyAll(false);
} else
aHostnameVerifier = null;
// Build WS client
final ManageParticipantIdentifierServiceCaller ret = new ManageParticipantIdentifierServiceCaller(aSMLEndpointURL);
ret.setSSLSocketFactory(aSocketFactory);
ret.setHostnameVerifier(aHostnameVerifier);
final Integer aConnectionTimeoutMS = SMPServerConfiguration.getSMLConnectionTimeoutMS();
if (aConnectionTimeoutMS != null)
ret.setConnectionTimeoutMS(aConnectionTimeoutMS.intValue());
final int nRequestTimeoutMS = SMPServerConfiguration.getSMLRequestTimeoutMS();
if (nRequestTimeoutMS >= 0)
ret.setRequestTimeoutMS(nRequestTimeoutMS);
return ret;
}
use of com.helger.peppol.smlclient.ManageParticipantIdentifierServiceCaller in project peppol-commons by phax.
the class SMLFuncTest method testMigrate.
@Test
public void testMigrate() throws Exception {
final ManageParticipantIdentifierServiceCaller aPIClientOld = new ManageParticipantIdentifierServiceCaller(SML_INFO);
aPIClientOld.setSSLSocketFactory(createConfiguredSSLSocketFactory(SML_INFO, false));
final ManageServiceMetadataServiceCaller aClient2 = new ManageServiceMetadataServiceCaller(SML_INFO);
aClient2.setSSLSocketFactory(createConfiguredSSLSocketFactory(SML_INFO, false));
try {
aClient2.delete(SMP_ID2);
} catch (final NotFoundFault e) {
// This is fine, since we are just cleaning
}
_createSMPData(aClient2, SMP_ID2);
final ManageParticipantIdentifierServiceCaller aPIClientNew = new ManageParticipantIdentifierServiceCaller(SML_INFO);
aPIClientNew.setSSLSocketFactory(createConfiguredSSLSocketFactory(SML_INFO, false));
final IParticipantIdentifier aPI = PeppolIdentifierFactory.INSTANCE.createParticipantIdentifierWithDefaultScheme(TEST_BUSINESS_IDENTIFIER1);
aPIClientOld.create(SMP_ID, aPI);
final String sMigrationKey = aPIClientOld.prepareToMigrate(aPI, SMP_ID);
assertNotNull(sMigrationKey);
aPIClientNew.migrate(aPI, sMigrationKey, SMP_ID2);
try {
aPIClientOld.delete(SMP_ID, aPI);
fail();
} catch (final UnauthorizedFault e) {
// This must happen.
}
// Should be able to delete, since New is now the owner
aPIClientNew.delete(SMP_ID, aPI);
}
Aggregations