1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.BufferedInputStream; |
---|
4 | import java.io.ByteArrayInputStream; |
---|
5 | import java.io.File; |
---|
6 | import java.io.FileInputStream; |
---|
7 | import java.io.IOException; |
---|
8 | import java.io.InputStream; |
---|
9 | import java.net.MalformedURLException; |
---|
10 | import java.net.URL; |
---|
11 | |
---|
12 | import javax.servlet.http.HttpServletRequest; |
---|
13 | import javax.xml.transform.TransformerException; |
---|
14 | |
---|
15 | import net.sf.json.JSON; |
---|
16 | import net.sf.json.JSONSerializer; |
---|
17 | import net.sf.json.xml.XMLSerializer; |
---|
18 | |
---|
19 | import org.apache.commons.io.IOUtils; |
---|
20 | import org.apache.struts2.interceptor.ServletRequestAware; |
---|
21 | |
---|
22 | import com.opensymphony.xwork2.ActionSupport; |
---|
23 | |
---|
24 | /** |
---|
25 | * |
---|
26 | * @author |
---|
27 | * |
---|
28 | */ |
---|
29 | public class GenericWorkspaceAction extends ActionSupport |
---|
30 | { |
---|
31 | |
---|
32 | private static final long serialVersionUID = 1L; |
---|
33 | |
---|
34 | /** |
---|
35 | * Properties to be filled by Struts with according request-parameters |
---|
36 | */ |
---|
37 | private String actionkey; |
---|
38 | private String q; |
---|
39 | private String id; |
---|
40 | private String format; |
---|
41 | private String userMsg; |
---|
42 | |
---|
43 | private String base_path; |
---|
44 | |
---|
45 | public String getQ() { |
---|
46 | return q; |
---|
47 | } |
---|
48 | |
---|
49 | public void setQ(String q) { |
---|
50 | this.q = q; |
---|
51 | } |
---|
52 | |
---|
53 | public String getID() { |
---|
54 | return id; |
---|
55 | } |
---|
56 | |
---|
57 | public void setID(String _id) { |
---|
58 | this.id = _id; |
---|
59 | } |
---|
60 | |
---|
61 | public String getFormat() { |
---|
62 | return format; |
---|
63 | } |
---|
64 | |
---|
65 | public void setFormat(String format) { |
---|
66 | this.format = format; |
---|
67 | } |
---|
68 | |
---|
69 | public String getActionkey() { |
---|
70 | return actionkey; |
---|
71 | } |
---|
72 | |
---|
73 | public void setActionkey(String actionKey) { |
---|
74 | actionkey = actionKey; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * primitive identification of the target-proxy |
---|
79 | * base for finding the right base_url in the props |
---|
80 | * subclass has to override with its specific proxykey |
---|
81 | * @return |
---|
82 | */ |
---|
83 | public String getProxyKey() { |
---|
84 | return ""; |
---|
85 | } |
---|
86 | |
---|
87 | public String getUserMsg() { |
---|
88 | return userMsg; |
---|
89 | } |
---|
90 | |
---|
91 | public void setUserMsg(String userMsg) { |
---|
92 | this.userMsg = this.userMsg + "\n" + userMsg; |
---|
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 | public void setResultStream(InputStream _resultStream){ |
---|
107 | resultStream = _resultStream; |
---|
108 | } |
---|
109 | public void setSourceStream(InputStream _sourceStream){ |
---|
110 | sourceStream = _sourceStream; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | public URL getBaseURL() { |
---|
115 | File file=new File(getPath()); |
---|
116 | URL url=null; |
---|
117 | try{ |
---|
118 | url=file.toURL(); |
---|
119 | Admin.notifyUser(" JSON TEST: url " + url); |
---|
120 | } catch (MalformedURLException e){ |
---|
121 | Admin.notifyUser("JSON exception has been caught" + e); |
---|
122 | } |
---|
123 | |
---|
124 | return url; |
---|
125 | } |
---|
126 | |
---|
127 | public String getBasePath() { |
---|
128 | if (base_path == null) { |
---|
129 | base_path = Admin.getConfig().getProperty(getProxyKey() + ".path"); |
---|
130 | } |
---|
131 | return base_path; |
---|
132 | } |
---|
133 | //TODO identification of WOrkspaceProfile (user, name, id) |
---|
134 | public String getWorkspaceProfile() { |
---|
135 | return "WorkspaceProfile.xml"; |
---|
136 | } |
---|
137 | |
---|
138 | public String getFullFormat() { |
---|
139 | return "xml2json"; |
---|
140 | } |
---|
141 | |
---|
142 | public URL getURL() { |
---|
143 | File file=new File(getPath()); |
---|
144 | URL url=null; |
---|
145 | try{ |
---|
146 | url=file.toURL(); |
---|
147 | //Admin.notifyUser("url " + url); |
---|
148 | } catch (MalformedURLException e){ |
---|
149 | //Admin.notifyUser("" + e); |
---|
150 | } |
---|
151 | return url; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | public String getPath() { |
---|
156 | String targetPath = getBasePath() + getWorkspaceProfile(); |
---|
157 | |
---|
158 | return targetPath; |
---|
159 | } |
---|
160 | |
---|
161 | public InputStream getSourceStream() throws IOException { |
---|
162 | InputStream stream = new BufferedInputStream( new FileInputStream(getPath())); |
---|
163 | |
---|
164 | |
---|
165 | return stream; |
---|
166 | } |
---|
167 | |
---|
168 | public InputStream getPart(InputStream stream){ |
---|
169 | return stream; |
---|
170 | } |
---|
171 | @SuppressWarnings("deprecation") |
---|
172 | public void prepare() throws Exception { |
---|
173 | |
---|
174 | //Admin.notifyUser(getProxyKey() + ".targetURL: " + getTargetRequest() + " .format:" + getFullFormat()); |
---|
175 | Admin.notifyUser("execute:"); |
---|
176 | sourceStream = getSourceStream(); |
---|
177 | if (format.equals("xml")) { |
---|
178 | resultStream = sourceStream; |
---|
179 | }else { //JSON |
---|
180 | // set srcFile (for MDTransformer to pass to xsl-scripts) |
---|
181 | MDTransformer transformer = new MDTransformer(); |
---|
182 | transformer.setSrcFile(getURL()); |
---|
183 | //InputStream tmp_s = MDTransformer.getMDTransformer().transformXML(sourceStream, "attr2elements"); |
---|
184 | transformer.setParams(MDTransformer.createParamsMap(getFullFormat())); |
---|
185 | InputStream s = transformer.transformXML(sourceStream);//, getFullFormat()); |
---|
186 | resultStream = getPart(s); |
---|
187 | } |
---|
188 | |
---|
189 | Admin.notifyUser(getProxyKey() + " success:" + (resultStream!=null)); |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * default Action method |
---|
194 | */ |
---|
195 | public String execute() throws Exception { |
---|
196 | |
---|
197 | prepare(); |
---|
198 | if (resultStream == null) { |
---|
199 | return ERROR; |
---|
200 | } else { |
---|
201 | return SUCCESS; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | } |
---|