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