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