Search in sources :

Example 11 with MimeType

use of com.helger.commons.mime.MimeType in project phoss-smp by phax.

the class APIExecutorMigrationInboundFromPathPut method migrationInbound.

public static void migrationInbound(@Nonnull final String sServiceGroupID, @Nonnull final String sMigrationKey, @Nonnull final String sLogPrefix, @Nonnull final IRequestWebScopeWithoutResponse aRequestScope, @Nonnull final UnifiedResponse aUnifiedResponse) throws SMPServerException, GeneralSecurityException {
    LOGGER.info(sLogPrefix + "Starting inbound migration for Service Group ID '" + sServiceGroupID + "' and migration key '" + sMigrationKey + "'");
    // Only authenticated user may do so
    final BasicAuthClientCredentials aBasicAuth = getMandatoryAuth(aRequestScope.headers());
    final IUser aOwningUser = SMPUserManagerPhoton.validateUserCredentials(aBasicAuth);
    final ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, sServiceGroupID);
    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 aParticipantID = aIdentifierFactory.parseParticipantIdentifier(sServiceGroupID);
    if (aParticipantID == null) {
        // Invalid identifier
        throw SMPBadRequestException.failedToParseSG(sServiceGroupID, aDataProvider.getCurrentURI());
    }
    // Check that service group does not exist yet
    if (aServiceGroupMgr.containsSMPServiceGroupWithID(aParticipantID)) {
        throw new SMPBadRequestException("The Service Group '" + sServiceGroupID + "' already exists.", aDataProvider.getCurrentURI());
    }
    if (false) {
        // valid
        if (aParticipantMigrationMgr.containsInboundMigration(aParticipantID)) {
            throw new SMPBadRequestException("The inbound migration of the Service Group '" + sServiceGroupID + "' is already contained.", aDataProvider.getCurrentURI());
        }
    }
    // 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(sLogPrefix + "Successfully migrated '" + aParticipantID.getURIEncoded() + "' in the SML to this SMP using migration key '" + sMigrationKey + "'");
    } catch (final BadRequestFault | InternalErrorFault | NotFoundFault | UnauthorizedFault | ClientTransportException ex) {
        throw new SMPSMLException("Failed to confirm the migration for participant '" + aParticipantID.getURIEncoded() + "' in SML, hence the migration failed." + " Please check the participant identifier and the migration key.", ex);
    }
    // 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 allow any Extension here
        // Do NOT create in SMK/SML
        aSG = aServiceGroupMgr.createSMPServiceGroup(aOwningUser.getID(), aParticipantID, (String) null, false);
    } catch (final Exception ex) {
        aCaughtEx = ex;
    }
    if (aSG != null) {
        LOGGER.info(sLogPrefix + "The new SMP Service Group for participant '" + aParticipantID.getURIEncoded() + "' was successfully created.");
    } else {
        // No exception here
        LOGGER.error(sLogPrefix + "Error creating the new SMP Service Group for participant '" + aParticipantID.getURIEncoded() + "'.", aCaughtEx);
    }
    // Remember internally
    final ISMPParticipantMigration aMigration = aParticipantMigrationMgr.createInboundParticipantMigration(aParticipantID, sMigrationKey);
    if (aMigration != null) {
        LOGGER.info(sLogPrefix + "The participant migration for '" + aParticipantID.getURIEncoded() + "' with migration key '" + sMigrationKey + "' was successfully performed. Please inform the source SMP that the migration was successful.");
    } else {
        // No exception here
        LOGGER.error(sLogPrefix + "Failed to store the participant migration for '" + aParticipantID.getURIEncoded() + "'.");
    }
    final IMicroDocument aResponseDoc = new MicroDocument();
    final IMicroElement eRoot = aResponseDoc.appendElement("migrationInboundResponse");
    eRoot.setAttribute("success", aSG != null && aMigration != null);
    eRoot.setAttribute("serviceGroupCreated", aSG != null);
    eRoot.setAttribute("migrationCreated", aMigration != null);
    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();
}
Also used : ClientTransportException(com.sun.xml.ws.client.ClientTransportException) ISMPServiceGroupManager(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager) BadRequestFault(com.helger.peppol.smlclient.participant.BadRequestFault) ISMLInfo(com.helger.peppol.sml.ISMLInfo) NotFoundFault(com.helger.peppol.smlclient.participant.NotFoundFault) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) CMimeType(com.helger.commons.mime.CMimeType) MimeType(com.helger.commons.mime.MimeType) IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) ISMPParticipantMigration(com.helger.phoss.smp.domain.pmigration.ISMPParticipantMigration) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider) IUser(com.helger.photon.security.user.IUser) ISMPParticipantMigrationManager(com.helger.phoss.smp.domain.pmigration.ISMPParticipantMigrationManager) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) SMPBadRequestException(com.helger.phoss.smp.exception.SMPBadRequestException) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) ManageParticipantIdentifierServiceCaller(com.helger.peppol.smlclient.ManageParticipantIdentifierServiceCaller) SMPBadRequestException(com.helger.phoss.smp.exception.SMPBadRequestException) SMPSMLException(com.helger.phoss.smp.exception.SMPSMLException) GeneralSecurityException(java.security.GeneralSecurityException) SMPServerException(com.helger.phoss.smp.exception.SMPServerException) SMPPreconditionFailedException(com.helger.phoss.smp.exception.SMPPreconditionFailedException) ClientTransportException(com.sun.xml.ws.client.ClientTransportException) UnauthorizedFault(com.helger.peppol.smlclient.participant.UnauthorizedFault) SMPPreconditionFailedException(com.helger.phoss.smp.exception.SMPPreconditionFailedException) BasicAuthClientCredentials(com.helger.http.basicauth.BasicAuthClientCredentials) ISMPSettings(com.helger.phoss.smp.settings.ISMPSettings) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument) InternalErrorFault(com.helger.peppol.smlclient.participant.InternalErrorFault) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier)

