Search in sources :

Example 6 with ISMPServerAPIDataProvider

use of com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider in project phoss-smp by phax.

the class APIExecutorServiceMetadataDelete 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 sPathServiceGroupID = aPathVariables.get(SMPRestFilter.PARAM_SERVICE_GROUP_ID);
    final ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, sPathServiceGroupID);
    // Is the writable API disabled?
    if (SMPMetaManager.getSettings().isRESTWritableAPIDisabled()) {
        throw new SMPPreconditionFailedException("The writable REST API is disabled. deleteServiceRegistration will not be executed", aDataProvider.getCurrentURI());
    }
    final String sDocumentTypeID = aPathVariables.get(SMPRestFilter.PARAM_DOCUMENT_TYPE_ID);
    final BasicAuthClientCredentials aBasicAuth = getMandatoryAuth(aRequestScope.headers());
    switch(SMPServerConfiguration.getRESTType()) {
        case PEPPOL:
            new SMPServerAPI(aDataProvider).deleteServiceRegistration(sPathServiceGroupID, sDocumentTypeID, aBasicAuth);
            break;
        case OASIS_BDXR_V1:
            new BDXR1ServerAPI(aDataProvider).deleteServiceRegistration(sPathServiceGroupID, sDocumentTypeID, aBasicAuth);
            break;
        default:
            throw new UnsupportedOperationException("Unsupported REST type specified!");
    }
    aUnifiedResponse.setStatus(CHttp.HTTP_OK).disableCaching();
}
Also used : SMPPreconditionFailedException(com.helger.phoss.smp.exception.SMPPreconditionFailedException) BasicAuthClientCredentials(com.helger.http.basicauth.BasicAuthClientCredentials) SMPServerAPI(com.helger.phoss.smp.restapi.SMPServerAPI) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider) BDXR1ServerAPI(com.helger.phoss.smp.restapi.BDXR1ServerAPI)

Example 7 with ISMPServerAPIDataProvider

use of com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider in project phoss-smp by phax.

