Search in sources :

Example 1 with IIdentifierFactory

use of com.helger.peppolid.factory.IIdentifierFactory in project phoss-directory by phax.

the class SMPBusinessCardProvider method getBusinessCardPeppolSMP.

@Nullable
@VisibleForTesting
PDExtendedBusinessCard getBusinessCardPeppolSMP(@Nonnull final IParticipantIdentifier aParticipantID, @Nonnull final SMPClientReadOnly aSMPClient, @Nonnull final HttpClientSettings aHCS) {
    LOGGER.info("Querying BusinessCard for '" + aParticipantID.getURIEncoded() + "' from Peppol SMP '" + aSMPClient.getSMPHostURI() + "'");
    // First query the service group
    com.helger.xsds.peppol.smp1.ServiceGroupType aServiceGroup;
    try {
        aServiceGroup = aSMPClient.getServiceGroupOrNull(aParticipantID);
    } catch (final SMPClientException ex) {
        LOGGER.error("Error querying SMP for ServiceGroup of '" + aParticipantID.getURIEncoded() + "'", ex);
        return null;
    }
    // If the service group is present, try querying the business card
    final PDBusinessCard aBusinessCard;
    try (final HttpClientManager aHCM = HttpClientManager.create(aHCS)) {
        // Use the optional business card API
        final HttpGet aRequest = new HttpGet(aSMPClient.getSMPHostURI() + "businesscard/" + aParticipantID.getURIPercentEncoded());
        aBusinessCard = aHCM.execute(aRequest, new PDSMPHttpResponseHandlerBusinessCard());
    } catch (final IOException ex) {
        if ((ex instanceof HttpResponseException && ((HttpResponseException) ex).getStatusCode() == CHttp.HTTP_NOT_FOUND) || ex instanceof UnknownHostException) {
            LOGGER.warn("No BusinessCard available for '" + aParticipantID.getURIEncoded() + "' - not in configured SMK/SML? - " + ex.getMessage());
        } else
            LOGGER.error("Error querying SMP for BusinessCard of '" + aParticipantID.getURIEncoded() + "'", ex);
        return null;
    }
    if (aBusinessCard == null) {
        // No extension present - no need to try again
        LOGGER.warn("Failed to get SMP BusinessCard of " + aParticipantID.getURIEncoded());
        return null;
    }
    // Query all document types
    final IIdentifierFactory aIdentifierFactory = PDMetaManager.getIdentifierFactory();
    final ICommonsList<IDocumentTypeIdentifier> aDocumentTypeIDs = SMPClientReadOnly.getAllDocumentTypes(aServiceGroup, aIdentifierFactory, UNHANDLED_HREF_HANDLER);
    return new PDExtendedBusinessCard(aBusinessCard, aDocumentTypeIDs);
}
Also used : PDBusinessCard(com.helger.pd.businesscard.generic.PDBusinessCard) UnknownHostException(java.net.UnknownHostException) HttpGet(org.apache.http.client.methods.HttpGet) IDocumentTypeIdentifier(com.helger.peppolid.IDocumentTypeIdentifier) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) HttpClientManager(com.helger.httpclient.HttpClientManager) SMPClientException(com.helger.smpclient.exception.SMPClientException) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) VisibleForTesting(com.helger.commons.annotation.VisibleForTesting) Nullable(javax.annotation.Nullable)

Example 2 with IIdentifierFactory

use of com.helger.peppolid.factory.IIdentifierFactory in project phoss-directory by phax.

the class PageSecureParticipantActions method _deleteDuplicateIDs.

