Skip to content
This repository
Browse code

Now separating according to HTTP command.

  • Loading branch information...
commit b33d438b5da8b76b6708ce32bf9a6b5ce8285b35 1 parent 63f01eb
Enrico Franchi authored November 03, 2011
79  blogracy/src/it/unipr/aotlab/blogracy/web/AbstractRequestResolver.java
@@ -23,9 +23,12 @@
23 23
 package it.unipr.aotlab.blogracy.web;
24 24
 
25 25
 import it.unipr.aotlab.blogracy.errors.NotImplementedHTTPRequest;
  26
+import it.unipr.aotlab.blogracy.errors.URLMappingError;
26 27
 import org.gudy.azureus2.plugins.tracker.web.TrackerWebPageRequest;
27 28
 import org.gudy.azureus2.plugins.tracker.web.TrackerWebPageResponse;
28 29
 
  30
+import java.util.Map;
  31
+
29 32
 /**
30 33
  * User: enrico
31 34
  * Package: it.unipr.aotlab.blogracy.web
@@ -33,49 +36,67 @@
33 36
  * Time: 10:37 AM
34 37
  */
35 38
 public class AbstractRequestResolver implements RequestResolver {
36  
-    @Override
37  
-    public void resolve(final TrackerWebPageRequest request, final TrackerWebPageResponse response) throws Exception {
38  
-        if (isGET(request)) {
39  
-            get(request, response);
40  
-        } else if (isPOST(request)) {
41  
-            post(request, response);
42  
-        } else if (isPUT(request)) {
43  
-            put(request, response);
44  
-        } else if (isDELETE(request)) {
45  
-            delete(request, response);
46  
-        }
47  
-    }
  39
+    private Status requestStatus = Status.INVALID;
48 40
 
49  
-    protected void delete(final TrackerWebPageRequest request, final TrackerWebPageResponse response) throws NotImplementedHTTPRequest {
50  
-        throw new NotImplementedHTTPRequest();
  41
+    static protected enum Status {
  42
+        GET, POST, PUT, DELETE, INVALID
51 43
     }
52 44
 
53  
-    protected void put(final TrackerWebPageRequest request, final TrackerWebPageResponse response) throws NotImplementedHTTPRequest {
54  
-        throw new NotImplementedHTTPRequest();
55  
-    }
  45
+    ;
56 46
 
57  
-    protected void post(final TrackerWebPageRequest request, final TrackerWebPageResponse response) throws NotImplementedHTTPRequest {
58  
-        throw new NotImplementedHTTPRequest();
  47
+    protected Status getRequestStatus() {
  48
+        return requestStatus;
59 49
     }
60 50
 
61  
-    protected void get(final TrackerWebPageRequest request, final TrackerWebPageResponse response) throws NotImplementedHTTPRequest {
62  
-        throw new NotImplementedHTTPRequest();
  51
+    @Override
  52
+    public void resolve(final TrackerWebPageRequest request, final TrackerWebPageResponse response) throws Exception {
  53
+        processHeaders(request);
  54
+        switch (requestStatus) {
  55
+            case GET:
  56
+                get(request, response);
  57
+                break;
  58
+            case POST:
  59
+                post(request, response);
  60
+                break;
  61
+            case PUT:
  62
+                put(request, response);
  63
+                break;
  64
+            case DELETE:
  65
+                delete(request, response);
  66
+                break;
  67
+            case INVALID:
  68
+                throw new URLMappingError("Could not find out the kind of request we got.");
  69
+        }
63 70
     }
64 71
 
65  
-    private boolean isDELETE(final TrackerWebPageRequest request) {
66  
-        return false;
  72
+    private void processHeaders(final TrackerWebPageRequest request) {
  73
+        Map headers = request.getHeaders();
  74
+        String status = (String) headers.get("status");
  75
+        if (status.startsWith("GET")) {
  76
+            requestStatus = Status.GET;
  77
+        } else if (status.startsWith("POST")) {
  78
+            requestStatus = Status.POST;
  79
+        } else if (status.startsWith("PUT")) {
  80
+            requestStatus = Status.PUT;
  81
+        } else if (status.startsWith("DELETE")) {
  82
+            requestStatus = Status.DELETE;
  83
+        }
  84
+
67 85
     }
68 86
 
  87
+    protected void delete(final TrackerWebPageRequest request, final TrackerWebPageResponse response) throws NotImplementedHTTPRequest {
  88
+        throw new NotImplementedHTTPRequest("Command DELETE not supported for current resource.");
  89
+    }
69 90
 
70  
-    private boolean isPUT(final TrackerWebPageRequest request) {
71  
-        return false;
  91
+    protected void put(final TrackerWebPageRequest request, final TrackerWebPageResponse response) throws NotImplementedHTTPRequest {
  92
+        throw new NotImplementedHTTPRequest("Command PUT not supported for current resource.");
72 93
     }
73 94
 
74  
-    private boolean isPOST(final TrackerWebPageRequest request) {
75  
-        return false;
  95
+    protected void post(final TrackerWebPageRequest request, final TrackerWebPageResponse response) throws NotImplementedHTTPRequest {
  96
+        throw new NotImplementedHTTPRequest("Command POST not supported for current resource.");
76 97
     }
77 98
 
78  
-    private boolean isGET(final TrackerWebPageRequest request) {
79  
-        return false;
  99
+    protected void get(final TrackerWebPageRequest request, final TrackerWebPageResponse response) throws NotImplementedHTTPRequest {
  100
+        throw new NotImplementedHTTPRequest("Command GET not supported for current resource.");
80 101
     }
81 102
 }

0 notes on commit b33d438

Please sign in to comment.
Something went wrong with that request. Please try again.