Search in sources :

Example 1 with ResourceException

use of org.onehippo.cms7.crisp.api.resource.ResourceException in project hippo by NHS-digital-website.

the class SubscriptionResource method subscribe.

public String subscribe(final String emailAddress, final List<String> topicCodes) {
    final Subscriber subscriber = SubscriberFactory.create(emailAddress, topicCodes);
    try {
        final ResourceServiceBroker broker = CrispHstServices.getDefaultResourceServiceBroker(HstServices.getComponentManager());
        final Map<String, Object> pathVars = new HashMap<>();
        pathVars.put("account", ACCOUNT);
        final ExchangeHint exchangeHint = ExchangeHintBuilder.create().methodName("POST").requestHeader("Content-Type", "application/xml").requestBody(XML_MAPPER.writeValueAsString(subscriber)).build();
        final Resource resource = broker.resolve("govDeliveryApi", URL, pathVars, exchangeHint);
        return (String) ((Resource) resource.getValueMap().get("to-param")).getDefaultValue();
    } catch (final JsonProcessingException e) {
        LOGGER.error("Encountered exception when serializing XML.", e);
    } catch (final ResourceException e) {
        LOGGER.error("Encountered communication exception when calling the API.", e);
    } catch (final Exception e) {
        LOGGER.error("Encountered unexpected exception when subscribing user.", e);
    }
    return null;
}
Also used : Subscriber(uk.nhs.digital.common.forms.model.Subscriber) HashMap(java.util.HashMap) ExchangeHint(org.onehippo.cms7.crisp.api.exchange.ExchangeHint) Resource(org.onehippo.cms7.crisp.api.resource.Resource) ResourceException(org.onehippo.cms7.crisp.api.resource.ResourceException) ResourceServiceBroker(org.onehippo.cms7.crisp.api.broker.ResourceServiceBroker) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ResourceException(org.onehippo.cms7.crisp.api.resource.ResourceException)

Example 2 with ResourceException

use of org.onehippo.cms7.crisp.api.resource.ResourceException in project hippo by NHS-digital-website.

the class SubscriptionResourceTest method setUp.

@Before
public void setUp() {
    final Map<String, ResourceResolver> resourceResolverMap = new HashMap<>();
    resourceResolverMap.put("govDeliveryApi", new MockJdomResourceResolverAdapter() {

        @Override
        public Resource resolve(String absPath, Map<String, Object> pathVariables, ExchangeHint exchangeHint) throws ResourceException {
            return urlToResource(SubscriptionResourceTest.class.getResource("/subscriber-output.xml"));
        }
    });
    final ResourceServiceBroker mockBroker = new MockResourceServiceBroker(resourceResolverMap);
    MockCrispHstServices.setDefaultResourceServiceBroker(mockBroker);
}
Also used : MockJdomResourceResolverAdapter(org.onehippo.cms7.crisp.mock.resource.MockJdomResourceResolverAdapter) MockResourceServiceBroker(org.onehippo.cms7.crisp.mock.broker.MockResourceServiceBroker) HashMap(java.util.HashMap) Resource(org.onehippo.cms7.crisp.api.resource.Resource) MockResourceServiceBroker(org.onehippo.cms7.crisp.mock.broker.MockResourceServiceBroker) ResourceServiceBroker(org.onehippo.cms7.crisp.api.broker.ResourceServiceBroker) ExchangeHint(org.onehippo.cms7.crisp.api.exchange.ExchangeHint) ResourceResolver(org.onehippo.cms7.crisp.api.resource.ResourceResolver) ResourceException(org.onehippo.cms7.crisp.api.resource.ResourceException) Before(org.junit.Before)

Aggregations

HashMap (java.util.HashMap)2 ResourceServiceBroker (org.onehippo.cms7.crisp.api.broker.ResourceServiceBroker)2 ExchangeHint (org.onehippo.cms7.crisp.api.exchange.ExchangeHint)2 Resource (org.onehippo.cms7.crisp.api.resource.Resource)2 ResourceException (org.onehippo.cms7.crisp.api.resource.ResourceException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Before (org.junit.Before)1 ResourceResolver (org.onehippo.cms7.crisp.api.resource.ResourceResolver)1 MockResourceServiceBroker (org.onehippo.cms7.crisp.mock.broker.MockResourceServiceBroker)1 MockJdomResourceResolverAdapter (org.onehippo.cms7.crisp.mock.resource.MockJdomResourceResolverAdapter)1 Subscriber (uk.nhs.digital.common.forms.model.Subscriber)1