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;
}
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();
}
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();
}
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);
}
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);
}
Aggregations