1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.InputStream; |
---|
4 | import java.net.URL; |
---|
5 | |
---|
6 | import com.opensymphony.xwork2.ActionSupport; |
---|
7 | |
---|
8 | import eu.clarin.cmdi.mdservice.action.MDTransformer; |
---|
9 | |
---|
10 | /** |
---|
11 | * main Struts 2 controller |
---|
12 | * responds to requests (collections, model, recordset) |
---|
13 | * by dispatching the requests to appropriate internal methods and filling the inputStream with the result |
---|
14 | * |
---|
15 | * @author vronk |
---|
16 | * |
---|
17 | */ |
---|
18 | public class GenericProxyAction extends ActionSupport { |
---|
19 | |
---|
20 | private static final long serialVersionUID = 1L; |
---|
21 | |
---|
22 | /** |
---|
23 | * Properties to be filled by Struts with according request-parameters |
---|
24 | */ |
---|
25 | private String actionkey; |
---|
26 | private String q; |
---|
27 | private String collection; |
---|
28 | private int maxdepth; |
---|
29 | private String format; |
---|
30 | private String userMsg; |
---|
31 | |
---|
32 | public String getQ() { |
---|
33 | return q; |
---|
34 | } |
---|
35 | |
---|
36 | public void setQ(String q) { |
---|
37 | this.q = q; |
---|
38 | } |
---|
39 | |
---|
40 | public String getCollection() { |
---|
41 | return collection; |
---|
42 | } |
---|
43 | |
---|
44 | public void setCollection(String collection) { |
---|
45 | this.collection = collection; |
---|
46 | } |
---|
47 | |
---|
48 | public int getMaxdepth() { |
---|
49 | return maxdepth; |
---|
50 | } |
---|
51 | |
---|
52 | public void setMaxdepth(int maxdepth) { |
---|
53 | this.maxdepth = maxdepth; |
---|
54 | } |
---|
55 | |
---|
56 | public String getFormat() { |
---|
57 | return format; |
---|
58 | } |
---|
59 | |
---|
60 | public void setFormat(String format) { |
---|
61 | this.format = format; |
---|
62 | } |
---|
63 | |
---|
64 | public String getActionkey() { |
---|
65 | return actionkey; |
---|
66 | } |
---|
67 | |
---|
68 | public void setActionkey(String actionKey) { |
---|
69 | actionkey = actionKey; |
---|
70 | } |
---|
71 | |
---|
72 | public String getFullFormat() { |
---|
73 | return actionkey + "2" + format; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * primitive identification of the target-proxy |
---|
78 | * base for finding the right base_url in the props |
---|
79 | * subclass has to override with its specific proxykey |
---|
80 | * @return |
---|
81 | */ |
---|
82 | public String getProxyKey() { |
---|
83 | return ""; |
---|
84 | } |
---|
85 | |
---|
86 | public String getUserMsg() { |
---|
87 | return userMsg; |
---|
88 | } |
---|
89 | |
---|
90 | public void setUserMsg(String userMsg) { |
---|
91 | this.userMsg = this.userMsg + "\n" + userMsg; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | private InputStream resultStream; |
---|
97 | private InputStream sourceStream; |
---|
98 | |
---|
99 | /** |
---|
100 | * The stream holding the resulting data to be sent back to the user as response |
---|
101 | * @return |
---|
102 | */ |
---|
103 | public InputStream getResultStream() { |
---|
104 | return resultStream; |
---|
105 | } |
---|
106 | |
---|
107 | /* |
---|
108 | public InputStream getSourceStream() throws IOException { |
---|
109 | return getTargetRequest().openStream(); |
---|
110 | } |
---|
111 | */ |
---|
112 | |
---|
113 | private URL base_url ; |
---|
114 | |
---|
115 | public URL getBaseURL() throws Exception { |
---|
116 | if (base_url == null) { |
---|
117 | base_url = new URL(Admin.getConfig().getProperty(getProxyKey() + ".uri")); |
---|
118 | } |
---|
119 | return base_url; |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | public URL getTargetRequest() throws Exception { |
---|
124 | // URL targetURL =new URL( base_url, compname + ".xml"); |
---|
125 | URL targetURL = getBaseURL(); |
---|
126 | |
---|
127 | return targetURL; |
---|
128 | } |
---|
129 | |
---|
130 | public String getRequestKey() { |
---|
131 | String key=""; |
---|
132 | if (getActionkey()!=null) { |
---|
133 | key += getActionkey() + "-" ; |
---|
134 | } |
---|
135 | if (getQ()!=null) { |
---|
136 | key += getQ() + "-" ; |
---|
137 | } |
---|
138 | if (getCollection()!=null) { |
---|
139 | key += getCollection(); |
---|
140 | } |
---|
141 | |
---|
142 | return key; |
---|
143 | } |
---|
144 | |
---|
145 | public void prepare() throws Exception { |
---|
146 | |
---|
147 | Admin.notifyUser(getProxyKey() + ".targetURL: " + getTargetRequest() + " .format:" + getFullFormat()); |
---|
148 | |
---|
149 | |
---|
150 | // Caching |
---|
151 | // does not work yet see Cache.java for details |
---|
152 | /* |
---|
153 | String xcid; |
---|
154 | sourceStream = Cache.getCache().getFromCache(getRequestKey()); |
---|
155 | if (sourceStream == null) { |
---|
156 | sourceStream = getTargetRequest().openStream(); |
---|
157 | xcid = Cache.getCache().putInCache(getRequestKey(),sourceStream); |
---|
158 | Admin.notifyUser("putting in cache: " + getRequestKey()); |
---|
159 | sourceStream = Cache.getCache().getFromCache(getRequestKey()); |
---|
160 | } else { |
---|
161 | Admin.notifyUser("reading from cache: " + getRequestKey()); |
---|
162 | } |
---|
163 | */ |
---|
164 | sourceStream = getTargetRequest().openStream(); |
---|
165 | |
---|
166 | if (format.equals("xml")) { |
---|
167 | resultStream = sourceStream; |
---|
168 | }else { |
---|
169 | resultStream = MDTransformer.getMDTransformer().transformXML(sourceStream, getFullFormat()); |
---|
170 | } |
---|
171 | |
---|
172 | Admin.notifyUser(getProxyKey() + " success:" + (resultStream!=null)); |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * default Action method |
---|
177 | */ |
---|
178 | public String execute() throws Exception { |
---|
179 | |
---|
180 | prepare(); |
---|
181 | if (resultStream == null) { |
---|
182 | return ERROR; |
---|
183 | } else { |
---|
184 | return SUCCESS; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | } |
---|