Example usage for javax.servlet.http HttpServletRequest toString

List of usage examples for javax.servlet.http HttpServletRequest toString

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.webdavaccess.servlet.WebdavServlet.java

/**
 * DELETE Method.//www  . j  av a2 s  .  com
 * 
 * @param req
 *            HttpServletRequest
 * @param resp
 *            HttpServletResponse
 * @throws IOException
 *             if an error in the underlying store occurs
 */
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    if (!readOnly) {
        String path = getRelativePath(req);
        String lockOwner = "doDelete" + System.nanoTime() + req.toString();
        if (fResLocks.lock(path, lockOwner, true, -1)) {
            try {
                Hashtable errorList = new Hashtable();
                deleteResource(path, errorList, req, resp);
                if (!errorList.isEmpty()) {
                    sendReport(req, resp, errorList);
                }
            } catch (AccessDeniedException e) {
                log.error("WebdavServer not authenticated: ", e);
                resp.sendError(WebdavStatus.SC_FORBIDDEN);
            } catch (ObjectAlreadyExistsException e) {
                resp.sendError(WebdavStatus.SC_NOT_FOUND, req.getRequestURI());
            } catch (WebdavException e) {
                log.error("WebdavServer internal error: ", e);
                resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
            } finally {
                fResLocks.unlock(path, lockOwner);
            }
        } else {
            log.error("WebdavServer unable to lock resource " + lockOwner);
            resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
        }
    } else {
        log.error("WebdavServer not authenticated for write");
        resp.sendError(WebdavStatus.SC_FORBIDDEN);
    }

}

From source file:org.webdavaccess.servlet.WebdavServlet.java

/**
 * Process a POST request for the specified resource.
 * /*from  ww w. j av a2 s .  c o  m*/
 * @param req
 *            The servlet request we are processing
 * @param resp
 *            The servlet response we are creating
 * 
 * @exception WebdavException
 *                if an error in the underlying store occurs
 */
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (!readOnly) {
        String path = getRelativePath(req);
        String parentPath = getParentPath(path);
        String lockOwner = "doPut" + System.nanoTime() + req.toString();
        if (fResLocks.lock(path, lockOwner, true, -1)) {
            try {
                if (parentPath != null && fStore.isFolder(parentPath) && !fStore.isFolder(path)) {
                    if (!fStore.objectExists(path)) {
                        fStore.createResource(path);
                        resp.setStatus(HttpServletResponse.SC_CREATED);
                    } else {
                        resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
                    }
                    fStore.setResourceContent(path, req.getInputStream(), null, null);
                    resp.setContentLength((int) fStore.getResourceLength(path));
                } else {
                    resp.sendError(WebdavStatus.SC_CONFLICT);
                }
            } catch (AccessDeniedException e) {
                log.error("WebdavServer not authenticated: ", e);
                resp.sendError(WebdavStatus.SC_FORBIDDEN);
            } catch (WebdavException e) {
                log.error("WebdavServer internal error: ", e);
                resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
            } finally {
                fResLocks.unlock(path, lockOwner);
            }
        } else {
            log.error("WebdavServer unable to lock resource " + lockOwner);
            resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
        }
    } else {
        resp.sendError(WebdavStatus.SC_FORBIDDEN);
    }

}

From source file:org.webdavaccess.servlet.WebdavServlet.java

/**
 * COPY Method.//from w  ww .ja va2  s  . c  om
 * 
 * @param req
 *            HttpServletRequest
 * @param resp
 *            HttpServletResponse
 * @throws WebdavException
 *             if an error in the underlying store occurs
 * @throws IOException
 *             when an error occurs while sending the response
 */
