1 package pl.psnc.dl.ege.webapp.request;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import pl.psnc.dl.ege.types.DataType;
9
10
11
12
13
14
15
16 public class ConversionRequestResolver extends RequestResolver
17 {
18
19 public static final String CONVERSIONS_SLICE_BASE = "Conversions/";
20
21 private static final String EN = "en";
22
23
24 public ConversionRequestResolver(HttpServletRequest request, Method method)
25 throws RequestResolvingException
26 {
27 this.method = method;
28 this.request = request;
29 init();
30 }
31
32
33 private void init()
34 throws RequestResolvingException
35 {
36 if (method.equals(Method.GET)) {
37 resolveGET();
38 }
39 else if (method.equals(Method.POST)) {
40 resolvePOST();
41 }
42 }
43
44
45 private void resolvePOST()
46 throws RequestResolvingException
47 {
48 String[] queries = resolveQueries();
49 if (queries.length < 3) {
50 throw new RequestResolvingException(
51 RequestResolvingException.Status.BAD_REQUEST);
52 }
53
54 DataType iDataType = decodeDataType(queries[1]);
55 int lastIndex = queries.length - 1;
56 if (queries[queries.length - 1].lastIndexOf("conversion") > -1)
57 lastIndex = queries.length - 2;
58 List<DataType> pathFrame = new ArrayList<DataType>();
59 pathFrame.add(iDataType);
60
61 for (int i = 2; i <= lastIndex; i++) {
62 DataType dt = decodeDataType(queries[i]);
63 if (dt == null && i != lastIndex) {
64 throw new RequestResolvingException(
65 RequestResolvingException.Status.BAD_REQUEST);
66 }
67 pathFrame.add(dt);
68 }
69 operation = OperationId.PERFORM_CONVERSION;
70 data = pathFrame;
71
72 }
73
74
75 private void resolveGET()
76 throws RequestResolvingException
77 {
78 String[] queries = resolveQueries();
79 if (queries.length > 1) {
80
81 if (queries.length > 2) {
82 throw new RequestResolvingException(
83 RequestResolvingException.Status.WRONG_METHOD);
84 }
85 else {
86 DataType inputType = decodeDataType(queries[1]);
87 operation = OperationId.PRINT_CONVERSIONS_PATHS;
88 data = inputType;
89 }
90 }
91 else if (queries.length == 1) {
92 operation = OperationId.PRINT_INPUT_TYPES;
93 }
94 }
95
96 private String[] resolveQueries()
97 {
98 String params = request.getRequestURL().toString();
99 params = (params.endsWith(SLASH) ? params : params + SLASH);
100 params = params.substring(params.indexOf(CONVERSIONS_SLICE_BASE),
101 params.length());
102 String[] queries = params.split(SLASH);
103 return queries;
104 }
105
106
107
108
109
110
111
112
113 public String getConversionProperties()
114 throws RequestResolvingException
115 {
116 if (method.equals(Method.POST)) {
117 return request.getParameter("properties");
118 }
119 else {
120 throw new RequestResolvingException(
121 RequestResolvingException.CONV_PARAMS);
122 }
123 }
124
125
126
127
128 public String getLocale()
129 {
130 return EN;
131 }
132
133
134 }