Skip to content

Commit

Permalink
Added new REST API to push BusinessCards manually; #269
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Aug 12, 2024
1 parent 5674574 commit 22f0ad7
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (C) 2014-2024 Philip Helger and contributors
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.phoss.smp.rest;

import java.util.Map;

import javax.annotation.Nonnull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.helger.commons.annotation.Nonempty;
import com.helger.commons.http.CHttp;
import com.helger.peppolid.IParticipantIdentifier;
import com.helger.peppolid.factory.IIdentifierFactory;
import com.helger.phoss.smp.app.PDClientProvider;
import com.helger.phoss.smp.app.SMPWebAppConfiguration;
import com.helger.phoss.smp.domain.SMPMetaManager;
import com.helger.phoss.smp.domain.businesscard.ISMPBusinessCardManager;
import com.helger.phoss.smp.domain.user.SMPUserManagerPhoton;
import com.helger.phoss.smp.exception.SMPBadRequestException;
import com.helger.phoss.smp.exception.SMPInternalErrorException;
import com.helger.phoss.smp.exception.SMPPreconditionFailedException;
import com.helger.phoss.smp.exception.SMPServerException;
import com.helger.phoss.smp.restapi.BusinessCardServerAPI;
import com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider;
import com.helger.phoss.smp.restapi.SMPAPICredentials;
import com.helger.photon.api.IAPIDescriptor;
import com.helger.photon.security.user.IUser;
import com.helger.servlet.response.UnifiedResponse;
import com.helger.web.scope.IRequestWebScopeWithoutResponse;

public final class APIExecutorBusinessCardPush extends AbstractSMPAPIExecutor
{
private static final Logger LOGGER = LoggerFactory.getLogger (APIExecutorBusinessCardPush.class);

private static void _pushBusinessCard (@Nonnull final ISMPServerAPIDataProvider aDataProvider,
@Nonnull final String sServiceGroupID,
@Nonnull final SMPAPICredentials aCredentials) throws SMPServerException
{
final String sLog = BusinessCardServerAPI.LOG_PREFIX + "POST /businesscard/" + sServiceGroupID + "/push";
final String sAction = "pushBusinessCard";

LOGGER.info (sLog);
BusinessCardServerAPI.STATS_COUNTER_INVOCATION.increment (sAction);
try
{
final IIdentifierFactory aIdentifierFactory = SMPMetaManager.getIdentifierFactory ();
final IParticipantIdentifier aServiceGroupID = aIdentifierFactory.parseParticipantIdentifier (sServiceGroupID);
if (aServiceGroupID == null)
{
// Invalid identifier
throw SMPBadRequestException.failedToParseSG (sServiceGroupID, aDataProvider.getCurrentURI ());
}
final IUser aSMPUser = SMPUserManagerPhoton.validateUserCredentials (aCredentials);
SMPUserManagerPhoton.verifyOwnership (aServiceGroupID, aSMPUser);

final ISMPBusinessCardManager aBusinessCardMgr = SMPMetaManager.getBusinessCardMgr ();
if (aBusinessCardMgr == null)
{
throw new SMPBadRequestException ("This SMP server does not support the Business Card API",
aDataProvider.getCurrentURI ());
}

// Only if a business card is present
if (!aBusinessCardMgr.containsSMPBusinessCardOfID (aServiceGroupID))
throw new SMPBadRequestException ("The provided Service Group ID '" +
sServiceGroupID +
"' has no BusinessCard on this SMP",
aDataProvider.getCurrentURI ());

// Notify PD server: update
if (PDClientProvider.getInstance ().getPDClient ().addServiceGroupToIndex (aServiceGroupID).isFailure ())
throw new SMPInternalErrorException ("Failed to inform the Directory to index '" +
sServiceGroupID +
"' - see server log file for details");

LOGGER.info (sLog + " SUCCESS");
BusinessCardServerAPI.STATS_COUNTER_SUCCESS.increment (sAction);
}
catch (final SMPServerException ex)
{
LOGGER.warn (sLog + " ERROR - " + ex.getMessage ());
BusinessCardServerAPI.STATS_COUNTER_ERROR.increment (sAction);
throw ex;
}
}

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);