protected void doCopy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String path = getRelativePath(req);
    if (!readOnly) {
        String lockOwner = "doCopy" + System.nanoTime() + req.toString();
        if (fResLocks.lock(path, lockOwner, false, -1)) {
            try {
                copyResource(req, resp);
            } catch (AccessDeniedException e) {
                log.error("WebdavServer not authenticated: ", e);
                resp.sendError(WebdavStatus.SC_FORBIDDEN);
            } catch (ObjectAlreadyExistsException e) {
                resp.sendError(WebdavStatus.SC_CONFLICT, req.getRequestURI());
            } catch (ObjectNotFoundException e) {
                resp.sendError(WebdavStatus.SC_NOT_FOUND, req.getRequestURI());
            } catch (WebdavException e) {
                log.error("WebdavServer internal error: ", e);
                resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
            } finally {
                fResLocks.unlock(path, lockOwner);
            }
        } else {
            log.error("WebdavServer unable to lock resource " + lockOwner);
            resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
        }

    } else {
        log.error("WebdavServer not authenticated for write");
        resp.sendError(WebdavStatus.SC_FORBIDDEN);
    }

}

From source file:org.webdavaccess.servlet.WebdavServlet.java

/**
 * MOVE Method./*from w ww . j a  va  2s  . co  m*/
 * 
 * @param req
 *            HttpServletRequest
 * @param resp
 *            HttpServletResponse
 * @throws ServletException
 * @throws WebdavException
 *             if an error in the underlying store occurs
 * @throws IOException
 *             when an error occurs while sending the response
 */
protected void doMove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (!readOnly) {

        String path = getRelativePath(req);
        String lockOwner = "doMove" + System.nanoTime() + req.toString();
        if (fResLocks.lock(path, lockOwner, false, -1)) {
            try {
                String destinationPath = copyResource(req, resp);
                if (destinationPath != null) {
                    Hashtable errorList = new Hashtable();
                    deleteResource(path, errorList, req, resp);
                    if (!errorList.isEmpty()) {
                        sendReport(req, resp, errorList);
                    }
                    if (fAliasManager != null)
                        fAliasManager.resourceMovedNotification(path, destinationPath);
                } else {
                    log.error("Destination directory in doMove cannot be empty");
                    resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
                }
            } catch (AccessDeniedException e) {
                log.error("WebdavServer not authenticated: ", e);
                resp.sendError(WebdavStatus.SC_FORBIDDEN);
            } catch (ObjectAlreadyExistsException e) {
                resp.sendError(WebdavStatus.SC_NOT_FOUND, req.getRequestURI());
            } catch (WebdavException e) {
                log.error("WebdavServer internal error: ", e);
                resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
            } finally {
                fResLocks.unlock(path, lockOwner);
            }
        } else {
            log.error("WebdavServer unable to lock resource " + lockOwner);
            resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
        }
    } else {
        log.error("WebdavServer not authenticated for write");
        resp.sendError(WebdavStatus.SC_FORBIDDEN);

    }
}

From source file:org.webdavaccess.servlet.WebdavServlet.java

/**
 * Copy a resource.//from www.  j a  va  2  s.  co m
 * 
 * @param req
 *            Servlet request
 * @param resp
 *            Servlet response
 * @return true if the copy is successful
 * @throws WebdavException
 *             if an error in the underlying store occurs
 * @throws IOException
 *             when an error occurs while sending the response
 */
