1 package pl.psnc.dl.ege.webapp.request;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLDecoder;
5 import java.net.URLEncoder;
6
7 import javax.servlet.http.HttpServletRequest;
8
9 import pl.psnc.dl.ege.types.DataType;
10
11
12
13
14
15
16 public abstract class RequestResolver
17 {
18 public static final String UNDERSCORE = "_";
19
20 public static final String DUO_UNDERSCORE = "__";
21
22 public static final String COLON = ":";
23
24 public static final String SPLITTER = COLON;
25
26 public static final String SLASH = "/";
27
28 public static final String COMMA = ",";
29
30 public static final String SEMICOLON = ";";
31
32 protected Method method;
33
34 protected HttpServletRequest request;
35
36 protected OperationId operation = null;
37
38 protected Object data = null;
39
40
41
42
43
44
45
46 public OperationId getOperationId(){
47 return operation;
48 }
49
50
51
52
53
54
55
56 public Object getData(){
57 return data;
58 }
59
60
61
62
63
64
65 public HttpServletRequest getRequest(){
66 return request;
67 }
68
69
70
71
72
73
74 public abstract String getLocale();
75
76
77
78
79
80
81
82
83
84 public String encodeDataType(DataType dataType)
85 {
86 String format = null;
87 String mime = null;
88 try {
89 format = dataType.getFormat();
90 String[] mimes = dataType.getMimeType().split(SLASH);
91 mime = mimes[0] + SPLITTER + mimes[1];
92 return URLEncoder.encode(format + SPLITTER + mime, "UTF-8");
93 }
94 catch (UnsupportedEncodingException ex) {
95 return null;
96 }
97 }
98
99
100
101
102
103
104
105
106
107 public DataType decodeDataType(String uriPart)
108 {
109 try {
110 String[] partial = (URLDecoder.decode(uriPart, "UTF-8"))
111 .split(SPLITTER);
112 DataType dataType = null;
113 String mime = partial[1] + SLASH + partial[2];
114 dataType = new DataType(partial[0], mime.replaceAll(" ","+"));
115 return dataType;
116 }
117 catch (UnsupportedEncodingException ex) {
118 return null;
119 }
120
121 }
122 }