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, String msg) throws IOException;

Source Link

Document

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

Usage

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

@RequestMapping(value = DATA_PATH, method = RequestMethod.GET)
public void getData(@PathVariable("id") long id, HttpServletResponse response) throws IOException {
    if (videos.containsKey(id)) {
        Video v = videos.get(id);/* w w  w  .j  av  a  2s.  com*/
        manager.copyVideoData(v, response.getOutputStream());
    } else {
        response.sendError(404, "No video with id=" + id + " exists.");
    }
}

From source file:com.appranix.adapter.controller.AdapterMonitoring.java

@RequestMapping("/adapter")
public void TestAdapter(HttpServletResponse response) throws IOException {

    try {//w ww .j a v a  2  s.  c o  m

        int id = adapterServie.adapterCreate();

        adapterServie.adapterRead(id);

        adapterServie.adapterDelete(id);

        System.out.println(id);

    } catch (Exception e) {
        response.sendError(400, e.getMessage());
    }
}

From source file:de.steilerdev.myVerein.server.security.rest.RestAuthenticationFailureHandler.java

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {
    logger.warn("[{}] Authentication failed: {}", SecurityHelper.getClientIpAddr(request),
            exception.getMessage());/*from  w  w w .  j a  va2s. co m*/
    if (!response.isCommitted()) {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
                "Authentication failed: " + exception.getMessage());
    }
}

From source file:jetbrains.buildServer.staticUIExtensions.web.StaticContentController.java

@Override
protected ModelAndView doHandle(@NotNull final HttpServletRequest request,
        @NotNull final HttpServletResponse response) throws Exception {

    final String token = request.getParameter(myPaths.getTokenParameter());
    if (!myConfig.getAccessToken().equals(token)) {
        response.sendError(HttpStatus.SC_NOT_FOUND, "Path not found. Invalid access token");
        return null;
    }//from ww  w . j av  a  2  s .  c  om

    if (request.getParameter(myPaths.getEmptyContentParameter()) != null) {
        return null;
    }

    //TODO: allow directories, may use Util from TeamCity for that
    final String file = request.getParameter(myPaths.getIncludeFileParameter());
    if (StringUtil.isEmptyOrSpaces(file) || file.contains("/") || file.contains("\\") || file.contains("..")) {
        final String message = "Failed to open file to include by invalid path: "
                + (file == null ? "no file specified" : file) + ".";
        LOG.warn(message);
        return sendError(request, response, message);
    }

    final File includeFile = myConfig.mapIncludeFilePath(file);
    if (includeFile == null || !includeFile.isFile()) {
        LOG.warn("Failed to open file to include: " + (includeFile != null ? includeFile : file) + ".");
        return sendError(request, response, "Path not found: " + file);
    }

    final char[] data;
    try {
        data = myCache.getContent(includeFile);
    } catch (IOException e) {
        LOG.warn("Failed to open file to include: " + includeFile + ". " + e.getMessage(), e);
        return sendError(request, response, "Failed to open file: " + includeFile.getName());
    }

    response.getWriter().write(data);
    return null;
}

From source file:com.tce.oauth2.spring.client.filters.SessionDataFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;

    // Todo Controller is not authorized for anonymous user
    if (req.getRequestURI().indexOf("/todos") != -1) {
        if (req.getSession().getAttribute("username") == null) {
            resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
        } else {/*from ww  w  .j  a  va  2  s .  co m*/
            filterChain.doFilter(request, response);
        }
    } else {
        filterChain.doFilter(request, response);
    }
}

From source file:ai.susi.server.api.aaa.AccessServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Query post = RemoteAccess.evaluate(request);
    if (post.isDoS_servicereduction() || post.isDoS_blackout()) {
        response.sendError(503, "your request frequency is too high");
        return;/*from  ww w  .  ja  v  a  2 s.  c  o m*/
    } // DoS protection

    boolean anonymize = !post.isLocalhostAccess();

    String callback = post.get("callback", "");
    boolean jsonp = callback != null && callback.length() > 0;

    post.setResponse(response, "application/javascript");
    Collection<Track> tracks = DAO.access.getTracks();

    // generate json
    JSONObject json = new JSONObject(true);
    JSONArray access = new JSONArray();
    json.put("access", access);
    int maxcount = anonymize ? 100 : 1000;
    for (Track track : tracks) {
        if (anonymize && !track.get("class").equals("SearchServlet"))
            continue;
        JSONObject a = new JSONObject(true);
        for (String key : track.keySet()) {
            Object value = track.get(key);
            if (anonymize && "host".equals(key)) {
                a.put("host-anonymized", Integer.toHexString(Math.abs(value.hashCode())));
            } else {
                a.put(key, value);
            }
        }
        access.put(a);
        if (maxcount-- <= 0)
            break;
    }

    // write json
    PrintWriter sos = response.getWriter();
    if (jsonp)
        sos.print(callback + "(");
    sos.print(json.toString(2));
    if (jsonp)
        sos.println(");");
    sos.println();
    post.finalize();
}

From source file:com.vmware.identity.samlservice.impl.SamlServiceImpl.java

/**
 * @param locale/* www  . j  av a2s.com*/
 * @param response
 * @param logoutState
 * @throws IOException
 */
static public void sendLogoutError(Locale locale, HttpServletResponse response, LogoutState logoutState,
        MessageSource messageSource) throws IOException {
    // use validation result code to return error to client
    ValidationResult vr = logoutState.getValidationResult();
    String message = null;
    if (messageSource != null && locale != null) {
        message = vr.getMessage(messageSource, locale);
    } else {
        message = vr.getBaseLocaleMessage();
    }
    response.sendError(vr.getResponseCode(), message);
    log.info("Responded with ERROR " + vr.getResponseCode() + ", message " + message);
}

From source file:org.loklak.api.server.AccountServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    RemoteAccess.Post post = RemoteAccess.evaluate(request);
    if (post.isDoS_blackout()) {
        response.sendError(503, "your request frequency is too high");
        return;//from w ww  .j  av a2  s  .  co  m
    } // DoS protection
    if (!post.isLocalhostAccess()) {
        response.sendError(503,
                "access only allowed from localhost, your request comes from " + post.getClientHost());
        return;
    } // danger! do not remove this!
    process(request, response, post);
}

From source file:net.anthonychaves.bookmarks.web.UserAuthorizationInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {

    String uri = request.getRequestURI();
    User user = (User) request.getSession().getAttribute("user");

    if (user != null || uri.indexOf("login") != -1) {
        return true;
    } else {/*from  w w  w  .  jav  a  2 s  .  c o m*/
        response.sendError(HttpServletResponse.SC_FORBIDDEN, UNAUTHORIZED_MSG);
        return false;
    }
}

From source file:org.loklak.api.server.AccountServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    RemoteAccess.Post post = RemoteAccess.evaluate(request);
    if (post.isDoS_blackout()) {
        response.sendError(503, "your request frequency is too high");
        return;//from   ww w  .  j  a  va  2  s . c  om
    } // DoS protection
    if (!post.isLocalhostAccess()) {
        response.sendError(503,
                "access only allowed from localhost, your request comes from " + post.getClientHost());
        return;
    } // danger! do not remove this!
    post.initPOST(RemoteAccess.getPostMap(request));
    process(request, response, post);
}