Example 12 with MimeType

use of com.helger.commons.mime.MimeType in project ph-commons by phax.

the class MimeTypeInfoMicroTypeConverter method convertToNative.

@Nullable
public MimeTypeInfo convertToNative(@Nonnull final IMicroElement aElement) {
    final ICommonsOrderedSet<MimeTypeWithSource> aMimeTypes = new CommonsLinkedHashSet<>();
    for (final IMicroElement eMimeType : aElement.getAllChildElements(ELEMENT_MIMETYPE)) {
        try {
            final MimeType aMimeType = MimeTypeParser.parseMimeType(eMimeType.getTextContentTrimmed());
            final String sSource = eMimeType.getAttributeValue(ATTR_SOURCE);
            aMimeTypes.add(new MimeTypeWithSource(aMimeType, sSource));
        } catch (final MimeTypeParserException ex) {
            throw new IllegalStateException("Failed to parse MIME type", ex);
        }
    }
    final String sComment = MicroHelper.getChildTextContent(aElement, ELEMENT_COMMENT);
    final ICommonsOrderedSet<String> aParentTypes = new CommonsLinkedHashSet<>();
    for (final IMicroElement eParentType : aElement.getAllChildElements(ELEMENT_PARENT_TYPE)) aParentTypes.add(eParentType.getTextContentTrimmed());
    final ICommonsOrderedSet<String> aGlobs = new CommonsLinkedHashSet<>();
    for (final IMicroElement eGlob : aElement.getAllChildElements(ELEMENT_GLOB)) aGlobs.add(eGlob.getTextContentTrimmed());
    final ICommonsOrderedSet<ExtensionWithSource> aExtensions = new CommonsLinkedHashSet<>();
    for (final IMicroElement eExtension : aElement.getAllChildElements(ELEMENT_EXTENSION)) {
        // May be null if the empty extension ("") is used
        final String sExtension = StringHelper.getNotNull(eExtension.getTextContentTrimmed());
        final String sSource = eExtension.getAttributeValue(ATTR_SOURCE);
        aExtensions.add(new ExtensionWithSource(sExtension, sSource));
    }
    final String sSource = aElement.getAttributeValue(ATTR_SOURCE);
    return new MimeTypeInfo(aMimeTypes, sComment, aParentTypes, aGlobs, aExtensions, sSource);
}
Also used : MimeTypeWithSource(com.helger.xml.util.mime.MimeTypeInfo.MimeTypeWithSource) ExtensionWithSource(com.helger.xml.util.mime.MimeTypeInfo.ExtensionWithSource) IMicroElement(com.helger.xml.microdom.IMicroElement) MimeTypeParserException(com.helger.commons.mime.MimeTypeParserException) CommonsLinkedHashSet(com.helger.commons.collection.impl.CommonsLinkedHashSet) MimeType(com.helger.commons.mime.MimeType) Nullable(javax.annotation.Nullable)

Aggregations

MimeType (com.helger.commons.mime.MimeType)12 CMimeType (com.helger.commons.mime.CMimeType)9 IMicroDocument (com.helger.xml.microdom.IMicroDocument)8 BasicAuthClientCredentials (com.helger.http.basicauth.BasicAuthClientCredentials)7 ISMPServiceGroupManager (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager)7 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)7 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)5 ISMPServerAPIDataProvider (com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider)5 IMicroElement (com.helger.xml.microdom.IMicroElement)5 ISMPSettingsManager (com.helger.phoss.smp.settings.ISMPSettingsManager)4 IXMLWriterSettings (com.helger.xml.serialize.write.IXMLWriterSettings)4 ISMPParticipantMigration (com.helger.phoss.smp.domain.pmigration.ISMPParticipantMigration)3 ISMPParticipantMigrationManager (com.helger.phoss.smp.domain.pmigration.ISMPParticipantMigrationManager)3 SMPBadRequestException (com.helger.phoss.smp.exception.SMPBadRequestException)3 SMPPreconditionFailedException (com.helger.phoss.smp.exception.SMPPreconditionFailedException)3 IUser (com.helger.photon.security.user.IUser)3 MicroDocument (com.helger.xml.microdom.MicroDocument)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)2 CommonsLinkedHashSet (com.helger.commons.collection.impl.CommonsLinkedHashSet)2 IJsonObject (com.helger.json.IJsonObject)2