1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.InputStream; |
---|
4 | import eu.clarin.cmdi.mdservice.internal.Cache; |
---|
5 | import eu.clarin.cmdi.mdservice.internal.MDTransformer; |
---|
6 | import eu.clarin.cmdi.mdservice.proxy.BasicProxy; |
---|
7 | |
---|
8 | /** |
---|
9 | * Acts as a Struts 2 controller |
---|
10 | * serves the requests regarding Components |
---|
11 | * it passes them further (Proxy) to the ComponentRegistry. |
---|
12 | * |
---|
13 | * Nested Components are resolved during xsl-transformation. |
---|
14 | * @author vronk |
---|
15 | * |
---|
16 | */ |
---|
17 | public class CompRegistryProxyAction extends GenericAction { |
---|
18 | |
---|
19 | private static final long serialVersionUID = 1L; |
---|
20 | |
---|
21 | private String proxy_key = "compregistry"; |
---|
22 | private String compid; |
---|
23 | |
---|
24 | public String getCompid() { |
---|
25 | return compid; |
---|
26 | } |
---|
27 | |
---|
28 | public void setCompid(String compid) { |
---|
29 | this.compid = compid; |
---|
30 | } |
---|
31 | |
---|
32 | @Override |
---|
33 | public String getFullFormat() { |
---|
34 | if (compid==null || compid.equals("")) { |
---|
35 | if (getActionkey().equals("elements")){ |
---|
36 | return "terms2" + getFormat(); |
---|
37 | } else { |
---|
38 | return "complist2" + getFormat(); |
---|
39 | } |
---|
40 | } else { |
---|
41 | return "comp2" + getFormat(); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | //@Override |
---|
46 | public String getProxyKey() { |
---|
47 | return proxy_key; |
---|
48 | } |
---|
49 | |
---|
50 | public void setTargetProxy(){ |
---|
51 | |
---|
52 | setTargetProxy(new BasicProxy()); |
---|
53 | |
---|
54 | } |
---|
55 | |
---|
56 | @Override |
---|
57 | /* |
---|
58 | public String getRequestKey() { |
---|
59 | String key=""; |
---|
60 | if (getActionkey()!=null) { |
---|
61 | key += getActionkey() + "//-" ; |
---|
62 | } |
---|
63 | if (getCompid()!=null) { |
---|
64 | key += getCompid() + "//-" ; |
---|
65 | } |
---|
66 | |
---|
67 | return key; |
---|
68 | } |
---|
69 | */ |
---|
70 | public String getRequestKey() { |
---|
71 | String key=""; |
---|
72 | if (getActionkey()!=null) { |
---|
73 | key += getActionkey() + "//-" ; |
---|
74 | }else { |
---|
75 | key +="//-" ; |
---|
76 | } |
---|
77 | if (getCompid()!=null) { |
---|
78 | key += getCompid() + "//-" ; |
---|
79 | } else { |
---|
80 | key +="//-" ; |
---|
81 | } |
---|
82 | //if (getCollection()!=null) { |
---|
83 | key +="//-" ; |
---|
84 | |
---|
85 | //if (getSquery()!=null) { |
---|
86 | key +="//-" ; |
---|
87 | |
---|
88 | if (getStartRecord()!=null) { |
---|
89 | key += getStartRecord() + "//-"; |
---|
90 | } |
---|
91 | else{ |
---|
92 | key +="//-" ; |
---|
93 | } |
---|
94 | if (getMaximumRecords()!=null) { |
---|
95 | key += getMaximumRecords() + "//-"; |
---|
96 | } |
---|
97 | else{ |
---|
98 | key +="//-" ; |
---|
99 | } |
---|
100 | |
---|
101 | key += getRepository() + "//-"; |
---|
102 | key += getMaxdepth() + "//-"; |
---|
103 | |
---|
104 | if (getLang()!=null) { |
---|
105 | key += getLang() + "//-"; |
---|
106 | }else{ |
---|
107 | key +="//-" ; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | return key; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | @SuppressWarnings("deprecation") |
---|
116 | @Override |
---|
117 | public void prepare() { |
---|
118 | |
---|
119 | try { |
---|
120 | |
---|
121 | //log.debug("CRPA.prepare"); |
---|
122 | if (!getActionkey().equals("elements")) { |
---|
123 | super.prepare(); |
---|
124 | } else { |
---|
125 | loadParams(); |
---|
126 | setTargetProxy(); |
---|
127 | getTargetProxy().checkParams(); |
---|
128 | |
---|
129 | // log.debug("CRPA.prepareSourceStream - elements"); |
---|
130 | String xcid; |
---|
131 | InputStream _sourceStream; |
---|
132 | InputStream __sourceStream; |
---|
133 | InputStream _resultStream; |
---|
134 | |
---|
135 | _sourceStream = Cache.getCache().getFromCache(getRequestKey()); |
---|
136 | if (_sourceStream == null) { |
---|
137 | // log.debug("CRPA.prepareSourceStream- profiles"); |
---|
138 | //setActionkey("profiles"); |
---|
139 | //setFormat("xmlresolved"); |
---|
140 | __sourceStream = Cache.getCache().getFromCache(getRequestKey()); |
---|
141 | if (__sourceStream == null) { |
---|
142 | __sourceStream = getSourceStream(); |
---|
143 | xcid = Cache.getCache().putInCache(getRequestKey(),__sourceStream); |
---|
144 | //log.debug("CRPA-putting in cache: " + getRequestKey()); |
---|
145 | __sourceStream = Cache.getCache().getFromCache(getRequestKey()); |
---|
146 | } else{ |
---|
147 | //log.debug("CRPA-reading from cache: " + getRequestKey()); |
---|
148 | } |
---|
149 | // log.debug("CCCRPA." + getTargetRequest()); |
---|
150 | // log.debug("CCCRPA." + getFullFormat()); |
---|
151 | MDTransformer transformer = new MDTransformer(); |
---|
152 | transformer.setSrcFile(getTargetProxy().getTargetRequest()); |
---|
153 | //transformer.setParams(createTransformerParams()); |
---|
154 | _sourceStream = transformer.transformXML(__sourceStream);//, getFullFormat(),null,null,null,null,null,null); |
---|
155 | // log.debug("CRPA.- elements"); |
---|
156 | //setActionkey("elements"); |
---|
157 | //setFormat("htmllist"); |
---|
158 | xcid = Cache.getCache().putInCache(getRequestKey(),_sourceStream); |
---|
159 | // log.debug("CRPA-putting in cache: " + getRequestKey()); |
---|
160 | _sourceStream = Cache.getCache().getFromCache(getRequestKey()); |
---|
161 | } /*else { |
---|
162 | log.debug("CRPA-reading from cache: " + getRequestKey()); |
---|
163 | }*/ |
---|
164 | /* |
---|
165 | File file=new File(Cache.getCache().formPath(getRequestKey())); |
---|
166 | log.debug("File-absolutePath " + file.getAbsolutePath()); |
---|
167 | URL url = null; |
---|
168 | try{ |
---|
169 | url = file.toURL(); |
---|
170 | log.debug("URL-tostring " + url.toString()); |
---|
171 | } catch (MalformedURLException me){ |
---|
172 | } |
---|
173 | */ |
---|
174 | |
---|
175 | if (getFormat().equals("xml")) { |
---|
176 | _resultStream = _sourceStream; |
---|
177 | }else { |
---|
178 | // set URL as srcFile (for MDTransformer to pass to xsl-scripts) |
---|
179 | MDTransformer transformer = new MDTransformer(); |
---|
180 | transformer.setSrcFile(getTargetProxy().getTargetRequest()); |
---|
181 | //transformer.setParams(MDTransformer.createParamsMap(getFullFormat())); |
---|
182 | _resultStream = transformer.transformXML(_sourceStream);//, getFullFormat()); |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | log.debug(getProxyKey() + " success:" + (_resultStream!=null)); |
---|
187 | |
---|
188 | setSourceStream(_sourceStream); |
---|
189 | setResultStream(_resultStream); |
---|
190 | |
---|
191 | /* |
---|
192 | if (getFormat().equals("txt")) { |
---|
193 | File f=new File(Cache.getCache().formPath("terms_autocomplete.txt")); |
---|
194 | OutputStream out=new FileOutputStream(f); |
---|
195 | byte buf[]=new byte[1024]; |
---|
196 | int len; |
---|
197 | while((len=getResultStream().read(buf))>0) |
---|
198 | out.write(buf,0,len); |
---|
199 | out.close(); |
---|
200 | log.debug("terms_autocomplete.txt"); |
---|
201 | } |
---|
202 | */ |
---|
203 | } |
---|
204 | } catch(Exception e){ |
---|
205 | |
---|
206 | } |
---|
207 | |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | /** |
---|
212 | * uses base_url and (optionally) compid to form a url |
---|
213 | * no compid => list all components/profiles |
---|
214 | * @return the URL to component-registry |
---|
215 | * @throws Exception |
---|
216 | */ |
---|
217 | @Override |
---|
218 | public String getTargetRequestParams(){ |
---|
219 | String paramstr = ""; |
---|
220 | |
---|
221 | if (compid==null || compid.equals("")) { |
---|
222 | paramstr = getActionkey() ; |
---|
223 | } else { |
---|
224 | paramstr = getActionkey() + "/clarin.eu:cr1:" + compid; |
---|
225 | } |
---|
226 | return paramstr; |
---|
227 | } |
---|
228 | /* |
---|
229 | public URL getTargetRequest() throws IOException { |
---|
230 | // URL targetURL =new URL( base_url, compname + ".xml"); |
---|
231 | URL targetURL = null; |
---|
232 | if (compid==null || compid.equals("")) { |
---|
233 | targetURL =new URL( this.getTargetProxy().getBaseURL(), getActionkey() ); |
---|
234 | } else { |
---|
235 | targetURL =new URL( getTargetProxy().getBaseURL(), getActionkey() + "/clarin.eu:cr1:" + compid); |
---|
236 | } |
---|
237 | // log.debug("CompReg.targetURL:" + targetURL); |
---|
238 | // URLConnection urlConnection = targetURL.openConnection(); |
---|
239 | // urlConnection.setRequestProperty("Accept", "application/xml"); |
---|
240 | return targetURL; |
---|
241 | } |
---|
242 | */ |
---|
243 | |
---|
244 | |
---|
245 | } |
---|