private String copyResource(HttpServletRequest req, HttpServletResponse resp)
        throws WebdavException, IOException {

    // Parsing destination header

    String destinationPath = req.getHeader("Destination");

    if (destinationPath == null) {
        resp.sendError(WebdavStatus.SC_BAD_REQUEST);
        return null;
    }

    // Remove url encoding from destination
    destinationPath = RequestUtil.URLDecode(destinationPath, "UTF8");

    int protocolIndex = destinationPath.indexOf("://");
    if (protocolIndex >= 0) {
        // if the Destination URL contains the protocol, we can safely
        // trim everything upto the first "/" character after "://"
        int firstSeparator = destinationPath.indexOf("/", protocolIndex + 4);
        if (firstSeparator < 0) {
            destinationPath = "/";
        } else {
            destinationPath = destinationPath.substring(firstSeparator);
        }
    } else {
        String hostName = req.getServerName();
        if ((hostName != null) && (destinationPath.startsWith(hostName))) {
            destinationPath = destinationPath.substring(hostName.length());
        }

        int portIndex = destinationPath.indexOf(":");
        if (portIndex >= 0) {
            destinationPath = destinationPath.substring(portIndex);
        }

        if (destinationPath.startsWith(":")) {
            int firstSeparator = destinationPath.indexOf("/");
            if (firstSeparator < 0) {
                destinationPath = "/";
            } else {
                destinationPath = destinationPath.substring(firstSeparator);
            }
        }
    }

    // Normalise destination path (remove '.' and '..')
    destinationPath = normalize(destinationPath);

    String contextPath = req.getContextPath();
    if ((contextPath != null) && (destinationPath.startsWith(contextPath))) {
        destinationPath = destinationPath.substring(contextPath.length());
    }

    String path = getRelativePath(req);

    // if source = destination
    if (path.equals(destinationPath)) {
        log.error("WebdavServer cannot copy if source and destination is the same");
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
    }

    // Parsing overwrite header

    boolean overwrite = true;
    String overwriteHeader = req.getHeader("Overwrite");

    if (overwriteHeader != null) {
        overwrite = overwriteHeader.equalsIgnoreCase("T");
    }

    // Overwriting the destination
    String lockOwner = "copyResource" + System.nanoTime() + req.toString();
    if (fResLocks.lock(destinationPath, lockOwner, true, -1)) {
        try {

            // Retrieve the resources
            if (!fStore.objectExists(path)) {
                log.error("Recource to be copied does not exist " + path);
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return null;
            }

            Hashtable errorList = new Hashtable();

            copy(path, destinationPath, overwrite, errorList, req, resp);
            if (!errorList.isEmpty()) {
                sendReport(req, resp, errorList);
            }

        } finally {
            fResLocks.unlock(destinationPath, lockOwner);
        }
    } else {
        log.error("WebdavServer unable to lock resource " + lockOwner);
        resp.sendError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
        return null;
    }
    return destinationPath;

}

From source file:smartrics.rest.test.fitnesse.fixture.ResourcesServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    LOG.debug("Resource GET REQUEST ========= " + req.toString());
    String uri = sanitise(req.getRequestURI());
    String id = getId(uri);/*www.ja  va 2s  .c  om*/
    String type = getType(uri);
    String extension = getExtension(uri);
    echoHeader(req, resp);
    echoQString(req, resp);
    try {
        if (id == null) {
            list(resp, type, extension);
            headers(resp, extension, DEF_CHARSET);
        } else if (resources.get(type, id) == null) {
            notFound(resp);
        } else {
            if (resources.get(type, id).isDeleted()) {
                notFound(resp);
            } else {
                found(resp, type, id);
                headers(resp, extension, DEF_CHARSET);
            }
        }
    } catch (RuntimeException e) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
    } finally {
        LOG.debug("Resource GET RESPONSE ========= " + resp.toString());
    }
}

From source file:smartrics.rest.test.fitnesse.fixture.ResourcesServlet.java

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    LOG.debug("Resource DELETE REQUEST ========= " + req.toString());
    String uri = sanitise(req.getRequestURI());
    String type = getType(uri);//  w w  w.ja va 2 s  .c o  m
    echoHeader(req, resp);
    String id = getId(uri);
    Resource resource = resources.get(type, id);
    if (resource != null) {
        // resource.setDeleted(true);
        resources.remove(type, id);
        resp.getOutputStream().write("".getBytes());
        resp.setStatus(HttpServletResponse.SC_OK);
    } else {
        notFound(resp);
    }
    resp.getOutputStream().write("".getBytes());
    resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
    LOG.debug("Resource DELETE RESPONSE ========= " + req.toString());
}

From source file:smartrics.rest.test.fitnesse.fixture.ResourcesServlet.java

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    LOG.debug("Resource PUT REQUEST ========= " + req.toString());
    echoHeader(req, resp);//from  w ww.  j a v  a2 s .c  om
    String uri = sanitise(req.getRequestURI());
    String id = getId(uri);
    String type = getType(uri);
    String content = getContent(req.getInputStream());
    Resource resource = resources.get(type, id);
    if (resource != null) {
        resource.setPayload(content);
        resp.getOutputStream().write("".getBytes());
        resp.setStatus(HttpServletResponse.SC_OK);
    } else {
        notFound(resp);
    }
    LOG.debug("Resource PUT RESPONSE ========= " + req.toString());
}