the class APIExecutorServiceMetadataGet 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 sPathServiceGroupID = aPathVariables.get(SMPRestFilter.PARAM_SERVICE_GROUP_ID);
    final String sPathDocumentTypeID = aPathVariables.get(SMPRestFilter.PARAM_DOCUMENT_TYPE_ID);
    final ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, sPathServiceGroupID);
    // Create the unsigned response document
    final Document aDoc;
    switch(SMPServerConfiguration.getRESTType()) {
        case PEPPOL:
            {
                final com.helger.xsds.peppol.smp1.SignedServiceMetadataType ret = new SMPServerAPI(aDataProvider).getServiceRegistration(sPathServiceGroupID, sPathDocumentTypeID);
                // Convert to DOM document
                // Disable XSD check, because Signature is added later
                final SMPMarshallerSignedServiceMetadataType aMarshaller = new SMPMarshallerSignedServiceMetadataType(false);
                aDoc = aMarshaller.getAsDocument(ret);
                break;
            }
        case OASIS_BDXR_V1:
            {
                final com.helger.xsds.bdxr.smp1.SignedServiceMetadataType ret = new BDXR1ServerAPI(aDataProvider).getServiceRegistration(sPathServiceGroupID, sPathDocumentTypeID);
                // Convert to DOM document
                // Disable XSD check, because Signature is added later
                final BDXR1MarshallerSignedServiceMetadataType aMarshaller = new BDXR1MarshallerSignedServiceMetadataType(false);
                aDoc = aMarshaller.getAsDocument(ret);
                break;
            }
        default:
            throw new UnsupportedOperationException("Unsupported REST type specified!");
    }
    if (aDoc == null)
        throw new IllegalStateException("Failed to serialize unsigned node!");
    // Sign the document
    try {
        SMPKeyManager.getInstance().signXML(aDoc.getDocumentElement(), SMPServerConfiguration.getRESTType().isBDXR());
        LOGGER.info("Successfully signed response XML");
    } catch (final Exception ex) {
        throw new SMPInternalErrorException("Error in signing the response XML", ex);
    }
    // Serialize the signed document
    try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream()) {
        if (false) {
            // IMPORTANT: no indent and no align!
            final IXMLWriterSettings aSettings = XMLWriterSettings.createForCanonicalization();
            // Write the result to a byte array
            if (XMLWriter.writeToStream(aDoc, aBAOS, aSettings).isFailure())
                throw new IllegalStateException("Failed to serialize signed node!");
        } else {
            // for validating the signature!
            try {
                final Transformer aTransformer = XMLTransformerFactory.newTransformer();
                aTransformer.transform(new DOMSource(aDoc), new StreamResult(aBAOS));
            } catch (final TransformerException ex) {
                throw new IllegalStateException("Failed to serialized signed node", ex);
            }
        }
        aUnifiedResponse.setContent(aBAOS.toByteArray()).setMimeType(CMimeType.TEXT_XML).setCharset(XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SMPServerAPI(com.helger.phoss.smp.restapi.SMPServerAPI) IXMLWriterSettings(com.helger.xml.serialize.write.IXMLWriterSettings) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) SMPMarshallerSignedServiceMetadataType(com.helger.smpclient.peppol.marshal.SMPMarshallerSignedServiceMetadataType) BDXR1MarshallerSignedServiceMetadataType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerSignedServiceMetadataType) BDXR1ServerAPI(com.helger.phoss.smp.restapi.BDXR1ServerAPI) Document(org.w3c.dom.Document) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) TransformerException(javax.xml.transform.TransformerException) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) BDXR1MarshallerSignedServiceMetadataType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerSignedServiceMetadataType) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider) TransformerException(javax.xml.transform.TransformerException) SMPMarshallerSignedServiceMetadataType(com.helger.smpclient.peppol.marshal.SMPMarshallerSignedServiceMetadataType)

Example 8 with ISMPServerAPIDataProvider

use of com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider in project phoss-smp by phax.

the class APIExecutorServiceMetadataPut 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 sPathServiceGroupID = aPathVariables.get(SMPRestFilter.PARAM_SERVICE_GROUP_ID);
    final ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, sPathServiceGroupID);
    // Is the writable API disabled?
    if (SMPMetaManager.getSettings().isRESTWritableAPIDisabled()) {
        throw new SMPPreconditionFailedException("The writable REST API is disabled. saveServiceRegistration will not be executed", aDataProvider.getCurrentURI());
    }
    // Parse main payload
    final byte[] aPayload = StreamHelper.getAllBytes(aRequestScope.getRequest().getInputStream());
    final Document aServiceMetadataDoc = DOMReader.readXMLDOM(aPayload);
    if (aServiceMetadataDoc == null) {
        throw new SMPBadRequestException("Failed to parse provided payload as XML", aDataProvider.getCurrentURI());
    }
    final String sDocumentTypeID = aPathVariables.get(SMPRestFilter.PARAM_DOCUMENT_TYPE_ID);
    final BasicAuthClientCredentials aBasicAuth = getMandatoryAuth(aRequestScope.headers());
    ESuccess eSuccess = ESuccess.FAILURE;
    switch(SMPServerConfiguration.getRESTType()) {
        case PEPPOL:
            {
                final com.helger.xsds.peppol.smp1.ServiceMetadataType aServiceMetadata = new SMPMarshallerServiceMetadataType(XML_SCHEMA_VALIDATION).read(aServiceMetadataDoc);
                if (aServiceMetadata != null) {
                    eSuccess = new SMPServerAPI(aDataProvider).saveServiceRegistration(sPathServiceGroupID, sDocumentTypeID, aServiceMetadata, aBasicAuth);
                }
                break;
            }
        case OASIS_BDXR_V1:
            {
                final com.helger.xsds.bdxr.smp1.ServiceMetadataType aServiceMetadata = new BDXR1MarshallerServiceMetadataType(XML_SCHEMA_VALIDATION).read(aServiceMetadataDoc);
                if (aServiceMetadata != null) {
                    eSuccess = new BDXR1ServerAPI(aDataProvider).saveServiceRegistration(sPathServiceGroupID, sDocumentTypeID, aServiceMetadata, aBasicAuth);
                }
                break;
            }
        default:
            throw new UnsupportedOperationException("Unsupported REST type specified!");
    }
    if (eSuccess.isFailure())
        aUnifiedResponse.setStatus(CHttp.HTTP_INTERNAL_SERVER_ERROR);
    else
        aUnifiedResponse.setStatus(CHttp.HTTP_OK).disableCaching();
}
Also used : ESuccess(com.helger.commons.state.ESuccess) SMPMarshallerServiceMetadataType(com.helger.smpclient.peppol.marshal.SMPMarshallerServiceMetadataType) SMPBadRequestException(com.helger.phoss.smp.exception.SMPBadRequestException) SMPServerAPI(com.helger.phoss.smp.restapi.SMPServerAPI) BDXR1ServerAPI(com.helger.phoss.smp.restapi.BDXR1ServerAPI) Document(org.w3c.dom.Document) BDXR1MarshallerServiceMetadataType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerServiceMetadataType) SMPPreconditionFailedException(com.helger.phoss.smp.exception.SMPPreconditionFailedException) BasicAuthClientCredentials(com.helger.http.basicauth.BasicAuthClientCredentials) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider) BDXR1MarshallerServiceMetadataType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerServiceMetadataType) SMPMarshallerServiceMetadataType(com.helger.smpclient.peppol.marshal.SMPMarshallerServiceMetadataType)

