Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 239   Methods: 9
NCLOC: 183   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
UploadCollectionViewer.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: UploadCollectionViewer.java 711958 2008-11-06 20:12:54Z natalia $
 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.FaultCodes;
 31    import org.apache.xindice.util.XindiceException;
 32    import org.apache.xindice.util.StringUtilities;
 33    import org.apache.xindice.webadmin.viewer.HtmlCollectionViewer;
 34    import org.apache.xindice.webadmin.viewer.HtmlViewerComponent;
 35    import org.apache.xindice.xml.dom.DOMParser;
 36    import org.w3c.dom.Document;
 37   
 38    import javax.servlet.ServletException;
 39    import javax.servlet.http.HttpServletRequest;
 40    import javax.servlet.http.HttpServletResponse;
 41    import java.io.IOException;
 42    import java.io.Writer;
 43    import java.util.Iterator;
 44   
 45    /**
 46    * Xindice Html Viewer for uploading documents to this collection.
 47    *
 48    * @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
 49    * @version $Revision: 711958 $, $Date: 2008-11-06 20:12:54 +0000 (Thu, 06 Nov 2008) $
 50    */
 51    public class UploadCollectionViewer extends HtmlViewerComponent implements HtmlCollectionViewer {
 52   
 53    protected static final String INLINE_METADATA = "inline-metadata";
 54    private static final Log log = LogFactory.getLog(UploadCollectionViewer.class);
 55   
 56  0 public void execute(HttpServletRequest req, HttpServletResponse res, Collection col)
 57    throws ServletException, IOException {
 58   
 59  0 String path = col.getCanonicalName();
 60  0 String title = "Upload Document to Collection " + path;
 61   
 62  0 Writer output = startViewer(title, path, res);
 63  0 printStartTable(title, output);
 64  0 printStartSingleTableBox(output);
 65   
 66  0 String method = req.getMethod();
 67  0 if (!method.equalsIgnoreCase("POST")) {
 68  0 output.write(UPLOAD_FORM);
 69    } else {
 70    // Create a new file upload handler
 71  0 ServletFileUpload upload = new ServletFileUpload();
 72    // Set upload parameters
 73  0 upload.setSizeMax(-1);
 74  0 upload.setHeaderEncoding(req.getCharacterEncoding());
 75  0 upload.setFileItemFactory(new DiskFileItemFactory());
 76   
 77  0 Form form;
 78  0 try {
 79  0 form = parseUpload(req, upload);
 80    } catch (FileUploadException e) {
 81  0 log.error(e);
 82  0 throw new ServletException(e);
 83    }
 84   
 85  0 if (form.content == null && form.content.length == 0) {
 86  0 throw new ServletException("empty upload file");
 87    }
 88   
 89  0 if (form.type != 'b') { // xml or detect
 90  0 handleXML(col, form, output);
 91    } else {
 92  0 handleBinary(col, form, output);
 93    }
 94   
 95    }
 96  0 printEndSingleTableBox(output);
 97  0 printEndTable(output);
 98  0 finishViewer(output);
 99    }
 100   
 101  0 private Form parseUpload(HttpServletRequest req, ServletFileUpload upload) throws FileUploadException, IOException {
 102  0 Form form = new Form();
 103   
 104    // Process the uploaded fields
 105  0 Iterator i = upload.parseRequest(req).iterator();
 106  0 while (i.hasNext()) {
 107  0 FileItem item = (FileItem) i.next();
 108  0 if (!item.isFormField()) {
 109  0 if (item.getFieldName().equals("file")) {
 110  0 form.content = item.get();
 111    }
 112    } else {
 113  0 if (item.getFieldName().equals("overwrite")) {
 114  0 form.overwrite = (Boolean.getBoolean(item.getString()));
 115  0 } else if (item.getFieldName().equals("type")) {
 116  0 form.type = item.getString().charAt(0);
 117  0 } else if (item.getFieldName().equals("name")) {
 118  0 form.name = item.getString(req.getCharacterEncoding());
 119    }
 120    }
 121  0 item.delete();
 122    }
 123   
 124  0 return form;
 125    }
 126   
 127  0 private void handleXML(Collection col, Form form, Writer out) throws IOException {
 128  0 try {
 129  0 insertXML(col, form.name, form.content, form.overwrite, out);
 130    } catch (XindiceException e) {
 131  0 if (form.type == 'd') {
 132  0 try {
 133  0 insertBinary(col, form.name, form.content, form.overwrite, out);
 134    } catch (DBException e1) {
 135  0 log.error(e1);
 136  0 printError("Could not create Binary!", e1.getMessage(), out);
 137    }
 138    } else {
 139  0 printError("Not a valid xml", e.getMessage(), out);
 140    }
 141    }
 142    }
 143   
 144  0 private void handleBinary(Collection col, Form form, Writer out) throws IOException {
 145  0 try {
 146  0 insertBinary(col, form.name, form.content, form.overwrite, out);
 147    } catch (DBException e) {
 148  0 log.error(e);
 149  0 printError("Could not create Binary!", e.getMessage(), out);
 150    }
 151    }
 152   
 153  0 protected String getName() {
 154  0 return "upload";
 155    }
 156   
 157  0 private void printMetaError(Writer output) throws IOException {
 158  0 output.write("<b>Metadata storage is not enabled!<br />");
 159  0 output.write("If you want to store binary content please ");
 160  0 output.write("enable metadata storage inside the server.xml");
 161  0 output.write(" and enable Inline Metadata inside this ");
 162  0 output.write("Collection Configuration!</b>");
 163    }
 164   
 165  0 private void printError(String info, String message, Writer output) throws IOException {
 166  0 output.write("<b>");
 167  0 output.write(info);
 168  0 output.write("</b><br />");
 169  0 output.write(message);
 170    }
 171   
 172  0 private void insertXML(Collection col, String name, byte[] content, boolean overwrite, Writer out)
 173    throws XindiceException, IOException {
 174  0 Document resource = DOMParser.toDocument(content);
 175  0 if (name.trim().length() == 0) {
 176  0 name = null;
 177    }
 178   
 179  0 if (!overwrite) {
 180  0 try {
 181  0 name = col.insertDocument(name, resource).toString();
 182    } catch (DBException e) {
 183  0 if (e.faultCode == FaultCodes.COL_DUPLICATE_RESOURCE) {
 184  0 printError("Could not create Resource!", name + " already exists!", out);
 185    } else {
 186  0 throw e;
 187    }
 188    }
 189    } else {
 190  0 col.setDocument(name, resource);
 191    }
 192  0 out.write("<b>Resource <a href=\"./");
 193  0 out.write(StringUtilities.escapePath(name));
 194  0 out.write("?viewer=default\">");
 195  0 out.write(name);
 196  0 out.write("</a> created!</b>");
 197    }
 198   
 199  0 private void insertBinary(Collection col, String name, byte[] content, boolean overwrite, Writer out)
 200    throws DBException, IOException {
 201   
 202  0 boolean inlineMetaSupported = col.getConfig().getBooleanAttribute(INLINE_METADATA, false);
 203  0 if (!inlineMetaSupported) {
 204  0 printMetaError(out);
 205    } else {
 206  0 if (name.trim().length() == 0) {
 207  0 name = null;
 208    }
 209   
 210  0 if (!overwrite) {
 211  0 try {
 212  0 name = col.insertBinary(name, content).toString();
 213    } catch (DBException e) {
 214  0 if (e.faultCode == FaultCodes.COL_DUPLICATE_RESOURCE) {
 215  0 printError("Could not update Binary!", name + " already exists!", out);
 216    } else {
 217  0 throw e;
 218    }
 219    }
 220    } else {
 221  0 col.setBinary(name, content);
 222    }
 223   
 224  0 out.write("<b>Binary <a href=\"./");
 225  0 out.write(name);
 226  0 out.write("?viewer=default\">");
 227  0 out.write(name);
 228  0 out.write("</a> created!</b>");
 229    }
 230    }
 231   
 232    private class Form {
 233    public byte[] content = null;
 234    public String name = null;
 235    public boolean overwrite = false;
 236    public char type = 'd';
 237    }
 238   
 239    }