Commit 28267579 authored by Bjoern Konieczek's avatar Bjoern Konieczek
Browse files

Wrapper Class for Client and Server

parent 1f96e5bf
......@@ -21,7 +21,7 @@ public class BasicCoapResource implements CoapResource {
private CoapMediaType mediaType;
private String path;
private byte[] value;
protected byte[] value;
ResourceHandler resourceHandler = null;
ResourceServer serverListener = null; //could be a list of listener
String resourceType = null;
......
......@@ -69,15 +69,18 @@ public class CoapResourceServer implements CoapServer, ResourceServer {
}
@Override
public boolean updateResource(Resource resource) {
if (resource==null) return false;
if (resources.containsKey(resource.getPath())) {
addResource(resource);
generateEtag(resource);
logger.info("updated ressource: " + resource.getPath());
return true;
} else
return false;
public boolean updateResource(Resource resource, CoapRequest request ) {
if (resource==null)
return false;
if ( resources.containsKey(resource.getPath()) ) {
( (BasicCoapResource) resource ).setValue( request.getPayload() );
generateEtag(resource);
logger.info("updated ressource: " + resource.getPath());
return true;
} else
return false;
}
@Override
......@@ -99,19 +102,19 @@ public class CoapResourceServer implements CoapServer, ResourceServer {
/*corresponding to the coap spec the put is an update or create (or error)*/
public CoapResponseCode CoapResponseCode(Resource resource) {
Resource res = readResource(resource.getPath());
//TODO: check results
if (res == null){
createResource(resource);
return CoapResponseCode.Created_201;
} else if( res == coreResource ){
return CoapResponseCode.Forbidden_403;
} else {
updateResource(resource);
return CoapResponseCode.Changed_204;
}
}
// public CoapResponseCode CoapResponseCode(Resource resource) {
// Resource res = readResource(resource.getPath());
// //TODO: check results
// if (res == null){
// createResource(resource);
// return CoapResponseCode.Created_201;
// } else if( res == coreResource ){
// return CoapResponseCode.Forbidden_403;
// } else {
// updateResource(resource );
// return CoapResponseCode.Changed_204;
// }
// }
@Override
public void start() throws Exception {
......@@ -190,8 +193,12 @@ public class CoapResourceServer implements CoapServer, ResourceServer {
responseValue = resource.getValue();
}
response = channel.createResponse(request, CoapResponseCode.Content_205, resource.getCoapMediaType());
response.setPayload(responseValue);
if( request.getBlock2() != null || channel.getMaxSendBlocksize() != null ) {
response = channel.addBlockContext( request, responseValue );
} else {
response = channel.createResponse(request, CoapResponseCode.Content_205, resource.getCoapMediaType());
response.setPayload(responseValue);
}
if (request.getObserveOption() != null){
/*client wants to observe this resource*/
......@@ -251,7 +258,7 @@ public class CoapResourceServer implements CoapServer, ResourceServer {
} else {
/*update*/
if( request.getIfMatchOption() == null || checkEtagMatch( request.getIfMatchOption(), etags.get(targetPath) ) != -1 ) {
updateResource(parseRequest(request));
updateResource( resource, request);
response = channel.createResponse(request, CoapResponseCode.Changed_204);
} else if( request.getIfNoneMatchOption() || checkEtagMatch( request.getIfMatchOption(), etags.get(targetPath) ) == -1 ) {
response = channel.createResponse( request, CoapResponseCode.Precondition_Failed_412 );
......@@ -282,6 +289,15 @@ public class CoapResourceServer implements CoapServer, ResourceServer {
logger.error("Separate response failed but server never used separate responses");
}
@Override
public void onReset( CoapRequest lastRequest ) {
CoapResource resource = (CoapResource) readResource(lastRequest.getUriPath());
if( resource != null ) {
resource.removeObserver( lastRequest.getChannel() );
}
System.out.println("[INFO] Reset Message Received!");
}
protected String getLocalIpAddress() {
try {
......
......@@ -3,6 +3,8 @@ package org.ws4d.coap.rest;
import java.net.URI;
import org.ws4d.coap.interfaces.CoapRequest;
/**
* A ResourceServer provides network access to resources via a network protocol such as HTTP or CoAP.
*
......@@ -20,7 +22,7 @@ public interface ResourceServer {
/* returns the resource at the given path, null if no resource exists*/
public Resource readResource(String path);
/* updates a resource. resource must exist. if does not resource exist, false is returned. Resource is NOT created. */
public boolean updateResource(Resource resource);
public boolean updateResource(Resource resource, CoapRequest request);
/* deletes resource, returns false is resource does not exist */
public boolean deleteResource(String path);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment