summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/process/ItemHTTPBridge.java
blob: 86e365988c9b52ec6db6537a98b642ee3ebe0239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.c2kernel.process;

import java.util.StringTokenizer;
import java.util.UUID;

import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.lookup.DomainPath;
import com.c2kernel.lookup.ItemPath;
import com.c2kernel.utils.server.HTTPRequestHandler;

/* QueryData over HTTP Socket Handler
 * Processes an HTTP request consisting of /<path to item>/<path to kernel object>
 * and returns that kernel object as XML
 * Currently supports GET requests.
 * REVISIT: POST calls Item.request()
 */

public class ItemHTTPBridge extends HTTPRequestHandler {

    public ItemHTTPBridge() { }

    @Override
	public String getName() {
        return "Item HTTP Server";
    }

    @Override
	public String processRequest() {
    	System.out.println("ItemHTTPBridge::ProcessRequest()");
        StringTokenizer tok = new StringTokenizer(resource, "?");
        String path = tok.nextToken();
        String query = tok.nextToken();
        ItemPath itemPath;
        try {
        	itemPath = new ItemPath(UUID.fromString(path));
        } catch (IllegalArgumentException ex) {
        	DomainPath domPath = new DomainPath(path);
        	if (!domPath.exists())
        		return error("404 Not Found", "The path "+path+" you requested was not found.");
        	try {
				itemPath = domPath.getItemPath();
			} catch (ObjectNotFoundException e) {
				return error("404 Not Found", "The path "+path+" you requested was not found.");
			}
        }

        if (method.equals("GET")) {
            try {
                C2KLocalObject response = Gateway.getStorage().get(itemPath, query, null);
                return Gateway.getMarshaller().marshall(response);
            }
            catch (Exception e) {
                return error("400 Bad Request", "Usage: GET &lt;path to item&gt;?&lt;path to kernel object&gt;<br>"+e.getClass().getName());
            }
        }
        return(super.processRequest());
    }

}