if (!SMPMetaManager.getSettings ().isDirectoryIntegrationEnabled ())
{
// PD integration is disabled
throw new SMPPreconditionFailedException ("The " +
SMPWebAppConfiguration.getDirectoryName () +
" integration is disabled. pushBusinessCard will not be executed",
aDataProvider.getCurrentURI ());
}

// Parse main payload
final SMPAPICredentials aCredentials = getMandatoryAuth (aRequestScope.headers ());

_pushBusinessCard (aDataProvider, sServiceGroupID, aCredentials);
aUnifiedResponse.setStatus (CHttp.HTTP_OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,15 @@ public SMPRestFilter ()
final APIDescriptor aGetBusinessCard = new APIDescriptor (APIPath.get (PATH_BUSINESSCARD +
"{" +
PARAM_SERVICE_GROUP_ID +
"}"),
new APIExecutorBusinessCardGet ());
"}"), new APIExecutorBusinessCardGet ());
aGetBusinessCard.setExceptionMapper (aExceptionMapper);
aAPIRegistry.registerAPI (aGetBusinessCard);
}
{
final APIDescriptor aPutBusinessCard = new APIDescriptor (APIPath.put (PATH_BUSINESSCARD +
"{" +
PARAM_SERVICE_GROUP_ID +
"}"),
new APIExecutorBusinessCardPut ());
"}"), new APIExecutorBusinessCardPut ());
aPutBusinessCard.allowedMimeTypes ()
.addAll (CMimeType.TEXT_XML.getAsString (), CMimeType.APPLICATION_XML.getAsString ());
aPutBusinessCard.setExceptionMapper (aExceptionMapper);
Expand All @@ -116,6 +114,16 @@ public SMPRestFilter ()
aDeleteBusinessCard.setExceptionMapper (aExceptionMapper);
aAPIRegistry.registerAPI (aDeleteBusinessCard);
}
// Push BusinessCard
{
final APIDescriptor aPushBusinessCard = new APIDescriptor (APIPath.post (PATH_BUSINESSCARD +
"{" +
PARAM_SERVICE_GROUP_ID +
"}/push"),
new APIExecutorBusinessCardPush ());
aPushBusinessCard.setExceptionMapper (aExceptionMapper);
aAPIRegistry.registerAPI (aPushBusinessCard);
}
// CompleteServiceGroup
{
final APIDescriptor aGetCompleteServiceGroup = new APIDescriptor (APIPath.get (PATH_COMPLETE +
Expand All @@ -138,8 +146,7 @@ public SMPRestFilter ()
final APIDescriptor aGetServiceGroup = new APIDescriptor (APIPath.get (sQueryPathPrefix +
"/{" +
PARAM_SERVICE_GROUP_ID +
"}"),
new APIExecutorServiceGroupGet ());
"}"), new APIExecutorServiceGroupGet ());
aGetServiceGroup.setExceptionMapper (aExceptionMapper);
aAPIRegistry.registerAPI (aGetServiceGroup);
}
Expand Down Expand Up @@ -222,8 +229,7 @@ public SMPRestFilter ()
{
final APIDescriptor aSMPQueryDocTypes = new APIDescriptor (APIPath.get ("/smpquery/{" +
PARAM_SERVICE_GROUP_ID +
"}"),
new APIExecutorQueryGetDocTypes ());
"}"), new APIExecutorQueryGetDocTypes ());
aSMPQueryDocTypes.setExceptionMapper (aExceptionMapper);
aAPIRegistry.registerAPI (aSMPQueryDocTypes);
}
Expand Down Expand Up @@ -273,8 +279,7 @@ public SMPRestFilter ()
{
final APIDescriptor aSMPImportAll = new APIDescriptor (APIPath.put ("/exchange/import/xml/v1/{" +
PARAM_USER_ID +
"}"),
new APIExecutorImportXMLVer1 ());
"}"), new APIExecutorImportXMLVer1 ());
aSMPImportAll.setExceptionMapper (aExceptionMapper);
aAPIRegistry.registerAPI (aSMPImportAll);
}
Expand Down

0 comments on commit 22f0ad7

Please sign in to comment.