private void _deleteDuplicateIDs(@Nonnull final WebPageExecutionContext aWPEC) {
    LOGGER.info("Deleting all duplicate participant identifiers");
    final Locale aDisplayLocale = aWPEC.getDisplayLocale();
    final ICommonsMap<IParticipantIdentifier, ICommonsSortedSet<String>> aDupMap = _getDuplicateSourceMap();
    final ICommonsSortedSet<String> aPIsToDelete = new CommonsTreeSet<>();
    final ICommonsSortedSet<String> aPIsToAdd = new CommonsTreeSet<>();
    for (final Map.Entry<IParticipantIdentifier, ICommonsSortedSet<String>> aEntry : aDupMap.entrySet()) {
        final ICommonsSortedSet<String> aSet = aEntry.getValue();
        final IParticipantIdentifier aPI = aEntry.getKey();
        final String sDesiredVersion = aPI.getURIEncoded();
        if (aSet.contains(sDesiredVersion)) {
            // Simple kill the other ones
            aPIsToDelete.addAll(aSet, x -> !x.equals(sDesiredVersion));
        } else {
            // Remove all and index the correct version
            aPIsToDelete.addAll(aSet);
            aPIsToAdd.add(sDesiredVersion);
        }
    }
    if (aPIsToDelete.isNotEmpty()) {
        final HCNodeList aNL = new HCNodeList();
        // Important to use this identifier factory so that the correct key is
        // created
        final IIdentifierFactory aIF = SimpleIdentifierFactory.INSTANCE;
        final PDIndexerManager aIndexerMgr = PDMetaManager.getIndexerMgr();
        String sMsg = "Deleting " + aPIsToDelete.size() + " participant ID(s):";
        LOGGER.info(sMsg);
        aNL.addChild(h2(sMsg));
        HCOL aOL = aNL.addAndReturnChild(new HCOL());
        for (final String s : aPIsToDelete.getSorted(IComparator.getComparatorCollating(aDisplayLocale))) {
            aOL.addItem(s);
            aIndexerMgr.queueWorkItem(aIF.parseParticipantIdentifier(s), EIndexerWorkItemType.DELETE, "duplicate-elimination", PDIndexerManager.HOST_LOCALHOST);
        }
        if (aPIsToAdd.isNotEmpty()) {
            sMsg = "Adding " + aPIsToAdd.size() + " participant ID(s) instead:";
            LOGGER.info(sMsg);
            aNL.addChild(h2(sMsg));
            aOL = aNL.addAndReturnChild(new HCOL());
            for (final String s : aPIsToAdd.getSorted(IComparator.getComparatorCollating(aDisplayLocale))) {
                aOL.addItem(s);
                aIndexerMgr.queueWorkItem(aIF.parseParticipantIdentifier(s), EIndexerWorkItemType.CREATE_UPDATE, "duplicate-elimination", PDIndexerManager.HOST_LOCALHOST);
            }
        }
        aWPEC.postRedirectGetInternal(aNL);
    } else {
        final String sMsg = "Found no duplicate entries to remove";
        LOGGER.info(sMsg);
        aWPEC.postRedirectGetInternal(success(sMsg));
    }
}
Also used : Locale(java.util.Locale) HCNodeList(com.helger.html.hc.impl.HCNodeList) CommonsTreeSet(com.helger.commons.collection.impl.CommonsTreeSet) PDTToString(com.helger.commons.datetime.PDTToString) ICommonsSortedSet(com.helger.commons.collection.impl.ICommonsSortedSet) HCOL(com.helger.html.hc.html.grouping.HCOL) Map(java.util.Map) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) PDIndexerManager(com.helger.pd.indexer.mgr.PDIndexerManager) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier)

Example 3 with IIdentifierFactory

use of com.helger.peppolid.factory.IIdentifierFactory in project phoss-directory by phax.

the class PageSecureDeleteManually method fillContent.

