Example usage for javax.servlet.http HttpServletResponse SC_NOT_IMPLEMENTED

List of usage examples for javax.servlet.http HttpServletResponse SC_NOT_IMPLEMENTED

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_NOT_IMPLEMENTED.

Prototype

int SC_NOT_IMPLEMENTED

To view the source code for javax.servlet.http HttpServletResponse SC_NOT_IMPLEMENTED.

Click Source Link

Document

Status code (501) indicating the HTTP server does not support the functionality needed to fulfill the request.

Usage

From source file:org.wyona.yanel.servlet.YanelServlet.java

/**
 * Generate response from view of resource
 * @param request TODO// ww  w. ja v a  2  s  .  c  o m
 * @param response TODO
 */
private void getContent(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // INFO: Generate "yanel" document in order to collect information in case something should go wrong or some meta information should be requested
    org.w3c.dom.Document doc = null;
    try {
        doc = getDocument(NAMESPACE, "yanel");
    } catch (Exception e) {
        throw new ServletException(e.getMessage(), e);
    }

    Element rootElement = doc.getDocumentElement();

    rootElement.setAttribute("servlet-context-real-path", servletContextRealPath);

    Element requestElement = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "request"));
    requestElement.setAttributeNS(NAMESPACE, "uri", request.getRequestURI());
    requestElement.setAttributeNS(NAMESPACE, "servlet-path", request.getServletPath());

    HttpSession session = request.getSession(true);
    Element sessionElement = (Element) rootElement.appendChild(doc.createElement("session"));
    sessionElement.setAttribute("id", session.getId());
    Enumeration<?> attrNames = session.getAttributeNames();
    if (!attrNames.hasMoreElements()) {
        Element sessionNoAttributesElement = (Element) sessionElement
                .appendChild(doc.createElement("no-attributes"));
    }
    while (attrNames.hasMoreElements()) {
        String name = (String) attrNames.nextElement();
        String value = session.getAttribute(name).toString();
        Element sessionAttributeElement = (Element) sessionElement.appendChild(doc.createElement("attribute"));
        sessionAttributeElement.setAttribute("name", name);
        sessionAttributeElement.appendChild(doc.createTextNode(value));
    }

    String usecase = request.getParameter(YANEL_RESOURCE_USECASE);
    Resource res = null;
    TrackingInformationV1 trackInfo = null;
    long lastModified = -1;
    long size = -1;

    // START first try
    View view = null;
    try {
        Environment environment = getEnvironment(request, response);
        res = getResource(request, response);
        if (res != null) {
            if (isTrackable(res)) {
                //log.debug("Do track: " + res.getPath());
                trackInfo = new TrackingInformationV1();
                ((org.wyona.yanel.core.api.attributes.TrackableV1) res).doTrack(trackInfo);
                //} else {
                //    log.debug("Resource '" + res.getPath() + "' is not trackable.");
            }

            // START introspection generation
            if (usecase != null && usecase.equals("introspection")) {
                sendIntrospectionAsResponse(res, doc, rootElement, request, response);
                return;
            }
            // END introspection generation

            Element resourceElement = getResourceMetaData(res, doc, rootElement);
            Element viewElement = (Element) resourceElement.appendChild(doc.createElement("view"));
            if (ResourceAttributeHelper.hasAttributeImplemented(res, "Viewable", "1")) {
                if (log.isDebugEnabled())
                    log.debug("Resource is viewable V1");
                viewElement.setAttributeNS(NAMESPACE, "version", "1");
                appendViewDescriptors(doc, viewElement, ((ViewableV1) res).getViewDescriptors());

                String viewId = getViewID(request);
                try {
                    view = ((ViewableV1) res).getView(request, viewId);
                } catch (org.wyona.yarep.core.NoSuchNodeException e) {
                    String message = e.getMessage();
                    log.error(message, e);
                    do404(request, response, doc, message);
                    return;
                } catch (Exception e) {
                    String message = e.getMessage();
                    log.error(message, e);
                    Element exceptionElement = (Element) rootElement
                            .appendChild(doc.createElementNS(NAMESPACE, EXCEPTION_TAG_NAME));
                    exceptionElement.appendChild(doc.createTextNode(message));
                    exceptionElement.setAttributeNS(NAMESPACE, "status", "500");
                    response.setStatus(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    setYanelOutput(request, response, doc);
                    return;
                }
            } else if (ResourceAttributeHelper.hasAttributeImplemented(res, "Viewable", "2")) {
                if (log.isDebugEnabled())
                    log.debug("Resource '" + res.getPath() + "' is viewable V2");
                viewElement.setAttributeNS(NAMESPACE, "version", "2");
                appendViewDescriptors(doc, viewElement, ((ViewableV2) res).getViewDescriptors());

                if (!((ViewableV2) res).exists()) {
                    log.warn("ViewableV2 resource '" + res.getPath()
                            + "' does not seem to exist, whereas this resource might not implement exists() properly. Yanel does not generate a 404 response for backwards compatibility reasons, because there are various ViewableV2 resources which do not implement exists() properly. As a workaround one might want to use the exists() method within the getView(String) method and throw a ResourceNotFoundException instead.");
                    //do404(request, response, doc, res.getPath());
                    //return;
                }

                try {
                    size = ((ViewableV2) res).getSize();
                    Element sizeElement = (Element) resourceElement.appendChild(doc.createElement("size"));
                    sizeElement.appendChild(doc.createTextNode(String.valueOf(size)));
                } catch (ResourceNotFoundException e) {
                    log.error(e, e); // INFO: Let's be fault tolerant such that a 404 can be handled more gracefully further down
                }

                String viewId = getViewID(request);
                try {
                    String revisionName = request.getParameter(YANEL_RESOURCE_REVISION);
                    // NOTE: Check also if usecase is not roll-back, because roll-back is also using the yanel.resource.revision
                    if (revisionName != null && !isRollBack(request)) {
                        if (ResourceAttributeHelper.hasAttributeImplemented(res, "Versionable", "2")) {
                            view = ((VersionableV2) res).getView(viewId, revisionName);
                        } else {
                            log.warn("Resource '" + res.getPath()
                                    + "' has not VersionableV2 implemented, hence we cannot generate view for revision: "
                                    + revisionName);
                            view = ((ViewableV2) res).getView(viewId);
                        }
                    } else if (environment.getStateOfView().equals(StateOfView.LIVE)
                            && ResourceAttributeHelper.hasAttributeImplemented(res, "Workflowable", "1")
                            && WorkflowHelper.getWorkflow(res) != null) { // TODO: Instead using the WorkflowHelper the Workflowable interface should have a method to check if the resource actually has a workflow assigned, see http://lists.wyona.org/pipermail/yanel-development/2009-June/003709.html
                        // TODO: Check if resource actually exists (see the exist problem above), because even it doesn't exist, the workflowable interfaces can return something although it doesn't really make sense. For example if a resource type is workflowable, but it has no workflow associated with it, then WorkflowHelper.isLive will nevertheless return true, whereas WorkflowHelper.getLiveView will throw an exception!
                        if (!((ViewableV2) res).exists()) {
                            log.warn("No such ViewableV2 resource: " + res.getPath());
                            log.warn(
                                    "TODO: It seems like many ViewableV2 resources are not implementing exists() properly!");
                            do404(request, response, doc, res.getPath());
                            return;
                        }

                        WorkflowableV1 workflowable = (WorkflowableV1) res;
                        if (workflowable.isLive()) {
                            view = workflowable.getLiveView(viewId);
                        } else {
                            String message = "The viewable (V2) resource '" + res.getPath()
                                    + "' is WorkflowableV1, but has not been published yet.";
                            log.warn(message);
                            // TODO: Make this configurable per resource (or rather workflowable interface) or per realm?!
                            if (displayMostRecentVersion) {
                                // INFO: Because of backwards compatibility the default should display the most recent version
                                log.warn(
                                        "Instead a live/published version, the most recent version will be displayed!");
                                view = ((ViewableV2) res).getView(viewId);
                            } else {
                                log.warn("Instead a live/published version, a 404 will be displayed!");
                                // TODO: Instead a 404 one might want to show a different kind of screen
                                do404(request, response, doc, message);
                                return;
                            }
                        }
                    } else {
                        view = ((ViewableV2) res).getView(viewId);
                    }
                } catch (org.wyona.yarep.core.NoSuchNodeException e) {
                    String message = e.getMessage();
                    log.warn(message, e);
                    do404(request, response, doc, message);
                    return;
                } catch (ResourceNotFoundException e) {
                    String message = e.getMessage();
                    log.warn(message, e);
                    do404(request, response, doc, message);
                    return;
                } catch (Exception e) {
                    log.error(e, e);
                    handleException(request, response, e);
                    return;
                }
            } else { // NO Viewable interface implemented!
                String message = res.getClass().getName() + " is not viewable! (" + res.getPath() + ", "
                        + res.getRealm() + ")";
                log.error(message);
                Element noViewElement = (Element) resourceElement
                        .appendChild(doc.createElement("not-viewable"));
                noViewElement.appendChild(doc.createTextNode(res.getClass().getName() + " is not viewable!"));
                Element exceptionElement = (Element) rootElement
                        .appendChild(doc.createElementNS(NAMESPACE, EXCEPTION_TAG_NAME));
                exceptionElement.appendChild(doc.createTextNode(message));
                exceptionElement.setAttributeNS(NAMESPACE, "status", "501");
                response.setStatus(javax.servlet.http.HttpServletResponse.SC_NOT_IMPLEMENTED);
                setYanelOutput(request, response, doc);
                return;
            }

            if (ResourceAttributeHelper.hasAttributeImplemented(res, "Modifiable", "2")) {
                lastModified = ((ModifiableV2) res).getLastModified();
                Element lastModifiedElement = (Element) resourceElement
                        .appendChild(doc.createElement("last-modified"));
                lastModifiedElement.appendChild(doc.createTextNode(new Date(lastModified).toString()));
            } else {
                Element noLastModifiedElement = (Element) resourceElement
                        .appendChild(doc.createElement("no-last-modified"));
            }

            // INFO: Get the revisions, but only in the meta usecase (because of performance reasons)
            if (request.getParameter(RESOURCE_META_ID_PARAM_NAME) != null) {
                appendRevisionsAndWorkflow(doc, resourceElement, res, request);
            }

            if (ResourceAttributeHelper.hasAttributeImplemented(res, "Translatable", "1")) {
                TranslatableV1 translatable = ((TranslatableV1) res);
                Element translationsElement = (Element) resourceElement
                        .appendChild(doc.createElement("translations"));
                String[] languages = translatable.getLanguages();
                for (int i = 0; i < languages.length; i++) {
                    Element translationElement = (Element) translationsElement
                            .appendChild(doc.createElement("translation"));
                    translationElement.setAttribute("language", languages[i]);
                    String path = translatable.getTranslation(languages[i]).getPath();
                    translationElement.setAttribute("path", path);
                }
            }

            if (usecase != null && usecase.equals("checkout")) {
                if (log.isDebugEnabled())
                    log.debug("Checkout data ...");

                if (ResourceAttributeHelper.hasAttributeImplemented(res, "Versionable", "2")) {
                    // NOTE: The code below will throw an exception if the document is checked out already by another user.
                    String userID = environment.getIdentity().getUsername();
                    VersionableV2 versionable = (VersionableV2) res;
                    if (versionable.isCheckedOut()) {
                        String checkoutUserID = versionable.getCheckoutUserID();
                        if (checkoutUserID.equals(userID)) {
                            log.warn("Resource " + res.getPath() + " is already checked out by this user: "
                                    + checkoutUserID);
                        } else {
                            if (isClientSupportingNeutron(request)) {
                                String eMessage = "Resource '" + res.getPath()
                                        + "' is already checked out by another user: " + checkoutUserID;
                                response.setContentType("application/xml");
                                response.setStatus(
                                        javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                                // TODO: Checkout date and break-lock (optional)
                                response.getWriter().print(XMLExceptionV1.getCheckoutException(eMessage,
                                        res.getPath(), checkoutUserID, null));
                                return;
                            } else {
                                throw new Exception("Resource '" + res.getPath()
                                        + "' is already checked out by another user: " + checkoutUserID);
                            }
                        }
                    } else {
                        versionable.checkout(userID);
                    }
                } else {
                    log.warn("Acquire lock has not been implemented yet ...!");
                    // acquireLock();
                }
            }
        } else {
            Element resourceIsNullElement = (Element) rootElement
                    .appendChild(doc.createElement("resource-is-null"));
        }
    } catch (org.wyona.yarep.core.NoSuchNodeException e) {
        String message = e.getMessage();
        log.warn(message, e);
        do404(request, response, doc, message);
        return;
    } catch (org.wyona.yanel.core.ResourceNotFoundException e) {
        String message = e.getMessage();
        log.warn(message, e);
        do404(request, response, doc, message);
        return;
    } catch (Exception e) {
        log.error(e, e);
        handleException(request, response, e);
        return;
    }
    // END first try

    String meta = request.getParameter(RESOURCE_META_ID_PARAM_NAME);
    if (meta != null) {
        if (meta.length() > 0) {
            if (meta.equals("annotations")) {
                log.debug("Remove everything from the page meta document except the annotations");
                cleanMetaDoc(doc);
                appendAnnotations(doc, res);
                appendTrackingInformation(doc, trackInfo);
            } else {
                log.warn("TODO: Stripping everything from page meta document but, '" + meta
                        + "' not supported!");
            }
        } else {
            log.debug("Show all meta");
            appendAnnotations(doc, res);
            appendTrackingInformation(doc, trackInfo);
        }
        response.setStatus(javax.servlet.http.HttpServletResponse.SC_OK);
        setYanelOutput(request, response, doc);
        return;
    }

    if (view != null) {
        if (generateResponse(view, res, request, response, -1, doc, size, lastModified, trackInfo) != null) {
            //log.debug("Response has been generated successfully :-)");
            return;
        } else {
            log.warn("No response has been generated!");
        }
    } else {
        String message = "View is null!";
        Element exceptionElement = (Element) rootElement
                .appendChild(doc.createElementNS(NAMESPACE, EXCEPTION_TAG_NAME));
        exceptionElement.appendChild(doc.createTextNode(message));
    }

    response.setStatus(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    setYanelOutput(request, response, doc);
    return;
}

From source file:org.wyona.yanel.servlet.YanelServlet.java

/**
 * @see javax.servlet.http.HttpServlet#doDelete(HttpServletRequest, HttpServletResponse);
 *///from w  w  w.jav a  2s  .c  om
@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        Resource res = getResource(request, response);
        if (ResourceAttributeHelper.hasAttributeImplemented(res, "Modifiable", "2")) {
            if (((ModifiableV2) res).delete()) {
                // TODO: Also delete resource config! What about access policies?!
                log.debug("Resource '" + res + "' has been deleted via ModifiableV2 interface.");
                setResourceDeletedResponse(res, response);
                return;
            } else {
                log.warn("Deletable (or rather ModifiableV2) resource '" + res + "' could not be deleted!");
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return;
            }
        } else if (ResourceAttributeHelper.hasAttributeImplemented(res, "Deletable", "1")) {
            // TODO: Finish implementation, set resource input
            ((DeletableV1) res).delete(null);
            log.debug("Resource '" + res + "' has been deleted via DeletableV2 interface.");
            setResourceDeletedResponse(res, response);
            return;
        } else {
            log.error("Resource '" + res + "' has neither interface ModifiableV2 nor DeletableV1 implemented.");
            response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
            return; // QUESTION: According to the spec http://docs.oracle.com/javaee/1.4/api/javax/servlet/http/HttpServlet.html#doDelete%28javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse%29 one should rather throw a ServletException, right?
        }
    } catch (Exception e) {
        throw new ServletException(
                "Could not delete resource with URL <" + request.getRequestURL() + ">: " + e.getMessage(), e);
    }
}

From source file:org.opencms.webdav.CmsWebdavServlet.java

/**
 * Process a PROPPATCH WebDAV request for the specified resource.<p>
 * // w w  w .  j a  v  a2s . c o m
 * Not implemented yet.<p>
 * 
 * @param req the servlet request we are processing
 * @param resp the servlet response we are creating
 */
protected void doProppatch(HttpServletRequest req, HttpServletResponse resp) {

    // Check if Webdav is read only
    if (m_readOnly) {

        resp.setStatus(CmsWebdavStatus.SC_FORBIDDEN);

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_WEBDAV_READ_ONLY_0));
        }

        return;
    }

    // Check if resource is locked
    if (isLocked(req)) {

        resp.setStatus(CmsWebdavStatus.SC_LOCKED);

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_ITEM_LOCKED_1, getRelativePath(req)));
        }

        return;
    }

    resp.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
}