Example 9 with ISMPServerAPIDataProvider

use of com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider in project phoss-smp by phax.

the class APIExecutorExportSpecificXMLVer1 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 sPathServiceGroupID = aPathVariables.get(SMPRestFilter.PARAM_SERVICE_GROUP_ID);
    final String sLogPrefix = "[REST API Export-Specific-XML-V1] ";
    LOGGER.info(sLogPrefix + "Starting Export of '" + sPathServiceGroupID + "'");
    // Only authenticated user may do so
    final BasicAuthClientCredentials aBasicAuth = getMandatoryAuth(aRequestScope.headers());
    SMPUserManagerPhoton.validateUserCredentials(aBasicAuth);
    // Start action after authentication
    final ISMPSettingsManager aSettingsMgr = SMPMetaManager.getSettingsMgr();
    final ISMPServiceGroupManager aServiceGroupMgr = SMPMetaManager.getServiceGroupMgr();
    final ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, null);
    final IIdentifierFactory aIdentifierFactory = SMPMetaManager.getIdentifierFactory();
    final IParticipantIdentifier aPathServiceGroupID = aIdentifierFactory.parseParticipantIdentifier(sPathServiceGroupID);
    if (aPathServiceGroupID == null) {
        // Invalid identifier
        throw SMPBadRequestException.failedToParseSG(sPathServiceGroupID, aDataProvider.getCurrentURI());
    }
    // Retrieve the service group
    final ISMPServiceGroup aServiceGroup = aServiceGroupMgr.getSMPServiceGroupOfID(aPathServiceGroupID);
    if (aServiceGroup == null) {
        // No such service group
        throw new SMPNotFoundException("Unknown Service Group '" + sPathServiceGroupID + "'", aDataProvider.getCurrentURI());
    }
    final boolean bIncludeBusinessCards = aRequestScope.params().getAsBoolean(PARAM_INCLUDE_BUSINESS_CARDS, aSettingsMgr.getSettings().isDirectoryIntegrationEnabled());
    final IMicroDocument aDoc = ServiceGroupExport.createExportDataXMLVer10(new CommonsArrayList<>(aServiceGroup), bIncludeBusinessCards);
    LOGGER.info(sLogPrefix + "Finished creating Export data");
    // Build the XML response
    final IXMLWriterSettings aXWS = new XMLWriterSettings();
    aUnifiedResponse.setContentAndCharset(MicroWriter.getNodeAsString(aDoc, aXWS), aXWS.getCharset()).setMimeType(new MimeType(CMimeType.APPLICATION_XML).addParameter(CMimeType.PARAMETER_NAME_CHARSET, aXWS.getCharset().name())).disableCaching();
}
Also used : ISMPServiceGroupManager(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager) ISMPSettingsManager(com.helger.phoss.smp.settings.ISMPSettingsManager) IXMLWriterSettings(com.helger.xml.serialize.write.IXMLWriterSettings) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) IXMLWriterSettings(com.helger.xml.serialize.write.IXMLWriterSettings) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) CMimeType(com.helger.commons.mime.CMimeType) MimeType(com.helger.commons.mime.MimeType) SMPNotFoundException(com.helger.phoss.smp.exception.SMPNotFoundException) BasicAuthClientCredentials(com.helger.http.basicauth.BasicAuthClientCredentials) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider) IMicroDocument(com.helger.xml.microdom.IMicroDocument) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier)

