
com.helger.peppol.bdxrclient.BDXRClient Maven / Gradle / Ivy
/**
* Copyright (C) 2015-2019 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* The Original Code is Copyright The PEPPOL project (http://www.peppol.eu)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.helger.peppol.bdxrclient;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nonnull;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.http.CHttpHeader;
import com.helger.commons.mime.CMimeType;
import com.helger.http.basicauth.BasicAuthClientCredentials;
import com.helger.peppol.bdxr.RedirectType;
import com.helger.peppol.bdxr.ServiceGroupType;
import com.helger.peppol.bdxr.ServiceInformationType;
import com.helger.peppol.bdxr.ServiceMetadataType;
import com.helger.peppol.bdxr.marshal.BDXRMarshallerServiceGroupType;
import com.helger.peppol.bdxr.marshal.BDXRMarshallerServiceMetadataType;
import com.helger.peppol.httpclient.SMPHttpResponseHandlerWriteOperations;
import com.helger.peppol.identifier.bdxr.participant.BDXRParticipantIdentifier;
import com.helger.peppol.identifier.generic.doctype.IDocumentTypeIdentifier;
import com.helger.peppol.identifier.generic.participant.IParticipantIdentifier;
import com.helger.peppol.sml.ISMLInfo;
import com.helger.peppol.smpclient.exception.SMPClientBadRequestException;
import com.helger.peppol.smpclient.exception.SMPClientException;
import com.helger.peppol.smpclient.exception.SMPClientNotFoundException;
import com.helger.peppol.smpclient.exception.SMPClientUnauthorizedException;
import com.helger.peppol.url.IPeppolURLProvider;
import com.helger.peppol.url.PeppolDNSResolutionException;
/**
* This class is used for calling the BDXR SMP REST interface. This particular
* class also contains the non-standard writing methods. It inherits all reading
* methods from {@link BDXRClientReadOnly}.
*
* Note: this class is also licensed under Apache 2 license, as it was not part
* of the original implementation
*
*
* @author Philip Helger
*/
public class BDXRClient extends BDXRClientReadOnly
{
private static final Logger LOGGER = LoggerFactory.getLogger (BDXRClient.class);
// The default text/xml content type uses iso-8859-1!
private static final ContentType CONTENT_TYPE_TEXT_XML = ContentType.create (CMimeType.TEXT_XML.getAsString (),
StandardCharsets.UTF_8);
/**
* Constructor with SML lookup
*
* @param aURLProvider
* The URL provider to be used. May not be null
.
* @param aParticipantIdentifier
* The participant identifier to be used. Required to build the SMP
* access URI.
* @param aSMLInfo
* The SML to be used. Required to build the SMP access URI.
* @throws PeppolDNSResolutionException
* if DNS resolution fails
* @see IPeppolURLProvider#getSMPURIOfParticipant(IParticipantIdentifier,
* ISMLInfo)
*/
public BDXRClient (@Nonnull final IPeppolURLProvider aURLProvider,
@Nonnull final IParticipantIdentifier aParticipantIdentifier,
@Nonnull final ISMLInfo aSMLInfo) throws PeppolDNSResolutionException
{
super (aURLProvider, aParticipantIdentifier, aSMLInfo);
}
/**
* Constructor with SML lookup
*
* @param aURLProvider
* The URL provider to be used. May not be null
.
* @param aParticipantIdentifier
* The participant identifier to be used. Required to build the SMP
* access URI.
* @param sSMLZoneName
* The SML DNS zone name to be used. Required to build the SMP access
* URI. Must end with a trailing dot (".") and may neither be
* null
nor empty to build a correct URL. May not start
* with "http://". Example: sml.peppolcentral.org.
* @throws PeppolDNSResolutionException
* if DNS resolution fails
* @see IPeppolURLProvider#getSMPURIOfParticipant(IParticipantIdentifier,
* String)
*/
public BDXRClient (@Nonnull final IPeppolURLProvider aURLProvider,
@Nonnull final IParticipantIdentifier aParticipantIdentifier,
@Nonnull @Nonempty final String sSMLZoneName) throws PeppolDNSResolutionException
{
super (aURLProvider, aParticipantIdentifier, sSMLZoneName);
}
/**
* Constructor with a direct SMP URL.
* Remember: must be HTTP and using port 80 only!
*
* @param aSMPHost
* The address of the SMP service. Must be port 80 and basic http only
* (no https!). Example: http://smpcompany.company.org
*/
public BDXRClient (@Nonnull final URI aSMPHost)
{
super (aSMPHost);
}
/**
* Saves a service group. The meta data references should not be set and are
* not used.
*
* @param aServiceGroup
* The service group to save.
* @param aCredentials
* The user name and password to use as aCredentials.
* @throws SMPClientException
* in case something goes wrong
* @throws SMPClientUnauthorizedException
* The user name or password was not correct.
* @throws SMPClientNotFoundException
* A HTTP Not Found was received. This can happen if the service was
* not found.
* @throws SMPClientBadRequestException
* The request was not well formed.
*/
public void saveServiceGroup (@Nonnull final ServiceGroupType aServiceGroup,
@Nonnull final BasicAuthClientCredentials aCredentials) throws SMPClientException
{
ValueEnforcer.notNull (aServiceGroup, "ServiceGroup");
ValueEnforcer.notNull (aCredentials, "Credentials");
final String sBody = new BDXRMarshallerServiceGroupType ().getAsString (aServiceGroup);
final String sURI = getSMPHostURI () + aServiceGroup.getParticipantIdentifier ().getURIPercentEncoded ();
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("BDXRClient saveServiceGroup@" + sURI);
final HttpPut aRequest = new HttpPut (sURI);
aRequest.addHeader (CHttpHeader.AUTHORIZATION, aCredentials.getRequestValue ());
aRequest.setEntity (new StringEntity (sBody, CONTENT_TYPE_TEXT_XML));
executeGenericRequest (aRequest, new SMPHttpResponseHandlerWriteOperations ());
}
/**
* Saves a service group. The meta data references should not be set and are
* not used.
*
* @param aParticipantID
* The participant identifier for which the service group is to save.
* @param aCredentials
* The user name and password to use as credentials.
* @return The created {@link ServiceGroupType} object.
* @throws SMPClientException
* in case something goes wrong
* @throws SMPClientUnauthorizedException
* The user name or password was not correct.
* @throws SMPClientNotFoundException
* A HTTP Not Found was received. This can happen if the service was
* not found.
* @throws SMPClientBadRequestException
* The request was not well formed.
*/
@Nonnull
public ServiceGroupType saveServiceGroup (@Nonnull final IParticipantIdentifier aParticipantID,
@Nonnull final BasicAuthClientCredentials aCredentials) throws SMPClientException
{
ValueEnforcer.notNull (aParticipantID, "ParticipantID");
ValueEnforcer.notNull (aCredentials, "Credentials");
final ServiceGroupType aServiceGroup = new ServiceGroupType ();
aServiceGroup.setParticipantIdentifier (new BDXRParticipantIdentifier (aParticipantID));
saveServiceGroup (aServiceGroup, aCredentials);
return aServiceGroup;
}
/**
* Deletes a service group given by its service group id.
*
* @param aServiceGroupID
* The service group id of the service group to delete.
* @param aCredentials
* The user name and password to use as aCredentials.
* @throws SMPClientException
* in case something goes wrong
* @throws SMPClientNotFoundException
* The service group id did not exist.
* @throws SMPClientUnauthorizedException
* The user name or password was not correct.
* @throws SMPClientBadRequestException
* The request was not well formed.
*/
public void deleteServiceGroup (@Nonnull final IParticipantIdentifier aServiceGroupID,
@Nonnull final BasicAuthClientCredentials aCredentials) throws SMPClientException
{
ValueEnforcer.notNull (aCredentials, "Credentials");
final String sURI = getSMPHostURI () + aServiceGroupID.getURIPercentEncoded ();
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("BDXRClient deleteServiceGroup@" + sURI);
final HttpDelete aRequest = new HttpDelete (sURI);
aRequest.addHeader (CHttpHeader.AUTHORIZATION, aCredentials.getRequestValue ());
executeGenericRequest (aRequest, new SMPHttpResponseHandlerWriteOperations ());
}
private void _saveServiceInformation (@Nonnull final IParticipantIdentifier aServiceGroupID,
@Nonnull final IDocumentTypeIdentifier aDocumentTypeID,
@Nonnull final ServiceMetadataType aServiceMetadata,
@Nonnull final BasicAuthClientCredentials aCredentials) throws SMPClientException
{
final String sBody = new BDXRMarshallerServiceMetadataType ().getAsString (aServiceMetadata);
final String sURI = getSMPHostURI () +
aServiceGroupID.getURIPercentEncoded () +
"/services/" +
aDocumentTypeID.getURIPercentEncoded ();
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("BDXRClient saveServiceRegistration@" + sURI);
final HttpPut aRequest = new HttpPut (sURI);
aRequest.addHeader (CHttpHeader.AUTHORIZATION, aCredentials.getRequestValue ());
aRequest.setEntity (new StringEntity (sBody, CONTENT_TYPE_TEXT_XML));
executeGenericRequest (aRequest, new SMPHttpResponseHandlerWriteOperations ());
}
/**
* Saves a service information data object.
*
* @param aServiceInformation
* The service information object to save.
* @param aCredentials
* The user name and password to use as aCredentials.
* @throws SMPClientException
* in case something goes wrong
* @throws SMPClientUnauthorizedException
* The user name or password was not correct.
* @throws SMPClientNotFoundException
* A HTTP Not Found was received. This can happen if the service was
* not found.
* @throws SMPClientBadRequestException
* The request was not well formed.
* @see #saveServiceRedirect(IParticipantIdentifier, IDocumentTypeIdentifier,
* RedirectType, BasicAuthClientCredentials)
*/
public void saveServiceInformation (@Nonnull final ServiceInformationType aServiceInformation,
@Nonnull final BasicAuthClientCredentials aCredentials) throws SMPClientException
{
ValueEnforcer.notNull (aServiceInformation, "ServiceMetadata.ServiceInformation");
ValueEnforcer.notNull (aServiceInformation.getParticipantIdentifier (),
"ServiceMetadata.ServiceInformation.ParticipantIdentifier");
ValueEnforcer.notNull (aServiceInformation.getDocumentIdentifier (),
"ServiceMetadata.ServiceInformation.DocumentIdentifier");
ValueEnforcer.notNull (aCredentials, "Credentials");
final ServiceMetadataType aServiceMetadata = new ServiceMetadataType ();
aServiceMetadata.setServiceInformation (aServiceInformation);
_saveServiceInformation (aServiceInformation.getParticipantIdentifier (),
aServiceInformation.getDocumentIdentifier (),
aServiceMetadata,
aCredentials);
}
/**
* Saves a redirect data object.
*
* @param aServiceGroupID
* The service group ID to use. May not be null
.
* @param aDocumentTypeID
* The document type ID to use. May not be null
.
* @param aRedirect
* The redirect to be saved. May not be null
.
* @param aCredentials
* The user name and password to use as aCredentials.
* @throws SMPClientException
* in case something goes wrong
* @throws SMPClientUnauthorizedException
* The user name or password was not correct.
* @throws SMPClientNotFoundException
* A HTTP Not Found was received. This can happen if the service was
* not found.
* @throws SMPClientBadRequestException
* The request was not well formed.
* @see #saveServiceInformation(ServiceInformationType,
* BasicAuthClientCredentials)
*/
public void saveServiceRedirect (@Nonnull final IParticipantIdentifier aServiceGroupID,
@Nonnull final IDocumentTypeIdentifier aDocumentTypeID,
@Nonnull final RedirectType aRedirect,
@Nonnull final BasicAuthClientCredentials aCredentials) throws SMPClientException
{
ValueEnforcer.notNull (aServiceGroupID, "ServiceGroupID");
ValueEnforcer.notNull (aDocumentTypeID, "DocumentTypeID");
ValueEnforcer.notNull (aRedirect, "Redirect");
ValueEnforcer.notNull (aCredentials, "Credentials");
final ServiceMetadataType aServiceMetadata = new ServiceMetadataType ();
aServiceMetadata.setRedirect (aRedirect);
_saveServiceInformation (aServiceGroupID, aDocumentTypeID, aServiceMetadata, aCredentials);
}
/**
* Deletes a service meta data object given by its service group id and its
* document type.
*
* @param aServiceGroupID
* The service group id of the service meta data to delete.
* @param aDocumentTypeID
* The document type of the service meta data to delete.
* @param aCredentials
* The user name and password to use as aCredentials.
* @throws SMPClientException
* in case something goes wrong
* @throws SMPClientUnauthorizedException
* The user name or password was not correct.
* @throws SMPClientNotFoundException
* The service meta data object did not exist.
* @throws SMPClientBadRequestException
* The request was not well formed.
*/
public void deleteServiceRegistration (@Nonnull final IParticipantIdentifier aServiceGroupID,
@Nonnull final IDocumentTypeIdentifier aDocumentTypeID,
@Nonnull final BasicAuthClientCredentials aCredentials) throws SMPClientException
{
ValueEnforcer.notNull (aServiceGroupID, "ServiceGroupID");
ValueEnforcer.notNull (aDocumentTypeID, "DocumentTypeID");
ValueEnforcer.notNull (aCredentials, "Credentials");
final String sURI = getSMPHostURI () +
aServiceGroupID.getURIPercentEncoded () +
"/services/" +
aDocumentTypeID.getURIPercentEncoded ();
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("BDXRClient deleteServiceRegistration@" + sURI);
final HttpDelete aRequest = new HttpDelete (sURI);
aRequest.addHeader (CHttpHeader.AUTHORIZATION, aCredentials.getRequestValue ());
executeGenericRequest (aRequest, new SMPHttpResponseHandlerWriteOperations ());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy