Example usage for javax.servlet ServletException ServletException

List of usage examples for javax.servlet ServletException ServletException

Introduction

In this page you can find the example usage for javax.servlet ServletException ServletException.

Prototype


public ServletException(String message, Throwable rootCause) 

Source Link

Document

Constructs a new servlet exception when the servlet needs to throw an exception and include a message about the "root cause" exception that interfered with its normal operation, including a description message.

Usage

From source file:be.fedict.eid.applet.service.KmlServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    LOG.debug("doGet");

    HttpSession httpSession = request.getSession();
    EIdData eIdData = (EIdData) httpSession.getAttribute("eid");

    byte[] document;
    try {/*from   w w w  . j a  va 2s  . c  o  m*/
        document = this.kmlGenerator.generateKml(eIdData);
    } catch (IOException e) {
        throw new ServletException("KML generator error: " + e.getMessage(), e);
    }

    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=-1"); // http 1.1
    if (false == request.getScheme().equals("https")) {
        // else the download fails in IE
        response.setHeader("Pragma", "no-cache"); // http 1.0
    } else {
        response.setHeader("Pragma", "public");
    }
    response.setDateHeader("Expires", -1);
    response.setHeader("Content-disposition", "attachment");
    response.setContentLength(document.length);

    response.setContentType(KmlLight.MIME_TYPE);
    response.setContentLength(document.length);
    ServletOutputStream out = response.getOutputStream();
    out.write(document);
    out.flush();
}

From source file:org.bonitasoft.web.designer.controller.WidgetDirectiveLoaderController.java

@RequestMapping("runtime/widgets/**")
public void serveWidgetDirective(HttpServletRequest request, HttpServletResponse response)
        throws ServletException {
    Path filePath = extractPathWithinPattern(request);

    try {//  w  ww .  j a  va 2s.co  m
        HttpFile.writeFileInResponse(request, response, filePath);
    } catch (IOException e) {
        logger.error("Error on widget generation", e);
        throw new ServletException("Error on widget generation", e);
    }
}

From source file:gov.nih.nci.cabio.portal.portlet.RESTProxyServletController.java

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    try {/*from   w  w  w.j a  va  2  s.  c  o  m*/
        String relativeURL = request.getParameter("relativeURL");
        if (relativeURL == null) {
            log.error("Null relativeURL parameter");
            return null;
        }

        String qs = request.getQueryString();
        if (qs == null) {
            qs = "";
        }

        qs = qs.replaceFirst("relativeURL=(.*?)&", "");
        qs = URLDecoder.decode(qs, "UTF-8");

        String url = caBIORestURL + relativeURL + "?" + qs;
        log.info("Proxying URL: " + url);

        URI uri = new URI(url, false);
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod();
        method.setURI(uri);
        client.executeMethod(method);

        response.setContentType(method.getResponseHeader("Content-Type").getValue());

        CopyUtils.copy(method.getResponseBodyAsStream(), response.getOutputStream());
    } catch (Exception e) {
        throw new ServletException("Unable to connect to caBIO", e);
    }

    return null;
}

From source file:org.craftercms.engine.servlet.filter.ExceptionHandlingFilter.java

protected void handleException(HttpServletRequest request, HttpServletResponse response, Exception ex)
        throws ServletException, IOException {
    boolean handled = false;

    for (Iterator<ExceptionHandler> iter = exceptionHandlers.iterator(); iter.hasNext() && !handled;) {
        handled = iter.next().handle(request, response, ex);
    }// w ww.j  av  a 2s.  c  o m

    if (!handled) {
        throw new ServletException("Unhandled exception: " + ex.getMessage(), ex);
    }
}

From source file:com.github.woonsan.katharsis.servlet.AbstractKatharsisServlet.java

/**
 * {@inheritDoc}/*from w ww  .  j  ava 2 s.  c om*/
 */
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    KatharsisInvokerContext invokerContext = createKatharsisInvokerContext(request, response);

    try {
        getKatharsisInvoker().invoke(invokerContext);
    } catch (KatharsisInvokerException e) {
        log.warn("Katharsis Invoker exception.", e);
        response.setStatus(e.getStatusCode());
    } catch (Exception e) {
        throw new ServletException("Katharsis invocation failed.", e);
    }
}

