Commit 460a2f14 authored by Björn Butzin's avatar Björn Butzin
Browse files

Refactoring

parent 7b80696a
......@@ -34,11 +34,11 @@ public class BasicCoapBlockClient implements CoapClient {
try {
clientChannel = channelManager.connect(this, InetAddress.getByName(SERVER_ADDRESS), PORT);
CoapRequest coapRequest = clientChannel.createRequest(true, CoapRequestCode.GET);
this.clientChannel = this.channelManager.connect(this, InetAddress.getByName(SERVER_ADDRESS), PORT);
CoapRequest coapRequest = this.clientChannel.createRequest(true, CoapRequestCode.GET);
coapRequest.setUriPath("/large");
clientChannel.setMaxReceiveBlocksize(CoapBlockSize.BLOCK_64);
clientChannel.sendMessage(coapRequest);
this.clientChannel.setMaxReceiveBlocksize(CoapBlockSize.BLOCK_64);
this.clientChannel.sendMessage(coapRequest);
System.out.println("Sent Request");
} catch (UnknownHostException e) {
e.printStackTrace();
......
......@@ -47,7 +47,7 @@ public class BasicCoapClient implements CoapClient {
public boolean connect(){
try {
clientChannel = (BasicCoapClientChannel) channelManager.connect(this, InetAddress.getByName(SERVER_ADDRESS), PORT);
this.clientChannel = (BasicCoapClientChannel) this.channelManager.connect(this, InetAddress.getByName(this.SERVER_ADDRESS), this.PORT);
} catch( UnknownHostException e ){
e.printStackTrace();
return false;
......@@ -64,35 +64,35 @@ public class BasicCoapClient implements CoapClient {
public CoapRequest createRequest( boolean reliable, CoapRequestCode reqCode ) {
return clientChannel.createRequest( reliable, reqCode );
return this.clientChannel.createRequest( reliable, reqCode );
}
public void sendRequest( CoapRequest request ){
if( request.getRequestCode() == CoapRequestCode.PUT || request.getRequestCode() == CoapRequestCode.POST ){
if( ( clientChannel.getMaxSendBlocksize() != null || request.getBlock1() != null ) ) {
request = clientChannel.addBlockContext( request);
if( ( this.clientChannel.getMaxSendBlocksize() != null || request.getBlock1() != null ) ) {
request = this.clientChannel.addBlockContext( request);
}
} else if( request.getRequestCode() == CoapRequestCode.GET && ( request.getBlock2() == null && clientChannel.getMaxReceiveBlocksize() != null )) {
CoapBlockOption block2 = new CoapBlockOption( 0, false, clientChannel.getMaxReceiveBlocksize() );
} else if( request.getRequestCode() == CoapRequestCode.GET && ( request.getBlock2() == null && this.clientChannel.getMaxReceiveBlocksize() != null )) {
CoapBlockOption block2 = new CoapBlockOption( 0, false, this.clientChannel.getMaxReceiveBlocksize() );
request.setBlock2( block2 );
}
clientChannel.sendMessage(request);
this.clientChannel.sendMessage(request);
}
public void setReceiveBlockSize( CoapBlockSize size ){
if( clientChannel != null )
clientChannel.setMaxReceiveBlocksize( size );
if( this.clientChannel != null )
this.clientChannel.setMaxReceiveBlocksize( size );
}
public void setSendBlockSize( CoapBlockSize size ){
if( clientChannel != null )
clientChannel.setMaxSendBlocksize(size);
if( this.clientChannel != null )
this.clientChannel.setMaxSendBlocksize(size);
}
public byte[] generateRequestToken(int tokenLength ){
byte[] token = new byte[tokenLength];
tokenGen.nextBytes(token);
this.tokenGen.nextBytes(token);
return token;
}
......
......@@ -43,28 +43,28 @@ class ByteContentListener implements ContentListener {
public void consumeContent(ContentDecoder decoder, IOControl ioctrl)
throws IOException {
input.consumeContent(decoder);
this.input.consumeContent(decoder);
}
public void finish() {
input.reset();
this.input.reset();
}
byte[] getContent() throws IOException {
byte[] b = new byte[input.length()];
input.read(b);
byte[] b = new byte[this.input.length()];
this.input.read(b);
return b;
}
@Override
public void contentAvailable(ContentDecoder decoder, IOControl arg1)
throws IOException {
input.consumeContent(decoder);
this.input.consumeContent(decoder);
}
@Override
public void finished() {
input.reset();
this.input.reset();
}
}
......@@ -64,7 +64,7 @@ public class CoapClientProxy implements CoapClient{
channel.close();
if (context != null) {
context.setInCoapResponse(response);
mapper.handleCoapClientResponse(context);
this.mapper.handleCoapClientResponse(context);
}
}
......@@ -75,7 +75,7 @@ public class CoapClientProxy implements CoapClient{
if (context != null) {
logger.warn("Coap client connection failed (e.g., timeout)!");
context.setInCoapResponse(null); // null indicates no response
mapper.handleCoapClientResponse(context);
this.mapper.handleCoapClientResponse(context);
}
}
......
......@@ -53,8 +53,8 @@ public class CoapServerProxy implements CoapServer{
//constructor of coapserver-class, initiates the jcoap-components and starts CoapSender
public CoapServerProxy() {
channelManager = BasicCoapChannelManager.getInstance();
channelManager.createServerListener(this, LOCAL_PORT);
this.channelManager = BasicCoapChannelManager.getInstance();
this.channelManager.createServerListener(this, LOCAL_PORT);
}
//interface-function for the message-queue
......@@ -141,7 +141,7 @@ public class CoapServerProxy implements CoapServer{
ProxyMessageContext context = new ProxyMessageContext(request, translate, proxyUri);
context.setServerAddress(serverAddress, serverPort);
context.setOutCoapResponse(response);
mapper.handleCoapServerRequest(context);
this.mapper.handleCoapServerRequest(context);
} catch (Exception e) {
logger.warn("invalid message");
channel.sendMessage(channel.createResponse(request, CoapResponseCode.Bad_Request_400));
......
......@@ -40,12 +40,12 @@ public class HttpClientNIO extends Thread {
public HttpClientNIO() {
try {
httpClient = new DefaultHttpAsyncClient();
this.httpClient = new DefaultHttpAsyncClient();
} catch (IOReactorException e) {
System.exit(-1);
e.printStackTrace();
}
httpClient.start();
this.httpClient.start();
logger.info("HTTP client started");
}
......@@ -56,7 +56,7 @@ public class HttpClientNIO extends Thread {
logger.info("send HTTP request");
ProxyHttpFutureCallback fc = new ProxyHttpFutureCallback();
fc.setContext(context);
httpClient.execute(context.getOutHttpRequest(), fc);
this.httpClient.execute(context.getOutHttpRequest(), fc);
}
private class ProxyHttpFutureCallback implements FutureCallback<HttpResponse>{
......@@ -68,26 +68,26 @@ public class HttpClientNIO extends Thread {
// this is called when response is received
public void completed(final HttpResponse response) {
if (context != null) {
context.setInHttpResponse(response);
mapper.handleHttpClientResponse(context);
if (this.context != null) {
this.context.setInHttpResponse(response);
HttpClientNIO.this.mapper.handleHttpClientResponse(this.context);
}
}
public void failed(final Exception ex) {
logger.warn("HTTP client request failed");
if (context != null) {
context.setInHttpResponse(new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, ex.getMessage()));
mapper.handleHttpClientResponse(context);
if (this.context != null) {
this.context.setInHttpResponse(new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, ex.getMessage()));
HttpClientNIO.this.mapper.handleHttpClientResponse(this.context);
}
}
public void cancelled() {
logger.warn("HTTP Client Request cancelled");
if (context != null) {
if (this.context != null) {
/* null indicates no response */
context.setInHttpResponse(new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "http connection canceled"));
mapper.handleHttpClientResponse(context);
this.context.setInHttpResponse(new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "http connection canceled"));
HttpClientNIO.this.mapper.handleHttpClientResponse(this.context);
}
}
}
......
......@@ -189,9 +189,8 @@ public class ProxyCache {
}
if (cache.isKeyInCache(key)) {
return true;
} else {
return false;
}
return false;
}
//for some operations it is necessary to build an http-date from string
......@@ -206,7 +205,7 @@ public class ProxyCache {
formatter.setTimeZone(TimeZone.getDefault()); //CEST, default is GMT
try {
date = (Date) formatter.parse(string_date);
date = formatter.parse(string_date);
} catch (ParseException e) {
e.printStackTrace();
}
......@@ -262,7 +261,7 @@ public class ProxyCache {
}
public boolean isEnabled() {
return enabled;
return this.enabled;
}
public void setEnabled(boolean enabled) {
......
......@@ -118,7 +118,7 @@ public class ProxyMapper {
public void handleHttpServerRequest(ProxyMessageContext context) {
httpRequestCount++;
this.httpRequestCount++;
// do not translate methods: OPTIONS,TRACE,CONNECT -> error
// "Not Implemented"
if (isHttpRequestMethodSupported(context.getInHttpRequest())) {
......@@ -134,16 +134,16 @@ public class ProxyMapper {
/* answer from cache */
resourceToHttp(context, resource);
context.setCached(true); // avoid "recaching"
httpServer.sendResponse(context);
this.httpServer.sendResponse(context);
logger.info("served HTTP request from cache");
servedFromCacheCount++;
this.servedFromCacheCount++;
} else {
/* not cached -> forward request */
try {
coapClient.createChannel(context); //channel must be created first
this.coapClient.createChannel(context); //channel must be created first
transRequestHttpToCoap(context);
context.setRequestTime(System.currentTimeMillis());
coapClient.sendRequest(context);
this.coapClient.sendRequest(context);
} catch (Exception e) {
logger.warn("HTTP to CoAP Request failed: " + e.getMessage());
/* close if a channel was connected */
......@@ -160,7 +160,7 @@ public class ProxyMapper {
}
public void handleCoapServerRequest(ProxyMessageContext context) {
coapRequestCount++;
this.coapRequestCount++;
ProxyResource resource = null;
if (context.getInCoapRequest().getRequestCode() == CoapRequestCode.GET){
resource = cache.get(context);
......@@ -171,15 +171,15 @@ public class ProxyMapper {
/* answer from cache */
resourceToHttp(context, resource);
context.setCached(true); // avoid "recaching"
httpServer.sendResponse(context);
this.httpServer.sendResponse(context);
logger.info("served CoAP request from cache");
servedFromCacheCount++;
this.servedFromCacheCount++;
} else {
/* translate CoAP Request -> HTTP Request */
try {
transRequestCoapToHttp(context);
context.setRequestTime(System.currentTimeMillis());
httpClient.sendRequest(context);
this.httpClient.sendRequest(context);
} catch (Exception e) {
logger.warn("CoAP to HTTP Request translation failed: " + e.getMessage());
sendDirectCoapError(context, CoapResponseCode.Not_Found_404);
......@@ -191,16 +191,16 @@ public class ProxyMapper {
/* answer from cache */
resourceToCoap(context, resource);
context.setCached(true); // avoid "recaching"
coapServer.sendResponse(context);
this.coapServer.sendResponse(context);
logger.info("served from cache");
servedFromCacheCount++;
this.servedFromCacheCount++;
} else {
/* translate CoAP Request -> CoAP Request */
try {
coapClient.createChannel(context); //channel must be created first
this.coapClient.createChannel(context); //channel must be created first
transRequestCoapToCoap(context);
context.setRequestTime(System.currentTimeMillis());
coapClient.sendRequest(context);
this.coapClient.sendRequest(context);
} catch (Exception e) {
logger.warn("CoAP to CoAP Request forwarding failed: " + e.getMessage());
sendDirectCoapError(context, CoapResponseCode.Not_Found_404);
......@@ -224,7 +224,7 @@ public class ProxyMapper {
logger.warn("CoAP to HTTP Response translation failed: " + e.getMessage());
context.setOutHttpResponse(new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error"));
}
httpServer.sendResponse(context);
this.httpServer.sendResponse(context);
} else {
/* coap to coap */
try {
......@@ -233,7 +233,7 @@ public class ProxyMapper {
logger.warn("CoAP to CoAP Response forwarding failed: " + e.getMessage());
context.getOutCoapResponse().setResponseCode(CoapResponseCode.Internal_Server_Error_500);
}
coapServer.sendResponse(context);
this.coapServer.sendResponse(context);
}
}
......@@ -250,7 +250,7 @@ public class ProxyMapper {
logger.warn("HTTP to CoAP Response translation failed: " + e.getMessage());
context.getOutCoapResponse().setResponseCode(CoapResponseCode.Internal_Server_Error_500);
}
coapServer.sendResponse(context);
this.coapServer.sendResponse(context);
}
......@@ -507,7 +507,7 @@ public class ProxyMapper {
public void sendDirectHttpError(ProxyMessageContext context,int code, String reason){
HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, code, reason);
context.setOutHttpResponse(httpResponse);
httpServer.sendResponse(context);
this.httpServer.sendResponse(context);
}
/* these functions are called if the request translation fails and no message was forwarded */
......@@ -515,7 +515,7 @@ public class ProxyMapper {
CoapServerChannel channel = (CoapServerChannel) context.getInCoapRequest().getChannel();
CoapResponse response = channel.createResponse(context.getInCoapRequest(), code);
context.setInCoapResponse(response);
coapServer.sendResponse(context);
this.coapServer.sendResponse(context);
}
public static void resourceToHttp(ProxyMessageContext context, ProxyResource resource){
......@@ -714,9 +714,8 @@ public class ProxyMapper {
case HttpStatus.SC_NO_CONTENT:
if (context.getInCoapRequest().getRequestCode() == CoapRequestCode.DELETE) {
return CoapResponseCode.Deleted_202;
} else {
return CoapResponseCode.Changed_204;
}
return CoapResponseCode.Changed_204;
case HttpStatus.SC_NOT_MODIFIED: return CoapResponseCode.Valid_203;
case HttpStatus.SC_OK: return CoapResponseCode.Content_205;
case HttpStatus.SC_UNAUTHORIZED: return CoapResponseCode.Unauthorized_401;
......@@ -863,16 +862,16 @@ public class ProxyMapper {
//setter-functions to introduce other threads
public void setHttpServer(HttpServerNIO server) {
httpServer = server;
this.httpServer = server;
}
public void setHttpClient(HttpClientNIO client) {
httpClient = client;
this.httpClient = client;
}
public void setCoapServer(CoapServerProxy server) {
coapServer = server;
this.coapServer = server;
}
public void setCoapClient(CoapClientProxy client) {
coapClient = client;
this.coapClient = client;
}
......@@ -953,21 +952,21 @@ public class ProxyMapper {
}
public int getHttpRequestCount() {
return httpRequestCount;
return this.httpRequestCount;
}
public int getCoapRequestCount() {
return coapRequestCount;
return this.coapRequestCount;
}
public int getServedFromCacheCount() {
return servedFromCacheCount;
return this.servedFromCacheCount;
}
public void resetCounter(){
httpRequestCount = 0;
coapRequestCount = 0;
servedFromCacheCount = 0;
this.httpRequestCount = 0;
this.coapRequestCount = 0;
this.servedFromCacheCount = 0;
}
public void setCacheEnabled(boolean enabled) {
......@@ -975,19 +974,19 @@ public class ProxyMapper {
}
public CoapClientProxy getCoapClient() {
return coapClient;
return this.coapClient;
}
public CoapServerProxy getCoapServer() {
return coapServer;
return this.coapServer;
}
public HttpServerNIO getHttpServer() {
return httpServer;
return this.httpServer;
}
public HttpClientNIO getHttpClient() {
return httpClient;
return this.httpClient;
}
}
......@@ -100,17 +100,17 @@ public class ProxyMessageContext {
}
public boolean isCoapRequest(){
return inCoapRequest != null;
return this.inCoapRequest != null;
}
public boolean isHttpRequest(){
return inHttpRequest != null;
return this.inHttpRequest != null;
}
public CoapRequest getInCoapRequest() {
return inCoapRequest;
return this.inCoapRequest;
}
public void setInCoapRequest(CoapRequest inCoapRequest) {
......@@ -118,7 +118,7 @@ public class ProxyMessageContext {
}
public HttpRequest getInHttpRequest() {
return inHttpRequest;
return this.inHttpRequest;
}
public void setInHttpRequest(HttpRequest inHttpRequest) {
......@@ -126,7 +126,7 @@ public class ProxyMessageContext {
}
public CoapResponse getInCoapResponse() {
return inCoapResponse;
return this.inCoapResponse;
}
public void setInCoapResponse(CoapResponse inCoapResponse) {
......@@ -134,7 +134,7 @@ public class ProxyMessageContext {
}
public HttpResponse getInHttpResponse() {
return inHttpResponse;
return this.inHttpResponse;
}
public void setInHttpResponse(HttpResponse inHttpResponse) {
......@@ -142,7 +142,7 @@ public class ProxyMessageContext {
}
public CoapResponse getOutCoapResponse() {
return outCoapResponse;
return this.outCoapResponse;
}
public void setOutCoapResponse(CoapResponse outCoapResponse) {
......@@ -150,7 +150,7 @@ public class ProxyMessageContext {
}
public CoapRequest getOutCoapRequest() {
return outCoapRequest;
return this.outCoapRequest;
}
public void setOutCoapRequest(CoapRequest outCoapRequest) {
......@@ -158,7 +158,7 @@ public class ProxyMessageContext {
}
public HttpResponse getOutHttpResponse() {
return outHttpResponse;
return this.outHttpResponse;
}
public void setOutHttpResponse(HttpResponse outHttpResponse) {
......@@ -166,7 +166,7 @@ public class ProxyMessageContext {
}
public HttpUriRequest getOutHttpRequest() {
return outHttpRequest;
return this.outHttpRequest;
}
public void setOutHttpRequest(HttpUriRequest outHttpRequest) {
......@@ -174,7 +174,7 @@ public class ProxyMessageContext {
}
public CoapClientChannel getOutCoapClientChannel() {
return outCoapClientChannel;
return this.outCoapClientChannel;
}
public void setOutCoapClientChannel(CoapClientChannel outClientChannel) {
......@@ -182,7 +182,7 @@ public class ProxyMessageContext {
}
public InetAddress getClientAddress() {
return clientAddress;
return this.clientAddress;
}
public void setClientAddress(InetAddress clientAddress, int clientPort) {
......@@ -191,7 +191,7 @@ public class ProxyMessageContext {
}
public InetAddress getServerAddress() {
return serverAddress;
return this.serverAddress;
}
public void setServerAddress(InetAddress serverAddress, int serverPort) {
......@@ -200,15 +200,15 @@ public class ProxyMessageContext {
}
public int getClientPort() {
return clientPort;
return this.clientPort;
}
public int getServerPort() {
return serverPort;
return this.serverPort;
}
public boolean isTranslate() {
return translate;
return this.translate;
}
public void setTranslatedCoapRequest(CoapRequest request) {
......@@ -220,15 +220,15 @@ public class ProxyMessageContext {
}
public URI getUri() {
return uri;
return this.uri;
}
public NHttpResponseTrigger getTrigger() {
return trigger;
return this.trigger;
}
public boolean isCached() {
return cached;
return this.cached;
}
public void setCached(boolean cached) {
......@@ -236,7 +236,7 @@ public class ProxyMessageContext {
}
public ProxyResource getResource() {
return resource;