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