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