@Override
protected void fillContent(final WebPageExecutionContext aWPEC) {
    final HCNodeList aNodeList = aWPEC.getNodeList();
    final IIdentifierFactory aIdentifierFactory = PDMetaManager.getIdentifierFactory();
    final FormErrorList aFormErrors = new FormErrorList();
    if (aWPEC.hasAction(CPageParam.ACTION_PERFORM)) {
        final String sParticipantID = aWPEC.params().getAsString(FIELD_PARTICIPANT_ID);
        final IParticipantIdentifier aParticipantID = aIdentifierFactory.parseParticipantIdentifier(sParticipantID);
        if (StringHelper.hasNoText(sParticipantID))
            aFormErrors.addFieldError(FIELD_PARTICIPANT_ID, "A participant ID must be provided.");
        else if (aParticipantID == null)
            aFormErrors.addFieldError(FIELD_PARTICIPANT_ID, "The provided participant ID is syntactically invalid.");
        if (aFormErrors.isEmpty()) {
            int nDeleted = 0;
            try {
                nDeleted = PDMetaManager.getStorageMgr().deleteEntry(aParticipantID, null, false);
            } catch (final IOException ex) {
                // ignore
                nDeleted = -1;
            }
            if (nDeleted > 0)
                aWPEC.postRedirectGetInternal(success("The participant ID '" + aParticipantID.getURIEncoded() + "' was deleted (" + nDeleted + " rows)"));
            else
                aWPEC.postRedirectGetInternal(error("Error deleting participant ID '" + aParticipantID.getURIEncoded() + "'"));
        }
    }
    final BootstrapForm aForm = aNodeList.addAndReturnChild(getUIHandler().createFormSelf(aWPEC));
    aForm.addFormGroup(new BootstrapFormGroup().setLabelMandatory("Participant ID").setCtrl(new HCEdit(new RequestField(FIELD_PARTICIPANT_ID, PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME + CIdentifier.URL_SCHEME_VALUE_SEPARATOR))).setHelpText(span().addChild("Enter the fully qualified Peppol participant ID (including the scheme) you want to delete.\nExample identifier layout: ").addChild(code(aIdentifierFactory.createParticipantIdentifier(PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME, "9999:test").getURIEncoded()))).setErrorList(aFormErrors.getListOfField(FIELD_PARTICIPANT_ID)));
    final BootstrapButtonToolbar aToolbar = aForm.addAndReturnChild(new BootstrapButtonToolbar(aWPEC));
    aToolbar.addHiddenField(CPageParam.PARAM_ACTION, CPageParam.ACTION_PERFORM);
    aToolbar.addSubmitButton("Delete from index", EDefaultIcon.DELETE);
}
Also used : HCNodeList(com.helger.html.hc.impl.HCNodeList) FormErrorList(com.helger.photon.core.form.FormErrorList) HCEdit(com.helger.html.hc.html.forms.HCEdit) IOException(java.io.IOException) BootstrapButtonToolbar(com.helger.photon.bootstrap4.buttongroup.BootstrapButtonToolbar) BootstrapFormGroup(com.helger.photon.bootstrap4.form.BootstrapFormGroup) IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) BootstrapForm(com.helger.photon.bootstrap4.form.BootstrapForm) RequestField(com.helger.photon.core.form.RequestField)

Example 4 with IIdentifierFactory

use of com.helger.peppolid.factory.IIdentifierFactory in project phoss-directory by phax.

the class IndexerResource method deleteParticipant.

@DELETE
@Path("{participantID}")
public Response deleteParticipant(@Context @Nonnull final HttpServletRequest aHttpServletRequest, @PathParam("participantID") @Nonnull final String sParticipantID) {
    final String sLogPrefix = "[deleteParticipant] ";
    final ClientCertificateValidationResult aResult = _checkClientCertificate(aHttpServletRequest, sLogPrefix);
    if (aResult.isFailure())
        return Response.status(Response.Status.FORBIDDEN).build();
    final String sRealParticipantID = _unifyPID(sParticipantID);
    if (LOGGER.isInfoEnabled())
        LOGGER.info(sLogPrefix + "'" + sRealParticipantID + "'");
    // Parse identifier
    final IIdentifierFactory aIdentifierFactory = PDMetaManager.getIdentifierFactory();
    final IParticipantIdentifier aPI = aIdentifierFactory.parseParticipantIdentifier(sRealParticipantID);
    if (aPI == null) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error(sLogPrefix + "Failed to parse participant identifier '" + sRealParticipantID + "'");
        return Response.status(Status.BAD_REQUEST).build();
    }
    // Queue for handling
    if (PDMetaManager.getIndexerMgr().queueWorkItem(aPI, EIndexerWorkItemType.DELETE, aResult.getClientID(), _getRequestingHost(aHttpServletRequest)).isUnchanged()) {
        if (LOGGER.isInfoEnabled())
            LOGGER.info(sLogPrefix + "Ignoring duplicate DELETE request for '" + aPI.getURIEncoded() + "'");
    }
    // And done
    return Response.noContent().build();
}
Also used : IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) ClientCertificateValidationResult(com.helger.pd.indexer.clientcert.ClientCertificateValidationResult) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 5 with IIdentifierFactory

