1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.BufferedInputStream; |
---|
4 | import java.io.BufferedReader; |
---|
5 | import java.io.ByteArrayInputStream; |
---|
6 | import java.io.ByteArrayOutputStream; |
---|
7 | import java.io.File; |
---|
8 | import java.io.FileInputStream; |
---|
9 | import java.io.IOException; |
---|
10 | import java.io.InputStream; |
---|
11 | import java.io.InputStreamReader; |
---|
12 | import java.net.MalformedURLException; |
---|
13 | import java.net.URL; |
---|
14 | import java.util.Enumeration; |
---|
15 | |
---|
16 | import java.io.StringReader; |
---|
17 | import java.io.StringWriter; |
---|
18 | |
---|
19 | import javax.servlet.ServletInputStream; |
---|
20 | import javax.servlet.http.HttpServletRequest; |
---|
21 | import javax.xml.parsers.DocumentBuilder; |
---|
22 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
23 | import javax.xml.parsers.ParserConfigurationException; |
---|
24 | import javax.xml.transform.OutputKeys; |
---|
25 | import javax.xml.transform.Transformer; |
---|
26 | import javax.xml.transform.TransformerConfigurationException; |
---|
27 | import javax.xml.transform.TransformerException; |
---|
28 | import javax.xml.transform.TransformerFactory; |
---|
29 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
30 | import javax.xml.transform.dom.DOMSource; |
---|
31 | import javax.xml.transform.stream.StreamResult; |
---|
32 | |
---|
33 | |
---|
34 | import net.sf.json.JSON; |
---|
35 | import net.sf.json.JSONSerializer; |
---|
36 | import net.sf.json.xml.XMLSerializer; |
---|
37 | |
---|
38 | import java.security.AccessController; |
---|
39 | import java.security.PrivilegedActionException; |
---|
40 | import java.security.PrivilegedExceptionAction; |
---|
41 | //import javax.servlet.ServletInputStream; |
---|
42 | |
---|
43 | |
---|
44 | import org.apache.struts2.interceptor.ServletRequestAware; |
---|
45 | import org.w3c.dom.Document; |
---|
46 | import org.w3c.dom.Node; |
---|
47 | import org.w3c.dom.NodeList; |
---|
48 | import org.xml.sax.InputSource; |
---|
49 | import org.xml.sax.SAXException; |
---|
50 | |
---|
51 | import com.opensymphony.xwork2.ActionSupport; |
---|
52 | |
---|
53 | |
---|
54 | /** |
---|
55 | * |
---|
56 | * @author |
---|
57 | * |
---|
58 | */ |
---|
59 | public class WorkspaceAction extends GenericProxyAction |
---|
60 | implements ServletRequestAware |
---|
61 | { |
---|
62 | |
---|
63 | private static final long serialVersionUID = 1L; |
---|
64 | |
---|
65 | public static String USER = "user"; |
---|
66 | public static String SERVER = "server"; |
---|
67 | |
---|
68 | /** |
---|
69 | * Properties to be filled by Struts with according request-parameters |
---|
70 | */ |
---|
71 | //private String actionkey; |
---|
72 | private String data; |
---|
73 | private String type; |
---|
74 | //private String format; |
---|
75 | //private String userMsg; |
---|
76 | private String base_path; |
---|
77 | |
---|
78 | private HttpServletRequest request; |
---|
79 | |
---|
80 | public String getData(){ |
---|
81 | return data; |
---|
82 | } |
---|
83 | |
---|
84 | public void setData(String data) { |
---|
85 | this.data = data; |
---|
86 | } |
---|
87 | |
---|
88 | public String getType() { |
---|
89 | return type; |
---|
90 | } |
---|
91 | |
---|
92 | public void setType(String type) { |
---|
93 | this.type = type; |
---|
94 | } |
---|
95 | /* |
---|
96 | public String getFormat() { |
---|
97 | return format; |
---|
98 | } |
---|
99 | |
---|
100 | public void setFormat(String format) { |
---|
101 | this.format = format; |
---|
102 | } |
---|
103 | |
---|
104 | public String getActionkey() { |
---|
105 | return actionkey; |
---|
106 | } |
---|
107 | |
---|
108 | public void setActionkey(String actionKey) { |
---|
109 | actionkey = actionKey; |
---|
110 | } |
---|
111 | */ |
---|
112 | /** |
---|
113 | * primitive identification of the target-proxy |
---|
114 | * base for finding the right base_url in the props |
---|
115 | * subclass has to override with its specific proxykey |
---|
116 | * @return |
---|
117 | */ |
---|
118 | public String getProxyKey() { |
---|
119 | return "workspaceprofile"; |
---|
120 | } |
---|
121 | |
---|
122 | @Override |
---|
123 | public void setServletRequest(HttpServletRequest arg0) { |
---|
124 | request = arg0; |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | @Override |
---|
129 | public HttpServletRequest getServletRequest() { |
---|
130 | return request; |
---|
131 | } |
---|
132 | |
---|
133 | /* |
---|
134 | public String getUserMsg() { |
---|
135 | return userMsg; |
---|
136 | } |
---|
137 | |
---|
138 | public void setUserMsg(String userMsg) { |
---|
139 | this.userMsg = this.userMsg + "\n" + userMsg; |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | private InputStream resultStream; |
---|
144 | private InputStream sourceStream; |
---|
145 | */ |
---|
146 | /** |
---|
147 | * The stream holding the resulting data to be sent back to the user as response |
---|
148 | * @return |
---|
149 | */ |
---|
150 | /* |
---|
151 | public InputStream getResultStream() { |
---|
152 | return resultStream; |
---|
153 | } |
---|
154 | public void setResultStream(InputStream _resultStream){ |
---|
155 | resultStream = _resultStream; |
---|
156 | } |
---|
157 | public void setSourceStream(InputStream _sourceStream){ |
---|
158 | sourceStream = _sourceStream; |
---|
159 | } |
---|
160 | */ |
---|
161 | @Override |
---|
162 | public URL getBaseURL() { |
---|
163 | File file=new File(getPath()); |
---|
164 | URL url=null; |
---|
165 | try{ |
---|
166 | url=file.toURL(); |
---|
167 | Admin.notifyUser("DEBUG: WorkspaceAction.getBaseURL(): " + url); |
---|
168 | } catch (MalformedURLException e){ |
---|
169 | Admin.notifyUser("JSON exception has been caught" + e); |
---|
170 | } |
---|
171 | |
---|
172 | return url; |
---|
173 | } |
---|
174 | |
---|
175 | public String getBasePath() { |
---|
176 | if (base_path == null) { |
---|
177 | base_path = Admin.getConfig().getProperty(getProxyKey() + ".path"); |
---|
178 | } |
---|
179 | return base_path; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | public String getWorkspaceProfile() { |
---|
184 | String profilename = null; |
---|
185 | |
---|
186 | if (type.toLowerCase().equals(USER)) { |
---|
187 | if (getServletRequest().getRemoteUser() != null) { |
---|
188 | profilename = "WorkspaceProfile_" + getServletRequest().getRemoteUser() + ".xml"; |
---|
189 | } else { |
---|
190 | profilename = "WorkspaceProfile_default.xml"; |
---|
191 | } |
---|
192 | } else if (type.toLowerCase().equals(SERVER)) { |
---|
193 | profilename = "WorkspaceProfileServer.xml"; |
---|
194 | File f = new File(getBasePath() + profilename); |
---|
195 | if (! f.exists()){ |
---|
196 | //Admin.notifyUser(" not exists"); |
---|
197 | profilename = "WorkspaceProfile_default.xml"; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | if (profilename == null){ |
---|
202 | profilename = "WorkspaceProfile_default.xml"; |
---|
203 | Admin.notifyUser("WorkspaceProfilePath not specified (USER, SERVER)!"); |
---|
204 | } |
---|
205 | |
---|
206 | Admin.notifyUser("WorkspaceProfilename : " + profilename); |
---|
207 | return profilename; |
---|
208 | } |
---|
209 | |
---|
210 | @Override |
---|
211 | public String getFullFormat() { |
---|
212 | return "xml2json"; |
---|
213 | } |
---|
214 | |
---|
215 | public URL getURL() { |
---|
216 | File file=new File(getPath()); |
---|
217 | URL url=null; |
---|
218 | try{ |
---|
219 | url=file.toURL(); |
---|
220 | //Admin.notifyUser("url " + url); |
---|
221 | } catch (MalformedURLException e){ |
---|
222 | //Admin.notifyUser("" + e); |
---|
223 | } |
---|
224 | return url; |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | public String getPath() { |
---|
229 | String targetPath = getBasePath() + getWorkspaceProfile(); |
---|
230 | Admin.notifyUser("WorkspaceAction TARGETPATH: " + targetPath); |
---|
231 | return targetPath; |
---|
232 | } |
---|
233 | |
---|
234 | @Override |
---|
235 | public InputStream getSourceStream() throws IOException { |
---|
236 | InputStream stream = new BufferedInputStream( new FileInputStream(getPath())); |
---|
237 | |
---|
238 | |
---|
239 | return stream; |
---|
240 | } |
---|
241 | |
---|
242 | @Override |
---|
243 | public void prepare() throws Exception { |
---|
244 | |
---|
245 | //Admin.notifyUser("execute:"); |
---|
246 | InputStream in = getSourceStream(); |
---|
247 | this.setSourceStream(in); |
---|
248 | if (getFormat().equals("xml")) { |
---|
249 | setResultStream(in); |
---|
250 | }else { //JSON |
---|
251 | // set srcFile (for MDTransformer to pass to xsl-scripts) |
---|
252 | MDTransformer.getMDTransformer().setSrcFile(getURL()); |
---|
253 | setResultStream(MDTransformer.getMDTransformer().transformXML(in, getFullFormat())); |
---|
254 | } |
---|
255 | |
---|
256 | Admin.notifyUser(getProxyKey() + " success:" + (getResultStream()!=null)); |
---|
257 | } |
---|
258 | |
---|
259 | public void changeNodeName(Document doc, Node node, String new_name){ |
---|
260 | // Create an element with the new name |
---|
261 | Node node2 = (Node ) doc.createElement(new_name); |
---|
262 | // Copy the attributes to the new element NamedNodeMap attrs = element.getAttributes(); for (int i=0; i<attrs.getLength(); i++) { Attr attr2 = (Attr)doc.importNode(attrs.item(i), true); element2.getAttributes().setNamedItem(attr2); } |
---|
263 | // Move all the children |
---|
264 | while (node.hasChildNodes()) { node2.appendChild(node.getFirstChild()); } |
---|
265 | // Replace the old node with the new node |
---|
266 | Admin.notifyUser("replace:" + node2.getNodeName() + "," + node.getNodeName()); |
---|
267 | node.getParentNode().replaceChild(node2, node); |
---|
268 | } |
---|
269 | |
---|
270 | public String save() throws IOException { |
---|
271 | |
---|
272 | String jsonData = getData(); |
---|
273 | |
---|
274 | Admin.notifyUser("JSON data:"+jsonData); |
---|
275 | |
---|
276 | XMLSerializer serializer = new XMLSerializer(); |
---|
277 | JSON json = JSONSerializer.toJSON( jsonData ); |
---|
278 | serializer.setRootName("WorkspaceProfile"); |
---|
279 | serializer.setElementName("item"); |
---|
280 | serializer.setTypeHintsEnabled(false); |
---|
281 | String xml = serializer.write( json ); |
---|
282 | Admin.notifyUser("XML:" + xml); |
---|
283 | |
---|
284 | InputStream is_xml = new ByteArrayInputStream( xml.getBytes( ) ); |
---|
285 | |
---|
286 | Admin.writeToFile(getPath(), is_xml); |
---|
287 | |
---|
288 | String ret = "1"; |
---|
289 | this.setResultStream( new ByteArrayInputStream(ret.getBytes())); |
---|
290 | |
---|
291 | |
---|
292 | return SUCCESS; |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | } |
---|