Search in sources :

Example 1 with PDBusinessCard

use of com.helger.pd.businesscard.generic.PDBusinessCard 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 PDBusinessCard

use of com.helger.pd.businesscard.generic.PDBusinessCard in project phoss-directory by phax.

the class PDSMPHttpResponseHandlerBusinessCard method handleEntity.

@Override
@Nullable
public PDBusinessCard handleEntity(@Nonnull final HttpEntity aEntity) throws IOException {
    // Read the payload and remember it!
    final ContentType aContentType = ContentType.getOrDefault(aEntity);
    final Charset aCharset = aContentType.getCharset();
    final byte[] aData = StreamHelper.getAllBytes(aEntity.getContent());
    if (aData == null)
        return null;
    final PDBusinessCard aBC = PDBusinessCardHelper.parseBusinessCard(aData, aCharset);
    if (aBC != null)
        return aBC;
    // Unsupported
    throw new ClientProtocolException("Malformed XML document returned from SMP server:\n" + new String(aData, StandardCharsets.UTF_8));
}
Also used : PDBusinessCard(com.helger.pd.businesscard.generic.PDBusinessCard) ContentType(org.apache.http.entity.ContentType) Charset(java.nio.charset.Charset) ClientProtocolException(org.apache.http.client.ClientProtocolException) Nullable(javax.annotation.Nullable)

Example 3 with PDBusinessCard

use of com.helger.pd.businesscard.generic.PDBusinessCard in project phoss-directory by phax.

the class PD2APIHelper method createBusinessCard.

@Nonnull
public static PDBusinessCard createBusinessCard(@Nonnull final PD2BusinessCardType aBC) {
    ValueEnforcer.notNull(aBC, "BusinessCard");
    final PDBusinessCard ret = new PDBusinessCard();
    ret.setParticipantIdentifier(createIdentifier(aBC.getParticipantIdentifier()));
    ret.businessEntities().setAllMapped(aBC.getBusinessEntity(), PD2APIHelper::createBusinessEntity);
    return ret;
}
Also used : PDBusinessCard(com.helger.pd.businesscard.generic.PDBusinessCard) Nonnull(javax.annotation.Nonnull)

Example 4 with PDBusinessCard

use of com.helger.pd.businesscard.generic.PDBusinessCard in project phoss-directory by phax.

the class PD3APIHelper method createBusinessCard.

@Nonnull
public static PDBusinessCard createBusinessCard(@Nonnull final PD3BusinessCardType aBC) {
    ValueEnforcer.notNull(aBC, "BusinessCard");
    final PDBusinessCard ret = new PDBusinessCard();
    ret.setParticipantIdentifier(createIdentifier(aBC.getParticipantIdentifier()));
    ret.businessEntities().setAllMapped(aBC.getBusinessEntity(), PD3APIHelper::createBusinessEntity);
    return ret;
}
Also used : PDBusinessCard(com.helger.pd.businesscard.generic.PDBusinessCard) Nonnull(javax.annotation.Nonnull)

Example 5 with PDBusinessCard

use of com.helger.pd.businesscard.generic.PDBusinessCard in project phoss-directory by phax.

the class ExportHelper method getAsXML.

@Nonnull
public static IMicroDocument getAsXML(@Nonnull final ICommonsOrderedMap<IParticipantIdentifier, ICommonsList<PDStoredBusinessEntity>> aMap, final boolean bIncludeDocTypes) {
    // XML root
    final IMicroDocument aDoc = new MicroDocument();
    final IMicroElement aRoot = aDoc.appendElement(XML_EXPORT_NS_URI, "root");
    aRoot.setAttribute("version", "2");
    aRoot.setAttribute("creationdt", PDTWebDateHelper.getAsStringXSD(PDTFactory.getCurrentZonedDateTimeUTC()));
    // For all BCs
    for (final Map.Entry<IParticipantIdentifier, ICommonsList<PDStoredBusinessEntity>> aEntry : aMap.entrySet()) {
        final IParticipantIdentifier aParticipantID = aEntry.getKey();
        final PDBusinessCard aBC = new PDBusinessCard();
        aBC.setParticipantIdentifier(new PDIdentifier(aParticipantID.getScheme(), aParticipantID.getValue()));
        for (final PDStoredBusinessEntity aSBE : aEntry.getValue()) aBC.businessEntities().add(aSBE.getAsBusinessEntity());
        final IMicroElement eBC = aBC.getAsMicroXML(XML_EXPORT_NS_URI, "businesscard");
        // New in v2 - add all Document types
        if (bIncludeDocTypes && aEntry.getValue().isNotEmpty())
            for (final IDocumentTypeIdentifier aDocTypeID : aEntry.getValue().getFirst().documentTypeIDs()) eBC.appendChild(_createMicroElement(aDocTypeID));
        aRoot.appendChild(eBC);
    }
    return aDoc;
}
Also used : PDIdentifier(com.helger.pd.businesscard.generic.PDIdentifier) IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) ICommonsList(com.helger.commons.collection.impl.ICommonsList) PDBusinessCard(com.helger.pd.businesscard.generic.PDBusinessCard) PDStoredBusinessEntity(com.helger.pd.indexer.storage.PDStoredBusinessEntity) IMicroElement(com.helger.xml.microdom.IMicroElement) IDocumentTypeIdentifier(com.helger.peppolid.IDocumentTypeIdentifier) IMicroDocument(com.helger.xml.microdom.IMicroDocument) ICommonsOrderedMap(com.helger.commons.collection.impl.ICommonsOrderedMap) Map(java.util.Map) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull)

Aggregations

PDBusinessCard (com.helger.pd.businesscard.generic.PDBusinessCard)17 Nonnull (javax.annotation.Nonnull)10 PDIdentifier (com.helger.pd.businesscard.generic.PDIdentifier)6 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)6 HttpClientManager (com.helger.httpclient.HttpClientManager)5 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)5 HttpGet (org.apache.http.client.methods.HttpGet)5 PDBusinessEntity (com.helger.pd.businesscard.generic.PDBusinessEntity)4 PDName (com.helger.pd.businesscard.generic.PDName)4 Nullable (javax.annotation.Nullable)4 VisibleForTesting (com.helger.commons.annotation.VisibleForTesting)3 PDExtendedBusinessCard (com.helger.pd.indexer.businesscard.PDExtendedBusinessCard)3 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)3 SMPBadRequestException (com.helger.phoss.smp.exception.SMPBadRequestException)3 SMPPreconditionFailedException (com.helger.phoss.smp.exception.SMPPreconditionFailedException)3 ISMPServerAPIDataProvider (com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider)3 SMPClientException (com.helger.smpclient.exception.SMPClientException)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 HttpResponseException (org.apache.http.client.HttpResponseException)3