Search in sources :

Example 1 with InternalResourceUriException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException in project mule-coap-connector by teslanet-nl.

the class Listener method onStart.

@Override
public void onStart(SourceCallback<InputStream, CoAPRequestAttributes> sourceCallback) throws MuleException {
    try {
        operationalListener = new OperationalListener(pathPattern, new RequestCodeFlags(get, post, put, delete), sourceCallback);
        server.addListener(operationalListener);
    } catch (InternalResourceUriException | InternalUriPatternException e) {
        new DefaultMuleException(this + " start failed.", e);
    }
    logger.info(this + " started.");
}
Also used : DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) InternalResourceUriException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException) InternalUriPatternException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUriPatternException)

Example 2 with InternalResourceUriException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException in project mule-coap-connector by teslanet-nl.

the class OperationalListener method setUriPattern.

/**
 * Set the uri pattern of the resources the listener should process requests
 * for.
 *
 * @param uriPattern the uri pattern to set
 * @throws InternalResourceUriException when the uri pattern is not valid
 */
// TODO: make URIpattern class
public void setUriPattern(String uriPattern) throws InternalResourceUriException {
    // TODO assure no bad chars
    if (uriPattern == null)
        throw new InternalResourceUriException("null value is not allowed.");
    this.uriPattern = uriPattern.trim();
    if (!this.uriPattern.startsWith(Defs.COAP_URI_PATHSEP)) {
        this.uriPattern = Defs.COAP_URI_PATHSEP + this.uriPattern;
    }
    int wildcardIndex = this.uriPattern.indexOf(Defs.COAP_URI_WILDCARD);
    if (wildcardIndex >= 0 && wildcardIndex < this.uriPattern.length() - 1)
        throw new InternalResourceUriException("invalid uriPattern { " + uriPattern + " }, wildcard needs to be last character.");
    if (this.uriPattern.length() < 2)
        throw new InternalResourceUriException("invalid uriPattern { " + uriPattern + " }, uri cannot be empty.");
}
Also used : InternalResourceUriException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException)

Example 3 with InternalResourceUriException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException in project mule-coap-connector by teslanet-nl.

the class ResourceRegistryTest method testGetResourceNonexistent.

@Test
public void testGetResourceNonexistent() throws InternalResourceUriException, InternalResourceRegistryException {
    CoapResource root = new CoapResource("");
    ResourceRegistry registry = new ResourceRegistry("test_server", root);
    ResourceConfig resourceConfig;
    String name1 = "resource1";
    String uri1 = "/resource1";
    String name2 = "resource2";
    String uri2 = "/resource1/resource2";
    String name3 = "resource3";
    String uri4 = "/resource1/resource4";
    resourceConfig = new ResourceConfig();
    resourceConfig.setResourceName(name1);
    registry.add(null, resourceConfig);
    resourceConfig = new ResourceConfig();
    resourceConfig.setResourceName(name2);
    registry.add(uri1, resourceConfig);
    resourceConfig = new ResourceConfig();
    resourceConfig.setResourceName(name3);
    registry.add(uri2, resourceConfig);
    InternalResourceUriException e = assertThrows(InternalResourceUriException.class, () -> {
        registry.getResource(uri4).getURI();
    });
    assertTrue("exception has wrong message", e.getMessage().contains(uri4));
    assertTrue("exception has wrong message", e.getMessage().contains("does not exist"));
}
Also used : InternalResourceUriException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException) CoapResource(org.eclipse.californium.core.CoapResource) ResourceRegistry(nl.teslanet.mule.connectors.coap.internal.server.ResourceRegistry) ResourceConfig(nl.teslanet.mule.connectors.coap.api.ResourceConfig) Test(org.junit.Test)

Example 4 with InternalResourceUriException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException in project mule-coap-connector by teslanet-nl.

the class OperationalListenerTest method testConstructorWithInvalidUriEmpty1.

@Test
public void testConstructorWithInvalidUriEmpty1() throws InternalResourceUriException {
    TestSourceCallBack callback = new TestSourceCallBack();
    String uri = "";
    RequestCodeFlags flags = new RequestCodeFlags(true, true, true, false);
    InternalResourceUriException e = assertThrows(InternalResourceUriException.class, () -> {
        @SuppressWarnings("unused") OperationalListener listener = new OperationalListener(uri, new RequestCodeFlags(flags), callback);
    });
    assertEquals("exception has wrong message", "invalid uriPattern {  }, uri cannot be empty.", e.getMessage());
}
Also used : OperationalListener(nl.teslanet.mule.connectors.coap.internal.server.OperationalListener) RequestCodeFlags(nl.teslanet.mule.connectors.coap.internal.server.RequestCodeFlags) InternalResourceUriException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException) Test(org.junit.Test)

Example 5 with InternalResourceUriException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException in project mule-coap-connector by teslanet-nl.

the class OperationalListenerTest method testConstructorWithInvalidUriMultipleWildcard4.

@Test
public void testConstructorWithInvalidUriMultipleWildcard4() throws InternalResourceUriException {
    TestSourceCallBack callback = new TestSourceCallBack();
    String uri = "/some_resource/*/child/*";
    RequestCodeFlags flags = new RequestCodeFlags(true, true, true, false);
    InternalResourceUriException e = assertThrows(InternalResourceUriException.class, () -> {
        new OperationalListener(uri, new RequestCodeFlags(flags), callback);
    });
    assertEquals("exception has wrong message", "invalid uriPattern { /some_resource/*/child/* }, wildcard needs to be last character.", e.getMessage());
}
Also used : OperationalListener(nl.teslanet.mule.connectors.coap.internal.server.OperationalListener) RequestCodeFlags(nl.teslanet.mule.connectors.coap.internal.server.RequestCodeFlags) InternalResourceUriException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException) Test(org.junit.Test)

Aggregations

InternalResourceUriException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException)13 Test (org.junit.Test)10 OperationalListener (nl.teslanet.mule.connectors.coap.internal.server.OperationalListener)9 RequestCodeFlags (nl.teslanet.mule.connectors.coap.internal.server.RequestCodeFlags)9 ResourceConfig (nl.teslanet.mule.connectors.coap.api.ResourceConfig)1 InvalidResourceUriException (nl.teslanet.mule.connectors.coap.api.error.InvalidResourceUriException)1 InternalUriPatternException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUriPatternException)1 ResourceRegistry (nl.teslanet.mule.connectors.coap.internal.server.ResourceRegistry)1 CoapResource (org.eclipse.californium.core.CoapResource)1 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)1 Throws (org.mule.runtime.extension.api.annotation.error.Throws)1