From source file:io.wcm.dam.assetservice.impl.DataVersionServlet.java

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {
    String path = request.getResource().getPath();

    // check path is a valid DAM root folder path for asset service
    if (!damPathHandler.isAllowedDataVersionPath(path)) {
        log.debug("Path not allowed to get data version {}", path);
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;/*from  w  ww.j  ava2  s.  c  om*/
    }

    // return data version as JSON
    try {
        JSONObject jsonResponse = new JSONObject();
        jsonResponse.put("dataVersion", damPathHandler.getDataVersion());

        response.setContentType(ContentType.JSON);
        response.setCharacterEncoding(CharEncoding.UTF_8);
        response.getWriter().write(jsonResponse.toString());
    } catch (JSONException ex) {
        throw new ServletException("Unable to generate JSON.", ex);
    }
}

From source file:com.example.managedvms.twilio.SendSmsServlet.java

@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    final String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID");
    final String twilioAuthToken = System.getenv("TWILIO_AUTH_TOKEN");
    final String twilioNumber = System.getenv("TWILIO_NUMBER");
    final String toNumber = (String) req.getParameter("to");
    if (toNumber == null) {
        resp.getWriter().print("Please provide the number to message in the \"to\" query string parameter.");
        return;//w  w w.  ja  v a 2s  . c om
    }
    TwilioRestClient client = new TwilioRestClient(twilioAccountSid, twilioAuthToken);
    Account account = client.getAccount();
    MessageFactory messageFactory = account.getMessageFactory();
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("To", toNumber));
    params.add(new BasicNameValuePair("From", twilioNumber));
    params.add(new BasicNameValuePair("Body", "Hello from Twilio!"));
    try {
        Message sms = messageFactory.create(params);
        resp.getWriter().print(sms.getBody());
    } catch (TwilioRestException e) {
        throw new ServletException("Twilio error", e);
    }
}

From source file:com.alliander.osgp.webdevicesimulator.application.config.WebDeviceSimulatorInitializer.java

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    try {/*  w  w w .  jav  a 2 s. co m*/
        // Force the timezone of application to UTC (required for
        // Hibernate/JDBC)
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        final Context initialContext = new InitialContext();

        final String logLocation = (String) initialContext
                .lookup("java:comp/env/osp/webDeviceSimulator/log-config");
        LogbackConfigurer.initLogging(logLocation);
        InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());

        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationContext.class);

        final ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME,
                new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping(DISPATCHER_SERVLET_MAPPING);

        servletContext.addListener(new ContextLoaderListener(rootContext));
    } catch (final NamingException e) {
        throw new ServletException("naming exception", e);
    } catch (final FileNotFoundException e) {
        throw new ServletException("Logging file not found", e);
    } catch (final JoranException e) {
        throw new ServletException("Logback exception", e);
    }
}

From source file:jsst.server.FrontController.java

private void initSystemProperties() throws ServletException {
    URL resource = FrontController.class.getClassLoader().getResource(ENVIRONMENT_PROPERTIES_FILE);
    if (resource != null) {
        Properties properties = new Properties();
        try {/* w  w w. j  a v a2  s  .c o  m*/
            InputStream inputStream = resource.openStream();
            try {
                properties.load(inputStream);
                Set<Map.Entry<Object, Object>> entries = properties.entrySet();
                for (Map.Entry<Object, Object> entry : entries) {
                    System.setProperty((String) entry.getKey(), (String) entry.getValue());
                }
            } finally {
                inputStream.close();
            }
        } catch (IOException e) {
            throw new ServletException(
                    "Unable to load system properties from " + ENVIRONMENT_PROPERTIES_FILE + " file", e);
        }
    }
}

From source file:gov.nih.nci.ncicb.cadsr.umlmodelbrowser.struts.actions.XmlDownloadAction.java

public ActionForward xmlDownload(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws IOException, ServletException {
    try {//from   ww  w.  ja v a 2  s  .co  m
        Collection<UMLAttributeMetadata> attributes = (Collection<UMLAttributeMetadata>) getSessionObject(
                request, UMLBrowserFormConstants.CLASS_ATTRIBUTES);
        CaDSRQueryService caDSRQueryService = getAppServiceLocator().findCaDSRQueryService();
        String xmlString = caDSRQueryService.getXMLForAttributes(attributes);
        response.setContentType("application/octet-stream");
        String fileName = "DataElementDownload_" + sdf.format(new Date()) + ".xml";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.getWriter().write(xmlString);
        return null;
    } catch (Exception e) {
        log.error("Error getting data element xml for attributes.", e);
        throw new ServletException("Error getting data element xml for attributes.", e);
    }
}