Search in sources :

Example 11 with BasicAuthClientCredentials

use of com.helger.http.basicauth.BasicAuthClientCredentials in project phoss-smp by phax.

the class AbstractSMPAPIExecutor method getMandatoryAuth.

/**
 * Get the basic auth from the header
 *
 * @param aHttpHeaders
 *        Headers to extract from. May not be <code>null</code>.
 * @return The extracted basic auth. Never <code>null</code>.
 * @throws SMPUnauthorizedException
 *         If no BasicAuth HTTP header is present
 */
@Nonnull
public static BasicAuthClientCredentials getMandatoryAuth(@Nonnull final HttpHeaderMap aHttpHeaders) throws SMPUnauthorizedException {
    final ICommonsList<String> aHeaders = aHttpHeaders.getAllHeaderValues(CHttpHeader.AUTHORIZATION);
    if (aHeaders.isEmpty())
        throw new SMPUnauthorizedException("Missing required HTTP header '" + CHttpHeader.AUTHORIZATION + "' for user authentication");
    final BasicAuthClientCredentials ret = HttpBasicAuth.getBasicAuthClientCredentials(aHeaders.getFirst());
    if (ret == null)
        throw new SMPUnauthorizedException("The HTTP header '" + CHttpHeader.AUTHORIZATION + "' is malformed");
    return ret;
}
Also used : BasicAuthClientCredentials(com.helger.http.basicauth.BasicAuthClientCredentials) SMPUnauthorizedException(com.helger.phoss.smp.exception.SMPUnauthorizedException) Nonnull(javax.annotation.Nonnull)

Example 12 with BasicAuthClientCredentials

use of com.helger.http.basicauth.BasicAuthClientCredentials in project phoss-smp by phax.

the class APIExecutorExportAllXMLVer1 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 sLogPrefix = "[REST API Export-All-XML-V1] ";
    LOGGER.info(sLogPrefix + "Starting Export");
    // 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();
    // Now get all relevant service groups
    final ICommonsList<ISMPServiceGroup> aAllServiceGroups = aServiceGroupMgr.getAllSMPServiceGroups();
    final boolean bIncludeBusinessCards = aRequestScope.params().getAsBoolean(PARAM_INCLUDE_BUSINESS_CARDS, aSettingsMgr.getSettings().isDirectoryIntegrationEnabled());
    final IMicroDocument aDoc = ServiceGroupExport.createExportDataXMLVer10(aAllServiceGroups, 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) BasicAuthClientCredentials(com.helger.http.basicauth.BasicAuthClientCredentials) ISMPSettingsManager(com.helger.phoss.smp.settings.ISMPSettingsManager) IXMLWriterSettings(com.helger.xml.serialize.write.IXMLWriterSettings) IXMLWriterSettings(com.helger.xml.serialize.write.IXMLWriterSettings) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) ISMPServiceGroup(com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup) IMicroDocument(com.helger.xml.microdom.IMicroDocument) CMimeType(com.helger.commons.mime.CMimeType) MimeType(com.helger.commons.mime.MimeType)

Example 13 with BasicAuthClientCredentials

use of com.helger.http.basicauth.BasicAuthClientCredentials 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 14 with BasicAuthClientCredentials

use of com.helger.http.basicauth.BasicAuthClientCredentials 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)

Example 15 with BasicAuthClientCredentials

use of com.helger.http.basicauth.BasicAuthClientCredentials in project phoss-smp by phax.

the class APIExecutorBusinessCardPut 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. saveBusinessCard will not be executed", aDataProvider.getCurrentURI());
    }
    if (!SMPMetaManager.getSettings().isDirectoryIntegrationEnabled()) {
        // PD integration is disabled
        throw new SMPPreconditionFailedException("The " + SMPWebAppConfiguration.getDirectoryName() + " integration is disabled. saveBusinessCard will not be executed", aDataProvider.getCurrentURI());
    }
    // Parse main payload
    final byte[] aPayload = StreamHelper.getAllBytes(aRequestScope.getRequest().getInputStream());
    final PDBusinessCard aBC = PDBusinessCardHelper.parseBusinessCard(aPayload, (Charset) null);
    if (aBC == null) {
        // Cannot parse
        throw new SMPBadRequestException("Failed to parse XML payload as BusinessCard.", aDataProvider.getCurrentURI());
    }
    final BasicAuthClientCredentials aBasicAuth = getMandatoryAuth(aRequestScope.headers());
    final ESuccess eSuccess = new BusinessCardServerAPI(aDataProvider).createBusinessCard(sServiceGroupID, aBC, aBasicAuth);
    if (eSuccess.isFailure())
        aUnifiedResponse.setStatus(CHttp.HTTP_INTERNAL_SERVER_ERROR);
    else
        aUnifiedResponse.setStatus(CHttp.HTTP_OK);
}
Also used : ESuccess(com.helger.commons.state.ESuccess) SMPPreconditionFailedException(com.helger.phoss.smp.exception.SMPPreconditionFailedException) PDBusinessCard(com.helger.pd.businesscard.generic.PDBusinessCard) SMPBadRequestException(com.helger.phoss.smp.exception.SMPBadRequestException) BasicAuthClientCredentials(com.helger.http.basicauth.BasicAuthClientCredentials) BusinessCardServerAPI(com.helger.phoss.smp.restapi.BusinessCardServerAPI) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider)

Aggregations

BasicAuthClientCredentials (com.helger.http.basicauth.BasicAuthClientCredentials)26 ISMPServerAPIDataProvider (com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider)15 SMPPreconditionFailedException (com.helger.phoss.smp.exception.SMPPreconditionFailedException)12 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)10 ISMPServiceGroupManager (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager)8 SMPBadRequestException (com.helger.phoss.smp.exception.SMPBadRequestException)8 CMimeType (com.helger.commons.mime.CMimeType)7 MimeType (com.helger.commons.mime.MimeType)7 IMicroDocument (com.helger.xml.microdom.IMicroDocument)7 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)7 BDXR1ServerAPI (com.helger.phoss.smp.restapi.BDXR1ServerAPI)6 SMPServerAPI (com.helger.phoss.smp.restapi.SMPServerAPI)6 SMPClient (com.helger.smpclient.peppol.SMPClient)6 URI (java.net.URI)6 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)5 ISMPParticipantMigration (com.helger.phoss.smp.domain.pmigration.ISMPParticipantMigration)5 ISMPParticipantMigrationManager (com.helger.phoss.smp.domain.pmigration.ISMPParticipantMigrationManager)5 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)5 ISMPSettingsManager (com.helger.phoss.smp.settings.ISMPSettingsManager)4 IXMLWriterSettings (com.helger.xml.serialize.write.IXMLWriterSettings)4