Example usage for javax.naming NamingException printStackTrace

List of usage examples for javax.naming NamingException printStackTrace

Introduction

In this page you can find the example usage for javax.naming NamingException printStackTrace.

Prototype

public void printStackTrace(PrintStream s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print stream.

Usage

From source file:com.zeroio.webdav.WebdavServlet.java

/**
 * Delete a resource.//ww w  .  ja va2 s  .  co m
 *
 * @param path      Path of the resource which is to be deleted
 * @param setStatus Should the response status be set on
 *                  successful completion
 * @param context   Description of the Parameter
 * @param db        Description of the Parameter
 * @return Description of the Return Value
 * @throws ServletException Description of the Exception
 * @throws IOException      Description of the Exception
 * @throws SQLException     Description of the Exception
 */
private boolean deleteResource(ActionContext context, Connection db, String path, boolean setStatus)
        throws SQLException, ServletException, IOException {

    if ((path.toUpperCase().startsWith("/WEB-INF")) || (path.toUpperCase().startsWith("/META-INF"))) {
        context.getResponse().sendError(WebdavStatus.SC_FORBIDDEN);
        return false;
    }

    String ifHeader = context.getRequest().getHeader("If");
    if (ifHeader == null) {
        ifHeader = "";
    }

    String lockTokenHeader = context.getRequest().getHeader("Lock-Token");
    if (lockTokenHeader == null) {
        lockTokenHeader = "";
    }

    if (isLocked(path, ifHeader + lockTokenHeader)) {
        context.getResponse().sendError(WebdavStatus.SC_LOCKED);
        return false;
    }

    //Check to see if the delete is being issued due to a copy or a move action
    String destinationPath = context.getRequest().getHeader("Destination");
    if (destinationPath != null) {
        System.out.println("DELETION DUE TO COPY OR MOVE ACTION....");
    }

    // Retrieve the resources
    ModuleContext resources = getCFSResources(db, context);
    SystemStatus thisSystem = this.getSystemStatus(context);

    if (resources == null) {
        context.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return false;
    }

    Object object = null;
    boolean exists = true;

    try {
        //lookup the object at path. If it is a top level folder fail otherwise proceed
        object = resources.lookup(thisSystem, db, path);

        if (getWebdavManager(context).isTopLevelModule(object)) {
            //context.getResponse().sendError(WebdavStatus.SC_FORBIDDEN);
            return false;
        }
    } catch (NamingException e) {
        exists = false;
    }

    if (!exists) {
        context.getResponse().sendError(WebdavStatus.SC_NOT_FOUND);
        return false;
    }

    boolean collection = (object instanceof ModuleContext);

    if (!collection) {
        try {
            resources.unbind(thisSystem, db, path);
        } catch (NamingException e) {
            //e.printStackTrace(System.out);
            context.getResponse().sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
            return false;
        }
    } else {
        Hashtable errorList = new Hashtable();
        //System.out.println("Calling deleteCollection(.....) .......");
        deleteCollection(thisSystem, db, context.getRequest(), resources, path, errorList);
        try {
            resources.unbind(thisSystem, db, path);
        } catch (NamingException e) {
            e.printStackTrace(System.out);
            errorList.put(path, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
        } catch (SQLException sql) {
            sql.printStackTrace(System.out);
            throw new SQLException(sql.getMessage());
        }
        if (!errorList.isEmpty()) {
            sendReport(context.getRequest(), context.getResponse(), errorList);
            return false;
        }
    }
    if (setStatus) {
        context.getResponse().setStatus(WebdavStatus.SC_NO_CONTENT);
    }
    return true;
}

From source file:org.apache.catalina.servlets.DefaultServlet.java

/**
 * Process a POST request for the specified resource.
 *
 * @throws IOException      if an input/output error occurs
 * @throws ServletException if a servlet-specified error occurs
 *///from   w  w w. jav a  2s .c  o m
protected void doPut(ActionContext context) throws ServletException, IOException {

    //showRequestInfo(context.getRequest());

    if (readOnly) {
        context.getResponse().sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    String path = getRelativePath(context.getRequest());

    //Fix for MACOSX finder. Do not allow requests for files starting with a period
    if (path.indexOf("/.") > -1 || path.indexOf(".DS_Store") > -1) {
        return;
    }

    // Retrieve the resources
    Connection db = null;
    ModuleContext resources = null;
    SystemStatus thisSystem = null;
    Object object = null;

    boolean exists = true;
    boolean result = true;

    try {
        db = this.getConnection(context);
        resources = getCFSResources(db, context);

        if (resources == null) {
            context.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }

        thisSystem = this.getSystemStatus(context);

        try {
            object = resources.lookup(thisSystem, db, path);
        } catch (NamingException e) {
            exists = false;
        }

        // Temp. content file used to support partial PUT
        File contentFile = null;

        // Input stream for temp. content file used to support partial PUT
        FileInputStream contentFileInStream = null;

        //ResourceInfo resourceInfo = new ResourceInfo(thisSystem, path, resources);
        Range range = parseContentRange(context.getRequest(), context.getResponse());

        //InputStream resourceInputStream = null;
        ServletInputStream resourceInputStream = null;
        // Append data specified in ranges to existing content for this
        // resource - create a temp. file on the local filesystem to
        // perform this operation
        // Assume just one range is specified for now
        if (range != null) {
            contentFile = executePartialPut(context.getRequest(), range, path);
            //resourceInputStream = new FileInputStream(contentFile);
        } else {
            resourceInputStream = context.getRequest().getInputStream();
            //System.out.println("RESOURCE INPUT STREAM: " + resourceInputStream);
            System.out.println("CONTENT LENGTH: " + context.getRequest().getContentLength());
            //System.out.println("DATA: " + resourceInputStream.available());
        }

        try {
            Object thisObject = null;
            if (exists) {
                //resources.rebind(path, newResource);
                Resource oldResource = (Resource) object;
                oldResource.setContent(resourceInputStream);
                thisObject = resources.copyResource(thisSystem, db, path, oldResource);
            } else {
                Resource newResource = new Resource(resourceInputStream);
                thisObject = resources.copyResource(thisSystem, db, path, newResource);
            }
            if (thisObject != null) {
                processInsertHook(context, thisObject);
            }
        } catch (NamingException e) {
            //e.printStackTrace(System.out);
            result = false;
        }
    } catch (SQLException e) {
        e.printStackTrace(System.out);
    } finally {
        this.freeConnection(db, context);
    }

    if (result) {
        if (exists) {
            context.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
        } else {
            context.getResponse().setStatus(HttpServletResponse.SC_CREATED);
        }
    } else {
        context.getResponse().sendError(HttpServletResponse.SC_CONFLICT);
    }
}

From source file:com.concursive.connect.web.webdav.servlets.WebdavServlet.java

/**
 * PROPFIND Method./*www . j a v a  2 s .  c om*/
 *
 * @param context Description of the Parameter
 * @throws javax.servlet.ServletException Description of the Exception
 * @throws java.io.IOException            Description of the Exception
 */
protected void doPropfind(ActionContext context) throws ServletException, IOException {

    String path = getRelativePath(context.getRequest());
    //fix for windows clients
    if (path.equals("/files")) {
        path = "";
    }

    if (path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }

    if ((path.toUpperCase().startsWith("/WEB-INF")) || (path.toUpperCase().startsWith("/META-INF"))) {
        context.getResponse().sendError(WebdavStatus.SC_FORBIDDEN);
        return;
    }

    // Properties which are to be displayed.
    Vector properties = null;
    // Propfind depth
    int depth = INFINITY;
    // Propfind type
    int type = FIND_ALL_PROP;

    String depthStr = context.getRequest().getHeader("Depth");
    if (depthStr == null) {
        depth = INFINITY;
    } else {
        if (depthStr.equals("0")) {
            depth = 0;
        } else if (depthStr.equals("1")) {
            depth = 1;
        } else if (depthStr.equals("infinity")) {
            depth = INFINITY;
        }
    }

    /*
     *  Read the request xml and determine all the properties
     */

    /*
    Node propNode = null;
    DocumentBuilder documentBuilder = getDocumentBuilder();
    try {
      Document document = documentBuilder.parse
          (new InputSource(context.getRequest().getInputStream()));
      // Get the root element of the document
      Element rootElement = document.getDocumentElement();
      NodeList childList = rootElement.getChildNodes();
      for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        switch (currentNode.getNodeType()) {
          case Node.TEXT_NODE:
    break;
          case Node.ELEMENT_NODE:
    if (currentNode.getNodeName().endsWith("prop")) {
      type = FIND_BY_PROPERTY;
      propNode = currentNode;
    }
    if (currentNode.getNodeName().endsWith("propname")) {
      type = FIND_PROPERTY_NAMES;
    }
    if (currentNode.getNodeName().endsWith("allprop")) {
      type = FIND_ALL_PROP;
    }
    break;
        }
      }
    } catch (Exception e) {
      // Most likely there was no content : we use the defaults.
      // TODO : Enhance that !
      e.printStackTrace(System.out);
    }
            
    if (type == FIND_BY_PROPERTY) {
      properties = new Vector();
      NodeList childList = propNode.getChildNodes();
      for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        switch (currentNode.getNodeType()) {
          case Node.TEXT_NODE:
    break;
          case Node.ELEMENT_NODE:
    String nodeName = currentNode.getNodeName();
    String propertyName = null;
    if (nodeName.indexOf(':') != -1) {
      propertyName = nodeName.substring
          (nodeName.indexOf(':') + 1);
    } else {
      propertyName = nodeName;
    }
    // href is a live property which is handled differently
    properties.addElement(propertyName);
    break;
        }
      }
    }
    */
    // Properties have been determined
    // Retrieve the resources

    Connection db = null;
    boolean exists = true;
    boolean status = true;
    Object object = null;
    ModuleContext resources = null;
    StringBuffer xmlsb = new StringBuffer();
    try {
        db = this.getConnection(context);
        resources = getCFSResources(db, context);
        if (resources == null) {
            context.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
        object = resources.lookup(db, path);
    } catch (NamingException e) {
        //e.printStackTrace(System.out);
        exists = false;
        int slash = path.lastIndexOf('/');
        if (slash != -1) {
            String parentPath = path.substring(0, slash);
            Vector currentLockNullResources = (Vector) lockNullResources.get(parentPath);
            if (currentLockNullResources != null) {
                Enumeration lockNullResourcesList = currentLockNullResources.elements();
                while (lockNullResourcesList.hasMoreElements()) {
                    String lockNullPath = (String) lockNullResourcesList.nextElement();
                    if (lockNullPath.equals(path)) {
                        context.getResponse().setStatus(WebdavStatus.SC_MULTI_STATUS);
                        context.getResponse().setContentType("text/xml; charset=UTF-8");
                        // Create multistatus object
                        XMLWriter generatedXML = new XMLWriter(context.getResponse().getWriter());
                        generatedXML.writeXMLHeader();
                        generatedXML.writeElement(null, "multistatus" + generateNamespaceDeclarations(),
                                XMLWriter.OPENING);
                        parseLockNullProperties(context.getRequest(), generatedXML, lockNullPath, type,
                                properties);
                        generatedXML.writeElement(null, "multistatus", XMLWriter.CLOSING);
                        generatedXML.sendData();
                        //e.printStackTrace(System.out);
                        return;
                    }
                }
            }
        }
    } catch (SQLException e) {
        e.printStackTrace(System.out);
        context.getResponse().sendError(SQLERROR, e.getMessage());
        status = false;
    } finally {
        this.freeConnection(db, context);
    }

    if (!status) {
        return;
    }

    if (!exists) {
        context.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND, path);
        return;
    }

    context.getResponse().setStatus(WebdavStatus.SC_MULTI_STATUS);
    context.getResponse().setContentType("text/xml; charset=UTF-8");
    // Create multistatus object
    ////System.out.println("Creating Multistatus Object");

    XMLWriter generatedXML = new XMLWriter(context.getResponse().getWriter());
    generatedXML.writeXMLHeader();
    generatedXML.writeElement(null, "multistatus" + generateNamespaceDeclarations(), XMLWriter.OPENING);

    //System.out.println("Depth: " + depth);
    if (depth == 0) {
        parseProperties(context, resources, generatedXML, path, type, properties);
    } else {
        // The stack always contains the object of the current level
        Stack stack = new Stack();
        stack.push(path);
        // Stack of the objects one level below
        Stack stackBelow = new Stack();

        while ((!stack.isEmpty()) && (depth >= 0)) {
            String currentPath = (String) stack.pop();
            if (!currentPath.equals(path)) {
                parseProperties(context, resources, generatedXML, currentPath, type, properties);
            }
            try {
                db = this.getConnection(context);
                object = resources.lookup(db, currentPath);
            } catch (NamingException e) {
                continue;
            } catch (SQLException e) {
                //e.printStackTrace(System.out);
                context.getResponse().sendError(SQLERROR, e.getMessage());
                status = false;
            } finally {
                this.freeConnection(db, context);
            }

            if (!status) {
                return;
            }

            if ((object instanceof ModuleContext) && depth > 0) {
                // Get a list of all the resources at the current path and store them
                // in the stack
                try {
                    NamingEnumeration enum1 = resources.list(currentPath);
                    int count = 0;
                    while (enum1.hasMoreElements()) {
                        NameClassPair ncPair = (NameClassPair) enum1.nextElement();
                        String newPath = currentPath;
                        if (!(newPath.endsWith("/"))) {
                            newPath += "/";
                        }
                        newPath += ncPair.getName();
                        stackBelow.push(newPath);
                        count++;
                    }
                    if (currentPath.equals(path) && count == 0) {
                        // This directory does not have any files or folders.
                        parseProperties(context, resources, generatedXML, properties);
                    }
                } catch (NamingException e) {
                    //e.printStackTrace(System.out);
                    context.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path);
                    return;
                }

                // Displaying the lock-null resources present in that collection
                String lockPath = currentPath;
                if (lockPath.endsWith("/")) {
                    lockPath = lockPath.substring(0, lockPath.length() - 1);
                }
                Vector currentLockNullResources = (Vector) lockNullResources.get(lockPath);
                if (currentLockNullResources != null) {
                    Enumeration lockNullResourcesList = currentLockNullResources.elements();
                    while (lockNullResourcesList.hasMoreElements()) {
                        String lockNullPath = (String) lockNullResourcesList.nextElement();
                        System.out.println("Lock null path: " + lockNullPath);
                        parseLockNullProperties(context.getRequest(), generatedXML, lockNullPath, type,
                                properties);
                    }
                }
            }
            if (stack.isEmpty()) {
                depth--;
                stack = stackBelow;
                stackBelow = new Stack();
            }
            xmlsb.append(generatedXML.toString());
            //System.out.println("xml : " + generatedXML.toString());
            generatedXML.sendData();
        }
    }

    generatedXML.writeElement(null, "multistatus", XMLWriter.CLOSING);
    xmlsb.append(generatedXML.toString());
    generatedXML.sendData();
    //System.out.println("xml: " + xmlsb.toString());
}