Search in sources :

Example 1 with SMPMarshallerServiceGroupType

use of com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType in project peppol-commons by phax.

the class SMPClient method saveServiceGroup.

/**
 * Saves a service group. The meta data references should not be set and are
 * not used.
 *
 * @param aServiceGroup
 *        The service group to save. May not be <code>null</code>.
 * @param aCredentials
 *        The user name and password to use as credentials. May not be
 *        <code>null</code>.
 * @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 SMPMarshallerServiceGroupType(isXMLSchemaValidation()).getAsString(aServiceGroup);
    if (sBody == null)
        throw new IllegalArgumentException("Failed to serialize ServiceGroup: " + aServiceGroup);
    final String sURI = getSMPHostURI() + CIdentifier.getURIPercentEncoded(aServiceGroup.getParticipantIdentifier());
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("SMPClient 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());
}
Also used : StringEntity(org.apache.http.entity.StringEntity) SMPMarshallerServiceGroupType(com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType) SMPHttpResponseHandlerWriteOperations(com.helger.smpclient.httpclient.SMPHttpResponseHandlerWriteOperations) HttpPut(org.apache.http.client.methods.HttpPut)

Example 2 with SMPMarshallerServiceGroupType

use of com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType in project peppol-commons by phax.

the class SMPClientReadOnly method getServiceGroup.

/**
 * Returns a service group. A service group references to the service
 * metadata. This is a specification compliant method.
 *
 * @param aServiceGroupID
 *        The service group id corresponding to the service group which one
 *        wants to get.
 * @return The service group. Never <code>null</code>.
 * @throws SMPClientException
 *         in case something goes wrong
 * @throws SMPClientUnauthorizedException
 *         A HTTP Forbidden was received, should not happen.
 * @throws SMPClientNotFoundException
 *         The service group id did not exist.
 * @throws SMPClientBadRequestException
 *         The request was not well formed.
 * @see #getServiceGroupOrNull(IParticipantIdentifier)
 */
@Nonnull
public ServiceGroupType getServiceGroup(@Nonnull final IParticipantIdentifier aServiceGroupID) throws SMPClientException {
    ValueEnforcer.notNull(aServiceGroupID, "ServiceGroupID");
    final String sURI = getSMPHostURI() + aServiceGroupID.getURIPercentEncoded();
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("SMPClient getServiceGroup@" + sURI);
    final HttpGet aRequest = new HttpGet(sURI);
    final SMPMarshallerServiceGroupType aMarshaller = new SMPMarshallerServiceGroupType(isXMLSchemaValidation());
    customizeMarshaller(aMarshaller);
    final ServiceGroupType ret = executeGenericRequest(aRequest, new SMPHttpResponseHandlerUnsigned<>(aMarshaller));
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Received response: " + ret);
    return ret;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) SMPMarshallerServiceGroupType(com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType) SMPMarshallerCompleteServiceGroupType(com.helger.smpclient.peppol.marshal.SMPMarshallerCompleteServiceGroupType) ServiceGroupType(com.helger.xsds.peppol.smp1.ServiceGroupType) SMPMarshallerServiceGroupType(com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType) CompleteServiceGroupType(com.helger.xsds.peppol.smp1.CompleteServiceGroupType) Nonnull(javax.annotation.Nonnull)

Example 3 with SMPMarshallerServiceGroupType

use of com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType in project phoss-smp by phax.

the class SMPServiceGroupTest method testBasic.

