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.HtmlResourceViewer; |
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.Iterator; |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| public class MetadataResourceViewer extends HtmlViewerComponent implements HtmlResourceViewer { |
56 |
| |
57 |
| private static final Log log = LogFactory.getLog(MetadataCollectionViewer.class); |
58 |
| |
59 |
0
| public void execute(HttpServletRequest req, HttpServletResponse res, Collection col, String name)
|
60 |
| throws ServletException, IOException { |
61 |
| |
62 |
0
| String path = col.getCanonicalDocumentName(name);
|
63 |
0
| String title = "Resource Metadata for " + path;
|
64 |
| |
65 |
0
| Writer output = startViewer(title, path, res);
|
66 |
| |
67 |
0
| String custom = req.getParameter("custom");
|
68 |
0
| String deleteCustom = req.getParameter("deleteCustom");
|
69 |
| |
70 |
0
| if (custom != null && custom.length() > 0 && col.isMetaEnabled()) {
|
71 |
0
| try {
|
72 |
0
| MetaData customMeta = col.getDocumentMeta(name);
|
73 |
0
| Document customDoc = DOMParser.toDocument(custom);
|
74 |
0
| customMeta.setCustomDocument(customDoc);
|
75 |
0
| col.setDocumentMeta(name, customMeta);
|
76 |
| } catch (DBException e) { |
77 |
0
| log.error(e);
|
78 |
0
| printError("Cannot add/update Custom: " + e.getMessage(), output);
|
79 |
| } catch (XindiceException e) { |
80 |
0
| log.error(e);
|
81 |
0
| printError("Cannot add/update Custom: " + e.getMessage(), output);
|
82 |
| } |
83 |
0
| } else if (deleteCustom != null && deleteCustom.length() > 0 && col.isMetaEnabled()) {
|
84 |
0
| try {
|
85 |
0
| MetaData customMeta = col.getDocumentMeta(name);
|
86 |
0
| if (customMeta != null) {
|
87 |
0
| customMeta.setCustomDocument(null);
|
88 |
0
| col.setDocumentMeta(name, customMeta);
|
89 |
| } |
90 |
| } catch (DBException e) { |
91 |
0
| log.error(e);
|
92 |
0
| printError("Cannot delete Custom: " + e.getMessage(), output);
|
93 |
| } |
94 |
0
| } else if (req.getMethod().equalsIgnoreCase("POST")) {
|
95 |
0
| byte[] content = new byte[0];
|
96 |
0
| try {
|
97 |
| |
98 |
0
| ServletFileUpload upload = new ServletFileUpload();
|
99 |
| |
100 |
0
| upload.setSizeMax(-1);
|
101 |
0
| upload.setFileItemFactory(new DiskFileItemFactory());
|
102 |
| |
103 |
| |
104 |
0
| Iterator i = upload.parseRequest(req).iterator();
|
105 |
0
| while (i.hasNext()) {
|
106 |
0
| FileItem item = (FileItem) i.next();
|
107 |
0
| if (!item.isFormField()) {
|
108 |
0
| if (item.getFieldName().equals("customfile")) {
|
109 |
0
| content = item.get();
|
110 |
| } |
111 |
| } |
112 |
0
| item.delete();
|
113 |
| } |
114 |
| } catch (FileUploadException e) { |
115 |
0
| log.error(e);
|
116 |
| } |
117 |
| |
118 |
0
| if (content.length > 0) {
|
119 |
0
| Document customDoc;
|
120 |
0
| try {
|
121 |
0
| customDoc = DOMParser.toDocument(content);
|
122 |
| } catch (XindiceException e1) { |
123 |
0
| log.error(e1);
|
124 |
0
| StringBuffer newContent = new StringBuffer();
|
125 |
0
| newContent.append("<custom>").append(new String(content)).append("</custom>");
|
126 |
0
| try {
|
127 |
0
| customDoc = DOMParser.toDocument(newContent.toString());
|
128 |
| } catch (XindiceException e2) { |
129 |
0
| log.error(e2);
|
130 |
0
| printError("Cannot parse Custom: " + e2.getMessage(), output);
|
131 |
0
| customDoc = null;
|
132 |
| } |
133 |
| } |
134 |
0
| if (customDoc != null) {
|
135 |
0
| if (!customDoc.getDocumentElement().getNodeName().equals("custom")) {
|
136 |
0
| Document newCustomDoc = new DocumentImpl();
|
137 |
0
| Element customEle = newCustomDoc.createElement("custom");
|
138 |
0
| customEle.appendChild(customDoc.getDocumentElement());
|
139 |
0
| newCustomDoc.appendChild(customEle);
|
140 |
0
| customDoc = newCustomDoc;
|
141 |
| } |
142 |
0
| try {
|
143 |
0
| MetaData customMeta = col.getDocumentMeta(name);
|
144 |
0
| customMeta.setCustomDocument(customDoc);
|
145 |
0
| col.setDocumentMeta(name, customMeta);
|
146 |
| } catch (DBException e2) { |
147 |
0
| log.error(e2);
|
148 |
0
| printError("Cannot create Custom: " + e2.getMessage(), output);
|
149 |
| } |
150 |
| } |
151 |
| } |
152 |
| } |
153 |
| |
154 |
0
| if (col.isMetaEnabled()) {
|
155 |
0
| MetaData metaData;
|
156 |
0
| try {
|
157 |
0
| metaData = col.getDocumentMeta(name);
|
158 |
| } catch (DBException e) { |
159 |
0
| log.error(e);
|
160 |
0
| metaData = null;
|
161 |
| } |
162 |
0
| if (metaData != null) {
|
163 |
0
| printCustom(metaData, output);
|
164 |
| } |
165 |
| } |
166 |
| |
167 |
0
| finishViewer(output);
|
168 |
| } |
169 |
| |
170 |
0
| private void printCustom(MetaData meta, Writer output) throws IOException {
|
171 |
0
| Document custom = meta.getCustomDocument();
|
172 |
0
| printStartTable("Add/Update MetaData Custom", output);
|
173 |
0
| printStartSingleTableBox(output);
|
174 |
0
| output.write("<a href=\"./?viewer=default\">up to parent collection</a>");
|
175 |
0
| printEndSingleTableBox(output);
|
176 |
0
| printStartSingleTableBox(output);
|
177 |
0
| output.write(META_CUSTOM_UPDATE_FORM_START);
|
178 |
0
| if (custom != null && custom.getDocumentElement() != null) {
|
179 |
0
| Element customEle = custom.getDocumentElement();
|
180 |
0
| if (!customEle.hasChildNodes()) {
|
181 |
0
| Document helper = new DocumentImpl();
|
182 |
0
| customEle.appendChild(helper.createTextNode("\n\n"));
|
183 |
| } |
184 |
0
| TextWriter.write(custom.getDocumentElement(), output);
|
185 |
| } |
186 |
0
| output.write(META_CUSTOM_UPDATE_FORM_END);
|
187 |
0
| printEndSingleTableBox(output);
|
188 |
0
| printStartSingleTableBox(output);
|
189 |
0
| output.write("<a href=\"?viewer=" + getName() + "&deleteCustom=true\">\n");
|
190 |
0
| output.write("delete Custom</a>\n");
|
191 |
0
| printEndSingleTableBox(output);
|
192 |
0
| printStartSingleTableBox(output);
|
193 |
0
| output.write(META_CUSTOM_UPLOAD_FORM);
|
194 |
0
| printEndSingleTableBox(output);
|
195 |
0
| printEndTable(output);
|
196 |
0
| output.write("<br />\n");
|
197 |
0
| printStartTable("MetaData Custom", output);
|
198 |
0
| printStartSingleTableBox(output);
|
199 |
0
| if (custom != null && custom.getDocumentElement() != null) {
|
200 |
0
| Xml2HtmlWriter.write(custom.getDocumentElement(), output);
|
201 |
| } else { |
202 |
0
| output.write("No Custom Document!\n");
|
203 |
| } |
204 |
0
| printEndSingleTableBox(output);
|
205 |
0
| printEndTable(output);
|
206 |
| } |
207 |
| |
208 |
0
| private void printError(String message, Writer output) throws IOException {
|
209 |
0
| printStartTable("Error", output);
|
210 |
0
| printStartSingleTableBox(output);
|
211 |
0
| output.write(message);
|
212 |
0
| printEndSingleTableBox(output);
|
213 |
0
| printEndTable(output);
|
214 |
0
| output.write("<br />\n");
|
215 |
| } |
216 |
| |
217 |
| private static final String META_CUSTOM_UPDATE_FORM_START = |
218 |
| "<form action=\"\" method=\"get\"> \n" + |
219 |
| "<input name=\"viewer\" value=\"meta\" type=\"hidden\" /> \n" + |
220 |
| "<table class=\"upload-form\"> \n" + |
221 |
| "<tr><td> \n" + |
222 |
| "<textarea name=\"custom\" cols=\"80\" rows=\"8\">\n"; |
223 |
| |
224 |
| private static final String META_CUSTOM_UPDATE_FORM_END = |
225 |
| "</textarea> \n" + |
226 |
| "</td></tr><tr><td> \n" + |
227 |
| "<input type=\"submit\" name=\"submit\" value=\"add/update\" /> \n" + |
228 |
| "</td></tr></table></form> \n"; |
229 |
| |
230 |
| private static final String META_CUSTOM_UPLOAD_FORM = |
231 |
| "<form action=\"?viewer=meta\" method=\"post\" " + |
232 |
| "enctype=\"multipart/form-data\"> \n" + |
233 |
| "<table class=\"upload-form\"> \n" + |
234 |
| "<tr><td>Upload Custom file: </td> \n" + |
235 |
| "<td><input name=\"customfile\" type=\"file\" /></td> \n" + |
236 |
| "<td><input type=\"submit\" name=\"submit\" value=\"upload\" /> \n" + |
237 |
| "</td></tr> " + |
238 |
| "</table> \n" + |
239 |
| "</form> \n"; |
240 |
| |
241 |
0
| protected String getName() {
|
242 |
0
| return "meta";
|
243 |
| } |
244 |
| } |