From source file:smartrics.rest.test.fitnesse.fixture.ResourcesServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    LOG.debug("Resource POST REQUEST ========= " + req.toString());
    echoHeader(req, resp);/*from   w  ww  .  ja v  a2 s.c om*/
    String uri = sanitise(req.getRequestURI());
    String type = getType(uri);
    try {
        String contentType = req.getContentType();
        if (contentType.equals("application/octet-stream")) {
            LOG.debug("Resource POST REQUEST is a file upload");
            processFileUpload(req, resp);
        } else if (contentType.startsWith("multipart")) {
            LOG.debug("Resource POST REQUEST is a multipart file upload");
            processMultiPart(req, resp);
        } else {
            String content = getContent(req.getInputStream());
            if (contentType.contains("application/x-www-form-urlencoded")) {
                try {
                    generateResponse(resp, type, noddyKvpToXml(content, "UTF-8"));
                } catch (Exception e) {
                    LOG.warn("the content passed in isn't encoded as application/x-www-form-urlencoded: "
                            + content);
                    resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
                }
            } else if (content.trim().startsWith("<") || content.trim().endsWith("}")) {
                generateResponse(resp, type, content);
            } else {
                resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
            }
        }
    } catch (RuntimeException e) {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
    LOG.debug("Resource POST RESPONSE ========= " + req.toString());
}

From source file:tv.dyndns.kishibe.qmaclone.server.IconUploadServletStub.java

private void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    logger.log(Level.INFO, request.toString());

    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(64L * 1024L);//from   w ww . j a  va2s .c o  m
    List<FileItem> items = null;

    try {
        List<FileItem> temp = upload.parseRequest(request);
        items = temp;
    } catch (FileUploadException e) {
        writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_FAILED_TO_PARSE_REQUEST);
        return;
    }

    String userCode = null;
    BufferedImage inputImage = null;
    long originalFileSize = 0;

    for (FileItem item : items) {
        if (item.isFormField()) {
            String key = item.getFieldName();
            String value = item.getString();

            if (key.equals(Constant.FORM_NAME_USER_CODE)) {
                userCode = value;
            }

            continue;
        }

        // ??
        originalFileSize = item.getSize();
        try (InputStream inputStream = item.getInputStream()) {
            inputImage = ImageIO.read(inputStream);
        } catch (IOException e) {
            writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_FAILED_TO_DETECT_IMAGE_FILE_TYPE);
            return;
        }

        if (inputImage == null) {
            writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_FAILED_TO_DETECT_IMAGE_FILE_TYPE);
            return;
        }
    }

    if (userCode == null || inputImage == null) {
        writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_REQUEST_FORMAT_ERROR);
        return;
    }

    String extension = "jpg";
    String fileTitle = Utility.createFileName();
    String fileName = fileTitle + "." + extension;

    // 
    // TODO(nodchip): ?
    int size = Constant.ICON_SIZE * 2;
    ImageFilter imageFilter = new AreaAveragingScaleFilter(size, size);
    Image middleImage = new Canvas().createImage(new FilteredImageSource(inputImage.getSource(), imageFilter));
    BufferedImage outputImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    outputImage.createGraphics().drawImage(middleImage, 0, 0, size, size, null);

    // ???
    new File(Constant.ICON_FOLDER_PATH).mkdirs();
    File outputFile = new File(Constant.ICON_FOLDER_PATH, fileName);
    ImageIO.write(outputImage, "jpg", outputFile);

    logger.log(Level.INFO, String.format("%d bytes -> %d bytes (%s)", originalFileSize, outputFile.length(),
            outputFile.getPath()));

    // ???
    int userId = Integer.parseInt(userCode);
    PacketUserData userData;
    try {
        userData = database.getUserData(userId);
    } catch (DatabaseException e) {
        logger.log(Level.WARNING, "????????", e);
        throw new ServletException(e);
    }
    File oldFile = new File(Constant.ICON_FOLDER_PATH, userData.imageFileName);
    if (oldFile.isFile() && !oldFile.getName().equals(Constant.ICON_NO_IMAGE)) {
        oldFile.delete();
    }
    userData.imageFileName = fileName;
    try {
        database.setUserData(userData);
    } catch (DatabaseException e) {
        logger.log(Level.WARNING, "???????", e);
        throw new ServletException(e);
    }

    // ????
    writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_OK);
}