@Test
public void testBasic() {
    final IParticipantIdentifier aPI = SMPMetaManager.getIdentifierFactory().createParticipantIdentifier(PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME, "0088:dummy");
    final SMPServiceGroup aSG = new SMPServiceGroup(CSecurity.USER_ADMINISTRATOR_ID, aPI, "<foobar />");
    assertTrue(StringHelper.hasText(aSG.getID()));
    assertEquals(CSecurity.USER_ADMINISTRATOR_ID, aSG.getOwnerID());
    assertEquals(aPI, aSG.getParticipantIdentifier());
    assertEquals("[{\"Any\":\"<foobar />\"}]", aSG.getExtensionsAsString());
    final com.helger.xsds.peppol.smp1.ServiceGroupType aSGPeppol = aSG.getAsJAXBObjectPeppol();
    assertNotNull(aSGPeppol.getExtension());
    aSGPeppol.setServiceMetadataReferenceCollection(new com.helger.xsds.peppol.smp1.ServiceMetadataReferenceCollectionType());
    final Document aDoc = new SMPMarshallerServiceGroupType(true).getAsDocument(aSGPeppol);
    assertNotNull(aDoc);
}
Also used : SMPMarshallerServiceGroupType(com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType) Document(org.w3c.dom.Document) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Test(org.junit.Test)

Example 4 with SMPMarshallerServiceGroupType

use of com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType in project phoss-smp by phax.

the class APIExecutorServiceGroupGet 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 ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, sPathServiceGroupID);
    final byte[] aBytes;
    switch(SMPServerConfiguration.getRESTType()) {
        case PEPPOL:
            {
                final com.helger.xsds.peppol.smp1.ServiceGroupType ret = new SMPServerAPI(aDataProvider).getServiceGroup(sPathServiceGroupID);
                aBytes = new SMPMarshallerServiceGroupType(XML_SCHEMA_VALIDATION).getAsBytes(ret);
                break;
            }
        case OASIS_BDXR_V1:
            {
                final com.helger.xsds.bdxr.smp1.ServiceGroupType ret = new BDXR1ServerAPI(aDataProvider).getServiceGroup(sPathServiceGroupID);
                aBytes = new BDXR1MarshallerServiceGroupType(XML_SCHEMA_VALIDATION).getAsBytes(ret);
                break;
            }
        default:
            throw new UnsupportedOperationException("Unsupported REST type specified!");
    }
    if (aBytes == null) {
        // Internal error serializing the payload
        throw new SMPInternalErrorException("Failed to convert the returned ServiceGroup to XML");
    }
    aUnifiedResponse.setContent(aBytes).setMimeType(CMimeType.TEXT_XML).setCharset(XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ);
}
Also used : SMPServerAPI(com.helger.phoss.smp.restapi.SMPServerAPI) BDXR1MarshallerServiceGroupType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerServiceGroupType) SMPMarshallerServiceGroupType(com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType) BDXR1ServerAPI(com.helger.phoss.smp.restapi.BDXR1ServerAPI) SMPInternalErrorException(com.helger.phoss.smp.exception.SMPInternalErrorException) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider) SMPMarshallerServiceGroupType(com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType) BDXR1MarshallerServiceGroupType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerServiceGroupType)

Example 5 with SMPMarshallerServiceGroupType

use of com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType in project phoss-smp by phax.