use of com.helger.peppolid.factory.IIdentifierFactory in project phoss-directory by phax.

the class IndexerResource method createOrUpdateParticipant.

@PUT
public Response createOrUpdateParticipant(@Context @Nonnull final HttpServletRequest aHttpServletRequest, @Nonnull final String sParticipantID) {
    final String sLogPrefix = "[createOrUpdateParticipant] ";
    final ClientCertificateValidationResult aResult = _checkClientCertificate(aHttpServletRequest, sLogPrefix);
    if (aResult.isFailure())
        return Response.status(Response.Status.FORBIDDEN).build();
    final String sRealParticipantID = _unifyPID(sParticipantID);
    if (LOGGER.isInfoEnabled())
        LOGGER.info(sLogPrefix + "'" + sRealParticipantID + "'");
    // Parse identifier
    final IIdentifierFactory aIdentifierFactory = PDMetaManager.getIdentifierFactory();
    final IParticipantIdentifier aPI = aIdentifierFactory.parseParticipantIdentifier(sRealParticipantID);
    if (aPI == null) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error(sLogPrefix + "Failed to parse participant identifier '" + sRealParticipantID + "'");
        return Response.status(Status.BAD_REQUEST).build();
    }
    // Queue for handling
    if (PDMetaManager.getIndexerMgr().queueWorkItem(aPI, EIndexerWorkItemType.CREATE_UPDATE, aResult.getClientID(), _getRequestingHost(aHttpServletRequest)).isUnchanged()) {
        if (LOGGER.isInfoEnabled())
            LOGGER.info(sLogPrefix + "Ignoring duplicate CREATE/UPDATE request for '" + aPI.getURIEncoded() + "'");
    }
    // And done
    return Response.noContent().build();
}
Also used : IIdentifierFactory(com.helger.peppolid.factory.IIdentifierFactory) ClientCertificateValidationResult(com.helger.pd.indexer.clientcert.ClientCertificateValidationResult) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) PUT(javax.ws.rs.PUT)

Aggregations

IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)82 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)56 ISMPServiceGroupManager (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager)34 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)33 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)29 SMPServerException (com.helger.phoss.smp.exception.SMPServerException)22 IUser (com.helger.photon.security.user.IUser)20 Test (org.junit.Test)19 Nonnull (javax.annotation.Nonnull)18 ISMPServiceInformationManager (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformationManager)17 SMPNotFoundException (com.helger.phoss.smp.exception.SMPNotFoundException)16 ISMPRedirectManager (com.helger.phoss.smp.domain.redirect.ISMPRedirectManager)15 ISMPServiceInformation (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformation)13 SMPBadRequestException (com.helger.phoss.smp.exception.SMPBadRequestException)13 Locale (java.util.Locale)11 Nullable (javax.annotation.Nullable)11 HCNodeList (com.helger.html.hc.impl.HCNodeList)9 ISMPRedirect (com.helger.phoss.smp.domain.redirect.ISMPRedirect)9 BootstrapFormGroup (com.helger.photon.bootstrap4.form.BootstrapFormGroup)9 ISMPServerAPIDataProvider (com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider)8