org.eclipse.swordfish.registry.WSDLServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.swordfish.registry.WSDLServlet.java

Source

/*******************************************************************************
 * Copyright (c) 2008, 2009 SOPERA GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     SOPERA GmbH - initial API and implementation
 *******************************************************************************/

package org.eclipse.swordfish.registry;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class WSDLServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = -8376659320998034145L;

    private static final Log LOGGER = LogFactory.getLog(WSDLServlet.class);

    //   private WSDLResourceReader wsdlReader;

    //   private WSDLRepository repository;

    private Router router;

    public WSDLServlet() {
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        LOGGER.info("Received GET request:\n" + req.getRequestURL());
        Resource resource = null;

        try {
            resource = router.getResource(req);
        } catch (IncorrectRequestException e) {
            resp.sendError(e.getErrorCode(), e.getErrorMessage());
            return;
        }

        try {
            resp.setContentType(resource.getContentType());
            resp.setCharacterEncoding(resource.getCharacterEncoding());
            resource.get(resp.getWriter());
        } catch (VerbNotSupportedException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "GET is not supported for " + req.getRequestURL());
            return;
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        LOGGER.info("Received PUT request:\n" + req.getRequestURL());
        Resource resource = null;

        try {
            resource = router.getResource(req);
        } catch (IncorrectRequestException e) {
            resp.sendError(e.getErrorCode(), e.getErrorMessage());
            return;
        }

        try {
            resource.put(req.getReader());
        } catch (InvalidFormatException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Body does not contain a valid WSDL.");
            return;
        } catch (VerbNotSupportedException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT is not supported for " + req.getRequestURL());
        }
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        LOGGER.info("Received DELETE request:\n" + req.getRequestURL());
        Resource resource = null;

        try {
            resource = router.getResource(req);
        } catch (IncorrectRequestException e) {
            resp.sendError(e.getErrorCode(), e.getErrorMessage());
            return;
        }
        try {
            resource.delete();
        } catch (VerbNotSupportedException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "DELETE is not supported for " + req.getRequestURL());
            return;
        }
    }

    public void setRouter(Router router) {
        this.router = router;
    }

    /*
       public void setWsdlReader(WSDLResourceReader wsdlReader) {
    //      this.wsdlReader = wsdlReader;
       }
        
       public void setRepository(WSDLRepository repository) {
    //      this.repository = repository;
       }
    */
}