Examples of CoapResponse


Examples of de.uniluebeck.itm.ncoap.message.CoapResponse

        else
            resourceStatus = getWrappedResourceStatus(coapRequest.getAcceptedContentFormats());

        //create CoAP response
        CoapResponse coapResponse;
        if(resourceStatus == null){
            coapResponse = new CoapResponse(coapRequest.getMessageTypeName(), MessageCode.Name.NOT_ACCEPTABLE_406);
            coapResponse.setContent("None of the accepted content formats is supported!".getBytes(CoapMessage.CHARSET),
                    ContentFormat.TEXT_PLAIN_UTF8);
        }

        else{
            coapResponse = new CoapResponse(coapRequest.getMessageTypeName(), MessageCode.Name.CONTENT_205);
            coapResponse.setContent(resourceStatus.getContent(), resourceStatus.getContentFormat());
            coapResponse.setEtag(resourceStatus.getEtag());
            coapResponse.setMaxAge(resourceStatus.getMaxAge());
        }

        responseFuture.set(coapResponse);
    }
View Full Code Here

Examples of de.uniluebeck.itm.ncoap.message.CoapResponse


    public void setMethodNotAllowedResponse(SettableFuture<CoapResponse> responseFuture, CoapRequest coapRequest)
        throws Exception{

        CoapResponse coapResponse = new CoapResponse(coapRequest.getMessageTypeName(),
                MessageCode.Name.METHOD_NOT_ALLOWED_405);

        coapResponse.setContent("Only method GET is allowed!".getBytes(CoapMessage.CHARSET),
                ContentFormat.TEXT_PLAIN_UTF8);

        responseFuture.set(coapResponse);
    }
View Full Code Here

