1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.webadmin.viewer.components; |
21 |
| |
22 |
| import org.apache.commons.fileupload.FileItem; |
23 |
| import org.apache.commons.fileupload.FileUploadException; |
24 |
| import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
25 |
| import org.apache.commons.fileupload.servlet.ServletFileUpload; |
26 |
| import org.apache.commons.logging.Log; |
27 |
| import org.apache.commons.logging.LogFactory; |
28 |
| import org.apache.xindice.core.Collection; |
29 |
| import org.apache.xindice.core.DBException; |
30 |
| import org.apache.xindice.core.meta.MetaData; |
31 |
| import org.apache.xindice.util.XindiceException; |
32 |
| import org.apache.xindice.webadmin.viewer.HtmlCollectionViewer; |
33 |
| import org.apache.xindice.webadmin.viewer.HtmlViewerComponent; |
34 |
| import org.apache.xindice.xml.TextWriter; |
35 |
| import org.apache.xindice.xml.Xml2HtmlWriter; |
36 |
| import org.apache.xindice.xml.dom.DOMParser; |
37 |
| import org.apache.xindice.xml.dom.DocumentImpl; |
38 |
| |
39 |
| import org.w3c.dom.Document; |
40 |
| import org.w3c.dom.Element; |
41 |
| |
42 |
| import javax.servlet.ServletException; |
43 |
| import javax.servlet.http.HttpServletRequest; |
44 |
| import javax.servlet.http.HttpServletResponse; |
45 |
| |
46 |
| import java.io.IOException; |
47 |
| import java.io.Writer; |
48 |
| import java.util.Enumeration; |
49 |
| import java.util.Iterator; |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| public class MetadataCollectionViewer extends HtmlViewerComponent implements HtmlCollectionViewer { |
58 |
| |
59 |
| private static final Log log = LogFactory.getLog(MetadataCollectionViewer.class); |
60 |
| |
61 |
0
| public void execute(HttpServletRequest req, HttpServletResponse res, Collection col)
|
62 |
| throws ServletException, IOException { |
63 |
| |
64 |
0
| String path = col.getCanonicalName();
|
65 |
0
| String title = "Collection Metadata for " + path;
|
66 |
| |
67 |
0
| Writer output = startViewer(title, path, res);
|
68 |
| |
69 |
0
| String attrName = req.getParameter("attrname");
|
70 |
0
| if (attrName == null) {
|
71 |
0
| attrName = "";
|
72 |
| } |
73 |
0
| String attrValue = req.getParameter("attrvalue");
|
74 |
0
| if (attrValue == null) {
|
75 |
0
| attrValue = "";
|
76 |
| } |
77 |
0
| String updateAttr = req.getParameter("updateattr");
|
78 |
0
| String deleteAttr = req.getParameter("deleteattr");
|
79 |
0
| String custom = req.getParameter("custom");
|
80 |
0
| String deleteCustom = req.getParameter("deleteCustom");
|
81 |
| |
82 |
| |
83 |
0
| if (attrName.length() > 0 && col.isMetaEnabled() && updateAttr == null) {
|
84 |
0
| try {
|
85 |
0
| MetaData newMeta = col.getCollectionMeta();
|
86 |
0
| newMeta.setAttribute(attrName, attrValue);
|
87 |
0
| col.setCollectionMeta(newMeta);
|
88 |
0
| attrName = "";
|
89 |
0
| attrValue = "";
|
90 |
| } catch (DBException e1) { |
91 |
0
| log.error(e1);
|
92 |
0
| printError("Cannot add Attribute " + attrName + ": " + e1.getMessage(), output);
|
93 |
| } |
94 |
0
| } else if (updateAttr != null && updateAttr.length() > 0 && col.isMetaEnabled()) {
|
95 |
0
| attrName = updateAttr;
|
96 |
0
| try {
|
97 |
0
| attrValue = (String) col.getCollectionMeta().getAttribute(attrName);
|
98 |
| } catch (DBException e) { |
99 |
0
| log.error(e);
|
100 |
0
| attrValue = "";
|
101 |
| } |
102 |
0
| } else if (deleteAttr != null && deleteAttr.length() > 0 && col.isMetaEnabled()) {
|
103 |
0
| try {
|
104 |
0
| MetaData delMeta = col.getCollectionMeta();
|
105 |
0
| delMeta.removeAttribute(deleteAttr);
|
106 |
0
| col.setCollectionMeta(delMeta);
|
107 |
| } catch (DBException e) { |
108 |
0
| log.error(e);
|
109 |
0
| printError("Cannot delete Attribute " + deleteAttr + ": " + e.getMessage(), output);
|
110 |
| } |
111 |
0
| } else if (custom != null && custom.length() > 0 && col.isMetaEnabled()) {
|
112 |
0
| try {
|
113 |
0
| MetaData customMeta = col.getCollectionMeta();
|
114 |
0
| Document customDoc = DOMParser.toDocument(custom);
|
115 |
0
| customMeta.setCustomDocument(customDoc);
|
116 |
0
| col.setCollectionMeta(customMeta);
|
117 |
| } catch (DBException e) { |
118 |
0
| log.error(e);
|
119 |
0
| printError("Cannot add/update Custom: " + e.getMessage(), output);
|
120 |
| } catch (XindiceException e) { |
121 |
0
| log.error(e);
|
122 |
0
| printError("Cannot add/update Custom: " + e.getMessage(), output);
|
123 |
| } |
124 |
0
| } else if (deleteCustom != null && deleteCustom.length() > 0 && col.isMetaEnabled()) {
|
125 |
0
| try {
|
126 |
0
| MetaData customMeta = col.getCollectionMeta();
|
127 |
0
| if (customMeta != null) {
|
128 |
0
| customMeta.setCustomDocument(null);
|
129 |
0
| col.setCollectionMeta(customMeta);
|
130 |
| } |
131 |
| } catch (DBException e) { |
132 |
0
| log.error(e);
|
133 |
0
| printError("Cannot delete Custom: " + e.getMessage(), output);
|
134 |
| } |
135 |
0
| } else if (req.getMethod().equalsIgnoreCase("POST")) {
|
136 |
0
| byte[] content = new byte[0];
|
137 |
0
| try {
|
138 |
| |
139 |
0
| ServletFileUpload upload = new ServletFileUpload();
|
140 |
| |
141 |
0
| upload.setSizeMax(-1);
|
142 |
0
| upload.setFileItemFactory(new DiskFileItemFactory());
|
143 |
| |
144 |
| |
145 |
0
| Iterator i = upload.parseRequest(req).iterator();
|
146 |
0
| while (i.hasNext()) {
|
147 |
0
| FileItem item = (FileItem) i.next();
|
148 |
0
| if (!item.isFormField()) {
|
149 |
0
| if (item.getFieldName().equals("customfile")) {
|
150 |
0
| content = item.get();
|
151 |
| } |
152 |
| } |
153 |
0
| item.delete();
|
154 |
| } |
155 |
| } catch (FileUploadException e) { |
156 |
0
| log.error(e);
|
157 |
| } |
158 |
| |
159 |
0
| if (content.length > 0) {
|
160 |
0
| Document customDoc;
|
161 |
0
| try {
|
162 |
0
| customDoc = DOMParser.toDocument(content);
|
163 |
| } catch (XindiceException e1) { |
164 |
0
| log.error(e1);
|
165 |
0
| StringBuffer newContent = new StringBuffer();
|
166 |
0
| newContent.append("<custom>").append(new String(content)).append("</custom>");
|
167 |
0
| try {
|
168 |
0
| customDoc = DOMParser.toDocument(newContent.toString());
|
169 |
| } catch (XindiceException e2) { |
170 |
0
| log.error(e2);
|
171 |
0
| printError("Cannot parse Custom: " + e2.getMessage(), output);
|
172 |
0
| customDoc = null;
|
173 |
| } |
174 |
| } |
175 |
0
| if (customDoc != null) {
|
176 |
0
| if (!customDoc.getDocumentElement().getNodeName().equals("custom")) {
|
177 |
0
| Document newCustomDoc = new DocumentImpl();
|
178 |
0
| Element customEle = newCustomDoc.createElement("custom");
|
179 |
0
| customEle.appendChild(customDoc.getDocumentElement());
|
180 |
0
| newCustomDoc.appendChild(customEle);
|
181 |
0
| customDoc = newCustomDoc;
|
182 |
| } |
183 |
0
| try {
|
184 |
0
| MetaData customMeta = col.getCollectionMeta();
|
185 |
0
| customMeta.setCustomDocument(customDoc);
|
186 |
0
| col.setCollectionMeta(customMeta);
|
187 |
| } catch (DBException e2) { |
188 |
0
| log.error(e2);
|
189 |
0
| printError("Cannot create Custom: " + e2.getMessage(), output);
|
190 |
| } |
191 |
| } |
192 |
| } |
193 |
| } |
194 |
| |
195 |
0
| printStartTable("Add/Update Metadata Attribute", output);
|
196 |
0
| printStartSingleTableBox(output);
|
197 |
0
| output.write(META_ATTR_UPDATE_FORM_START);
|
198 |
0
| output.write(attrName);
|
199 |
0
| output.write(META_ATTR_UPDATE_FORM_MIDDLE);
|
200 |
0
| output.write(attrValue);
|
201 |
0
| output.write(META_ATTR_UPDATE_FORM_END);
|
202 |
0
| printEndSingleTableBox(output);
|
203 |
0
| printEndTable(output);
|
204 |
0
| output.write("<br />\n");
|
205 |
| |
206 |
0
| if (col.isMetaEnabled()) {
|
207 |
0
| MetaData metaData;
|
208 |
0
| try {
|
209 |
0
| metaData = col.getCollectionMeta();
|
210 |
| } catch (DBException e) { |
211 |
0
| log.error(e);
|
212 |
0
| metaData = null;
|
213 |
| } |
214 |
0
| if (metaData != null) {
|
215 |
0
| printAttributes(metaData, output);
|
216 |
0
| output.write("<br />\n");
|
217 |
0
| printCustom(metaData, output);
|
218 |
| } |
219 |
| } |
220 |
| |
221 |
0
| finishViewer(output);
|
222 |
| } |
223 |
| |
224 |
0
| private void printAttributes(MetaData meta, Writer output) throws IOException {
|
225 |
| |
226 |
0
| String[] attrTitle = {"Attribute Name", "Attribute Value", "delete Attribute"};
|
227 |
0
| if (meta.getAttributeKeys().hasMoreElements()) {
|
228 |
0
| printStartTable(attrTitle, output);
|
229 |
0
| for (Enumeration e = meta.getAttributeKeys(); e.hasMoreElements();) {
|
230 |
0
| String name = (String) e.nextElement();
|
231 |
0
| String value = (String) meta.getAttribute(name);
|
232 |
0
| String[] contents = {
|
233 |
| "<a href=\"?viewer=" + getName() + "&updateattr=" + name + "\">" |
234 |
| + name + "</a>", |
235 |
| value, |
236 |
| "<a href=\"?viewer=" + getName() + "&deleteattr=" + name + "\">" |
237 |
| + "delete</a>"}; |
238 |
0
| printTableRow(contents, output);
|
239 |
| } |
240 |
0
| printEndTable(output);
|
241 |
| } else { |
242 |
0
| String message = "No Attributes available!";
|
243 |
0
| printStartTable(message, output);
|
244 |
0
| printStartSingleTableBox(output);
|
245 |
0
| output.write(message + "\n");
|
246 |
0
| printEndSingleTableBox(output);
|
247 |
0
| printEndTable(output);
|
248 |
| } |
249 |
| } |
250 |
| |
251 |
0
| private void printCustom(MetaData meta, Writer output) throws IOException {
|
252 |
0
| Document custom = meta.getCustomDocument();
|
253 |
0
| printStartTable("Add/Update MetaData Custom", output);
|
254 |
0
| printStartSingleTableBox(output);
|
255 |
0
| output.write(META_CUSTOM_UPDATE_FORM_START);
|
256 |
0
| if (custom != null && custom.getDocumentElement() != null) {
|
257 |
0
| Element customEle = custom.getDocumentElement();
|
258 |
0
| if (!customEle.hasChildNodes()) {
|
259 |
0
| Document helper = new DocumentImpl();
|
260 |
0
| customEle
|
261 |
| .appendChild(helper.createTextNode("\n\n")); |
262 |
| } |
263 |
0
| TextWriter.write(custom.getDocumentElement(), output);
|
264 |
| } |
265 |
0
| output.write(META_CUSTOM_UPDATE_FORM_END);
|
266 |
0
| printEndSingleTableBox(output);
|
267 |
0
| printStartSingleTableBox(output);
|
268 |
0
| output.write("<a href=\"?viewer=" + getName() + "&deleteCustom=true\">\n");
|
269 |
0
| output.write("delete Custom</a>\n");
|
270 |
0
| printEndSingleTableBox(output);
|
271 |
0
| printStartSingleTableBox(output);
|
272 |
0
| output.write(META_CUSTOM_UPLOAD_FORM);
|
273 |
0
| printEndSingleTableBox(output);
|
274 |
0
| printEndTable(output);
|
275 |
0
| output.write("<br />\n");
|
276 |
0
| printStartTable("MetaData Custom", output);
|
277 |
0
| printStartSingleTableBox(output);
|
278 |
0
| if (custom != null && custom.getDocumentElement() != null) {
|
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
0
| Xml2HtmlWriter.write(custom.getDocumentElement(), output);
|
285 |
| } else { |
286 |
0
| output.write("No Custom Document!\n");
|
287 |
| } |
288 |
0
| printEndSingleTableBox(output);
|
289 |
0
| printEndTable(output);
|
290 |
| } |
291 |
| |
292 |
0
| private void printError(String message, Writer output) throws IOException {
|
293 |
0
| printStartTable("Error", output);
|
294 |
0
| printStartSingleTableBox(output);
|
295 |
0
| output.write(message + "\n");
|
296 |
0
| printEndSingleTableBox(output);
|
297 |
0
| printEndTable(output);
|
298 |
0
| output.write("<br />\n");
|
299 |
| } |
300 |
| |
301 |
| private static final String META_ATTR_UPDATE_FORM_START = |
302 |
| "<form action=\"\" method=\"get\"> \n" + |
303 |
| "<input name=\"viewer\" value=\"meta\" type=\"hidden\" /> \n" + |
304 |
| "<table class=\"upload-form\"> \n" + |
305 |
| "<tr><td> name: </td><td><input name=\"attrname\" type=\"text\" value=\""; |
306 |
| |
307 |
| private static final String META_ATTR_UPDATE_FORM_MIDDLE = |
308 |
| "\"/></td></tr> \n" + |
309 |
| "<tr><td> value: </td><td><input name=\"attrvalue\" type=\"text\" value=\""; |
310 |
| |
311 |
| private static final String META_ATTR_UPDATE_FORM_END = |
312 |
| "\"/></td></tr><tr><td>\n" + |
313 |
| "<input type=\"submit\" name=\"submit\" value=\"add/update\" /> \n" + |
314 |
| "</td></tr></table></form> \n"; |
315 |
| |
316 |
| private static final String META_CUSTOM_UPDATE_FORM_START = |
317 |
| "<form action=\"\" method=\"get\"> \n" + |
318 |
| "<input name=\"viewer\" value=\"meta\" type=\"hidden\" /> \n" + |
319 |
| "<table class=\"upload-form\"> \n" + |
320 |
| "<tr><td> \n" + |
321 |
| "<textarea name=\"custom\" cols=\"80\" rows=\"8\">\n"; |
322 |
| |
323 |
| private static final String META_CUSTOM_UPDATE_FORM_END = |
324 |
| "</textarea> \n" + |
325 |
| "</td></tr><tr><td> \n" + |
326 |
| "<input type=\"submit\" name=\"submit\" value=\"add/update\" /> \n" + |
327 |
| "</td></tr></table></form> \n"; |
328 |
| |
329 |
| private static final String META_CUSTOM_UPLOAD_FORM = |
330 |
| "<form action=\"?viewer=meta\" method=\"post\" " + |
331 |
| "enctype=\"multipart/form-data\"> \n" + |
332 |
| "<table class=\"upload-form\"> \n" + |
333 |
| "<tr><td>Upload Custom file: </td> \n" + |
334 |
| "<td><input name=\"customfile\" type=\"file\" /></td> \n" + |
335 |
| "<td><input type=\"submit\" name=\"submit\" value=\"upload\" /> \n" + |
336 |
| "</td></tr> " + |
337 |
| "</table> \n" + |
338 |
| "</form> \n"; |
339 |
| |
340 |
0
| protected String getName() {
|
341 |
0
| return "meta";
|
342 |
| } |
343 |
| } |