Example 10 with ISMPServerAPIDataProvider

use of com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider in project phoss-smp by phax.

the class APIExecutorBusinessCardDelete 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. deleteBusinessCard will not be executed", aDataProvider.getCurrentURI());
    }
    if (!SMPMetaManager.getSettings().isDirectoryIntegrationEnabled()) {
        // PD integration is disabled
        throw new SMPPreconditionFailedException("The " + SMPWebAppConfiguration.getDirectoryName() + " integration is disabled. deleteBusinessCard will not be executed", aDataProvider.getCurrentURI());
    }
    final BasicAuthClientCredentials aBasicAuth = getMandatoryAuth(aRequestScope.headers());
    new BusinessCardServerAPI(aDataProvider).deleteBusinessCard(sServiceGroupID, aBasicAuth);
    aUnifiedResponse.setStatus(CHttp.HTTP_OK);
}
Also used : SMPPreconditionFailedException(com.helger.phoss.smp.exception.SMPPreconditionFailedException) BasicAuthClientCredentials(com.helger.http.basicauth.BasicAuthClientCredentials) BusinessCardServerAPI(com.helger.phoss.smp.restapi.BusinessCardServerAPI) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider)

Aggregations

ISMPServerAPIDataProvider (com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider)24 SMPPreconditionFailedException (com.helger.phoss.smp.exception.SMPPreconditionFailedException)18 BasicAuthClientCredentials (com.helger.http.basicauth.BasicAuthClientCredentials)15 SMPBadRequestException (com.helger.phoss.smp.exception.SMPBadRequestException)11 BDXR1ServerAPI (com.helger.phoss.smp.restapi.BDXR1ServerAPI)9 SMPServerAPI (com.helger.phoss.smp.restapi.SMPServerAPI)9 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)8 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)8 ISMPServiceGroupManager (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager)6 IMicroDocument (com.helger.xml.microdom.IMicroDocument)6 CMimeType (com.helger.commons.mime.CMimeType)5 MimeType (com.helger.commons.mime.MimeType)5 SMPInternalErrorException (com.helger.phoss.smp.exception.SMPInternalErrorException)5 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)5 StopWatch (com.helger.commons.timing.StopWatch)4 IJsonObject (com.helger.json.IJsonObject)4 JsonWriter (com.helger.json.serialize.JsonWriter)4 ISMPParticipantMigration (com.helger.phoss.smp.domain.pmigration.ISMPParticipantMigration)4 ISMPParticipantMigrationManager (com.helger.phoss.smp.domain.pmigration.ISMPParticipantMigrationManager)4 ESuccess (com.helger.commons.state.ESuccess)3