Examples of de.uniluebeck.itm.ncoap.message.CoapResponse

    }


    private void handleIncomingCoapResponse(ChannelHandlerContext ctx, MessageEvent me) {

        CoapResponse coapResponse = (CoapResponse) me.getMessage();

        InetSocketAddress remoteEndpoint = (InetSocketAddress) me.getRemoteAddress();
        Token token = coapResponse.getToken();

        //Current response is NO update notification or is an error response (which SHOULD implicate the first)
        if(!coapResponse.isUpdateNotification() || MessageCode.isErrorMessage(coapResponse.getMessageCode())){
            if(observations.contains(remoteEndpoint, token)){
                log.info("Stop observation (remote address: {}, token: {}) due to received response: {}",
                        new Object[]{remoteEndpoint, token, coapResponse});

                stopObservation(remoteEndpoint, token);
            }
        }

        else if(coapResponse.isUpdateNotification()){
            //current response is update notification but there is no suitable observation
            if(!observations.contains(remoteEndpoint, token)){
                log.warn("No observation found for update notification (remote endpoint: {}, token: {}).",
                        remoteEndpoint, token);
            }

            //Current response is (non-error) update notification and there is a suitable observation
            else if(coapResponse.isUpdateNotification() && !MessageCode.isErrorMessage(coapResponse.getMessageCode())){
                //Lookup status age of latest update notification
                ResourceStatusAge latestStatusAge = observations.get(remoteEndpoint, token);

                //Get status age from newly received update notification
                long receivedSequenceNo = coapResponse.getObserve();
                ResourceStatusAge receivedStatusAge = new ResourceStatusAge(receivedSequenceNo, System.currentTimeMillis());

                if(ResourceStatusAge.isReceivedStatusNewer(latestStatusAge, receivedStatusAge)){
                    updateStatusAge(remoteEndpoint, token, receivedStatusAge);
                }
View Full Code Here

Examples of de.uniluebeck.itm.ncoap.message.CoapResponse

        Long contentFormat = determineResponseContentFormat(coapRequest);

        //create error response if content could not be created
        if(contentFormat == null){
            CoapResponse coapResponse =
                    new CoapResponse(coapRequest.getMessageTypeName(), MessageCode.Name.BAD_REQUEST_400);

            String content = "None of accepted content formats is supported by this Webservice.";
            coapResponse.setContent(content.getBytes(CoapMessage.CHARSET));
            responseFuture.set(coapResponse);
        }

        //create response with content if available
        else{
            byte[] content = getSerializedResourceStatus(contentFormat);
            CoapResponse coapResponse =
                    new CoapResponse(coapRequest.getMessageTypeName(), MessageCode.Name.CONTENT_205);

            coapResponse.setContent(content, contentFormat);
            responseFuture.set(coapResponse);
        }
    }
View Full Code Here

Examples of org.apache.mina.coap.resource.CoapResponse

        reg.register(new AbstractResourceHandler() {

            @Override
            public CoapResponse handle(CoapMessage request, IoSession session) {
                if (request.getCode() == CoapCode.GET.getCode()) {
                    return new CoapResponse(CoapCode.CONTENT.getCode(), status.getBytes());
                } else {
                    return new CoapResponse(CoapCode.METHOD_NOT_ALLOWED.getCode(), null);
                }
            }

            @Override
            public String getTittle() {
View Full Code Here

Examples of org.apache.mina.coap.resource.CoapResponse

                return "demo";
            }

            @Override
            public CoapResponse handle(CoapMessage request, IoSession session) {
                return new CoapResponse(CoapCode.CONTENT.getCode(), "niah niah niah niah niah\n niah niah niah\n"
                        .getBytes(), new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }));
            }

            @Override
            public String getTittle() {
                return "Some demo resource";
            }

        });

        reg.register(new AbstractResourceHandler() {

            @Override
            public CoapResponse handle(CoapMessage request, IoSession session) {
                String device = null;
                try {
                    for (CoapOption o : request.getOptions()) {
                        if (o.getType() == CoapOptionType.URI_QUERY) {
                            String qr = new String(o.getData(), "UTF-8");
                            if (qr.startsWith("id=")) {
                                device = qr.substring(2);
                            }
                        }
                    }
                    if (device != null) {
                        registration.put(device, session);
                        return new CoapResponse(CoapCode.CREATED.getCode(), null);
                    } else {
                        return new CoapResponse(CoapCode.BAD_REQUEST.getCode(), "no id=xxx parameter".getBytes("UTF-8"));
                    }
                } catch (UnsupportedEncodingException e) {
                    throw new IllegalStateException("no UTF-8 in the JVM", e);
                }
            }
View Full Code Here

Examples of org.apache.mina.coap.resource.CoapResponse

        reg.register(new AbstractResourceHandler() {

            @Override
            public CoapResponse handle(CoapMessage request, IoSession session) {
                if (request.getCode() == CoapCode.GET.getCode()) {
                    return new CoapResponse(CoapCode.CONTENT.getCode(), status.getBytes());
                } else {
                    return new CoapResponse(CoapCode.METHOD_NOT_ALLOWED.getCode(), null);
                }
            }

            @Override
            public String getTitle() {
View Full Code Here

Examples of org.apache.mina.coap.resource.CoapResponse

                return "demo";
            }

            @Override
            public CoapResponse handle(CoapMessage request, IoSession session) {
                return new CoapResponse(CoapCode.CONTENT.getCode(), "niah niah niah niah niah\n niah niah niah\n"
                        .getBytes(), new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }));
            }

            @Override
            public String getTitle() {
                return "Some demo resource";
            }

        });

        reg.register(new AbstractResourceHandler() {

            @Override
            public CoapResponse handle(CoapMessage request, IoSession session) {
                String device = null;
                try {
                    for (CoapOption o : request.getOptions()) {
                        if (o.getType() == CoapOptionType.URI_QUERY) {
                            String qr = new String(o.getData(), "UTF-8");
                            if (qr.startsWith("id=")) {
                                device = qr.substring(2);
                            }
                        }
                    }
                    if (device != null) {
                        registration.put(device, session);
                        return new CoapResponse(CoapCode.CREATED.getCode(), null);
                    } else {
                        return new CoapResponse(CoapCode.BAD_REQUEST.getCode(), "no id=xxx parameter".getBytes("UTF-8"));
                    }
                } catch (UnsupportedEncodingException e) {
                    throw new IllegalStateException("no UTF-8 in the JVM", e);
                }
            }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.