View Javadoc

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   * <p>Abstract base class for resolving request for WS operation.</p>
13   * 
14   * @author mariuszs
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  	 * Properly formed request should inform about operation to perform.<br/>
42  	 * E.g. operation of conversion or operation of listing all available validations. 
43  	 * 
44  	 * @return operation id
45  	 */
46  	public OperationId getOperationId(){
47  		return operation;
48  	}
49  	
50  	/**
51  	 * Returns decoded operation data (if it is needed), e.g. data types.
52  	 * Returned 'Object' should be casted into expected type.
53  	 * 
54  	 * @return data
55  	 */
56  	public Object getData(){
57  		return data;
58  	}
59  	
60  	/**
61  	 * Returns reference to request. 
62  	 * 
63  	 * @return
64  	 */
65  	public HttpServletRequest getRequest(){
66  		return request;
67  	}
68  	
69  	/**
70  	 * Should return local id of request. 
71  	 * 
72  	 * @return
73  	 */
74  	public abstract String getLocale();
75  	
76  	/**
77  	 * Encodes specified data type into URI proper form.
78  	 * Encoder uses 'UTF-8'. Encoded data type syntax is -  
79  	 * <p><b>[format]:[mime_part1]:[mime_part2]</b></p>
80  	 * 
81  	 * @param dataType
82  	 * @return
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 	 * Decodes URI part into data type.
101 	 * Encoded data type syntax is -  
102 	 * <p><b>[format]:[mime_part1]:[mime_part2]</b></p>
103 	 * 
104 	 * @param uriPart
105 	 * @return decoded data type
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 }