the class APIExecutorServiceGroupPut 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 ISMPServerAPIDataProvider aDataProvider = new SMPRestDataProvider(aRequestScope, sPathServiceGroupID);
    // Is the writable API disabled?
    if (SMPMetaManager.getSettings().isRESTWritableAPIDisabled()) {
        throw new SMPPreconditionFailedException("The writable REST API is disabled. saveServiceGroup will not be executed", aDataProvider.getCurrentURI());
    }
    // Parse main payload
    final byte[] aPayload = StreamHelper.getAllBytes(aRequestScope.getRequest().getInputStream());
    final Document aServiceGroupDoc = DOMReader.readXMLDOM(aPayload);
    if (aServiceGroupDoc == null) {
        throw new SMPBadRequestException("Failed to parse provided payload as XML", aDataProvider.getCurrentURI());
    }
    final BasicAuthClientCredentials aBasicAuth = getMandatoryAuth(aRequestScope.headers());
    final boolean bCreateInSML = !"false".equalsIgnoreCase(aRequestScope.params().getAsString("create-in-sml"));
    ESuccess eSuccess = ESuccess.FAILURE;
    switch(SMPServerConfiguration.getRESTType()) {
        case PEPPOL:
            {
                final com.helger.xsds.peppol.smp1.ServiceGroupType aServiceGroup = new SMPMarshallerServiceGroupType(XML_SCHEMA_VALIDATION).read(aServiceGroupDoc);
                if (aServiceGroup != null) {
                    new SMPServerAPI(aDataProvider).saveServiceGroup(sPathServiceGroupID, aServiceGroup, bCreateInSML, aBasicAuth);
                    eSuccess = ESuccess.SUCCESS;
                }
                break;
            }
        case OASIS_BDXR_V1:
            {
                final com.helger.xsds.bdxr.smp1.ServiceGroupType aServiceGroup = new BDXR1MarshallerServiceGroupType(XML_SCHEMA_VALIDATION).read(aServiceGroupDoc);
                if (aServiceGroup != null) {
                    new BDXR1ServerAPI(aDataProvider).saveServiceGroup(sPathServiceGroupID, aServiceGroup, bCreateInSML, aBasicAuth);
                    eSuccess = ESuccess.SUCCESS;
                }
                break;
            }
        default:
            throw new UnsupportedOperationException("Unsupported REST type specified!");
    }
    if (eSuccess.isFailure())
        aUnifiedResponse.setStatus(CHttp.HTTP_INTERNAL_SERVER_ERROR);
    else
        aUnifiedResponse.setStatus(CHttp.HTTP_OK).disableCaching();
}
Also used : ESuccess(com.helger.commons.state.ESuccess) SMPBadRequestException(com.helger.phoss.smp.exception.SMPBadRequestException) SMPServerAPI(com.helger.phoss.smp.restapi.SMPServerAPI) BDXR1MarshallerServiceGroupType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerServiceGroupType) SMPMarshallerServiceGroupType(com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType) BDXR1ServerAPI(com.helger.phoss.smp.restapi.BDXR1ServerAPI) Document(org.w3c.dom.Document) SMPPreconditionFailedException(com.helger.phoss.smp.exception.SMPPreconditionFailedException) BasicAuthClientCredentials(com.helger.http.basicauth.BasicAuthClientCredentials) ISMPServerAPIDataProvider(com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider) SMPMarshallerServiceGroupType(com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType) BDXR1MarshallerServiceGroupType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerServiceGroupType)

Aggregations

SMPMarshallerServiceGroupType (com.helger.smpclient.peppol.marshal.SMPMarshallerServiceGroupType)5 BDXR1ServerAPI (com.helger.phoss.smp.restapi.BDXR1ServerAPI)2 ISMPServerAPIDataProvider (com.helger.phoss.smp.restapi.ISMPServerAPIDataProvider)2 SMPServerAPI (com.helger.phoss.smp.restapi.SMPServerAPI)2 BDXR1MarshallerServiceGroupType (com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerServiceGroupType)2 Document (org.w3c.dom.Document)2 ESuccess (com.helger.commons.state.ESuccess)1 BasicAuthClientCredentials (com.helger.http.basicauth.BasicAuthClientCredentials)1 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)1 SMPBadRequestException (com.helger.phoss.smp.exception.SMPBadRequestException)1 SMPInternalErrorException (com.helger.phoss.smp.exception.SMPInternalErrorException)1 SMPPreconditionFailedException (com.helger.phoss.smp.exception.SMPPreconditionFailedException)1 SMPHttpResponseHandlerWriteOperations (com.helger.smpclient.httpclient.SMPHttpResponseHandlerWriteOperations)1 SMPMarshallerCompleteServiceGroupType (com.helger.smpclient.peppol.marshal.SMPMarshallerCompleteServiceGroupType)1 CompleteServiceGroupType (com.helger.xsds.peppol.smp1.CompleteServiceGroupType)1 ServiceGroupType (com.helger.xsds.peppol.smp1.ServiceGroupType)1 Nonnull (javax.annotation.Nonnull)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpPut (org.apache.http.client.methods.HttpPut)1 StringEntity (org.apache.http.entity.StringEntity)1