Example usage for javax.servlet.http HttpServletResponse sendError

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

Introduction

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

Prototype

public void sendError(int sc) throws IOException;

Source Link

Document

Sends an error response to the client using the specified status code and clears the buffer.

Usage

From source file:fr.aliasource.webmail.server.DownloadEmlImpl.java

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    IAccount account = (IAccount) req.getSession().getAttribute("account");

    if (account == null) {
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;//from  w ww  .j a  v a2 s .  c  o m
    }

    String emlId = req.getParameter("emlId");
    String folderName = req.getParameter("folderName");

    logger.info("[" + account.getLogin() + "] " + "download eml with id: " + emlId);

    InputStream in = account.downloadEml(folderName, emlId);

    resp.setHeader("Content-Disposition", "application/force-download; filename=\"email.eml\"");
    resp.setHeader("Content-Transfer-Encoding", "binary");
    resp.setHeader("Content-Type", "application/force-download; name=\"email.eml\"");
    OutputStream out = resp.getOutputStream();
    int size = transfer(in, out, true);
    resp.setHeader("Content-Length", "" + size);
}

From source file:org.magnum.dataup.CopyOfMyVideoController.java

@RequestMapping(value = "/video/{id}/data", method = RequestMethod.GET)
@Streaming/*from   w ww. j a  va  2s .  com*/
Response getData(Long id, HttpServletResponse response) throws IOException {
    System.out.println("getData() id - " + id);
    if (id < 1 || id > no_ids) {
        response.sendError(404);
        return null;
    }
    Video v = videos.get((int) (id - 1));
    if (v == null)
        response.sendError(404);

    VideoFileManager vfm = VideoFileManager.get();

    vfm.copyVideoData(v, response.getOutputStream());

    return null;
}

From source file:eu.creatingfuture.propeller.webLoader.servlets.WebRequestServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.addHeader("Access-Control-Allow-Origin", "*");
    String getUrl = req.getParameter("get");
    if (getUrl == null) {
        resp.sendError(400);
        return;/*from w ww .  j  a  v  a2  s. c  om*/
    }
    logger.info(getUrl);
    URL url;
    HttpURLConnection connection = null;
    try {
        url = new URL(getUrl);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setUseCaches(false);

        resp.setContentType(connection.getContentType());
        resp.setContentLengthLong(connection.getContentLengthLong());
        logger.log(Level.INFO, "Content length: {0} response code: {1} content type: {2}", new Object[] {
                connection.getContentLengthLong(), connection.getResponseCode(), connection.getContentType() });
        resp.setStatus(connection.getResponseCode());
        IOUtils.copy(connection.getInputStream(), resp.getOutputStream());
    } catch (IOException ioe) {
        resp.sendError(500, ioe.getMessage());
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.Cuenta.VisRegistro.java

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request/*from   w w w. ja v  a 2 s. c  o  m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //DO NOTHING
    System.out.print("Warning!: acceso por get: " + request);
    response.sendError(401);
}

From source file:com.antonjohansson.managementcenter.core.web.VaadinResourcesServlet.java

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String name = getRequestURI(request);
    Optional<URL> resource = getResource(name);

    if (!resource.isPresent()) {
        response.sendError(SC_NOT_FOUND);
        return;//from   ww w  .  j  a v a  2s . c o m
    }

    try (InputStream input = resource.get().openStream(); OutputStream output = response.getOutputStream()) {
        copy(input, output);
    }
}

From source file:com.rhythm.louie.info.DownloadServlet.java

private void downloadFile(HttpServletRequest request, HttpServletResponse response, String filePath)
        throws IOException {
    if (filePath.isEmpty()) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;/*w  w  w .ja  v  a 2 s . co m*/
    }
    File file = new File(filePath);
    int length = 0;
    try (ServletOutputStream outStream = response.getOutputStream()) {
        ServletContext context = getServletConfig().getServletContext();

        String mimetype = context.getMimeType(filePath);

        // sets response content type
        if (mimetype == null) {
            mimetype = "application/octet-stream";
        }
        response.setContentType(mimetype);
        response.setContentLength((int) file.length());
        String fileName = (new File(filePath)).getName();

        // sets HTTP header
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        byte[] byteBuffer = new byte[4096];
        // reads the file's bytes and writes them to the response stream
        try (DataInputStream in = new DataInputStream(new FileInputStream(file))) {
            // reads the file's bytes and writes them to the response stream
            while ((in != null) && ((length = in.read(byteBuffer)) != -1)) {
                outStream.write(byteBuffer, 0, length);
            }
        }
    }
}

From source file:com.carolinarollergirls.scoreboard.jetty.StreamServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    super.doGet(request, response);

    if (request.getPathInfo().equals("/list"))
        list(request, response);//from www . j  a  v a 2s  .  com
    else
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
}

From source file:com.thinkberg.webdav.GetHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    if (object.exists()) {
        if (FileType.FOLDER.equals(object.getType())) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }//w w w  .  jav  a2s .c om

        setHeader(response, object.getContent());

        InputStream is = object.getContent().getInputStream();
        OutputStream os = response.getOutputStream();
        Util.copyStream(is, os);
        is.close();
    } else {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:uk.org.iay.mdq.server.ResultTextView.java

@Override
public void render(final Map<String, ?> model, final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {

    log.debug("rendering as {}", getContentType());
    final Result result = (Result) model.get("result");

    if (result.isNotFound()) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;//  ww  w . ja  v  a2  s  .c  o  m
    }

    OutputStream out = response.getOutputStream();

    response.setContentType(new MediaType("text", "plain", Charset.forName("UTF-8")).toString());
    final Writer w = new OutputStreamWriter(out, Charset.forName("UTF-8"));

    final Representation norm = result.getRepresentation();
    final byte[] bytes = norm.getBytes();
    w.write("Query result is:\n");
    w.write("   " + bytes.length + " bytes\n");
    w.write("   ETag is " + norm.getETag() + "\n");
    w.write("\n");
    w.write(new String(bytes, Charset.forName("UTF-8")));
}

From source file:com.thinkberg.webdav.MkColHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    BufferedReader bufferedReader = request.getReader();
    String line = bufferedReader.readLine();
    if (line != null) {
        response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return;//ww w  .  j  a  v  a2 s.co m
    }

    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (object.exists()) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }

    if (!object.getParent().exists() || !FileType.FOLDER.equals(object.getParent().getType())) {
        response.sendError(HttpServletResponse.SC_CONFLICT);
        return;
    }

    try {
        object.createFolder();
        response.setStatus(HttpServletResponse.SC_CREATED);
    } catch (FileSystemException e) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }
}