Example usage for javax.servlet.http HttpServletResponse SC_FORBIDDEN

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

Introduction

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

Prototype

int SC_FORBIDDEN

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

Click Source Link

Document

Status code (403) indicating the server understood the request but refused to fulfill it.

Usage

From source file:org.jasig.portlet.announcements.controller.RssFeedController.java

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

    response.setContentType(CONTENT_TYPE);

    Long topicId;/*from  w  ww . jav  a  2  s. com*/
    try {
        topicId = Long.valueOf(ServletRequestUtils.getIntParameter(request, "topic"));
        if (topicId == null) {
            throw new IllegalStateException("Must specify the topic id");
        }
    } catch (Exception e) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Must specify topic id");
        return null;
    }

    Topic t = null;
    try {
        t = announcementService.getTopic(topicId);
    } catch (PortletException e) {
        e.printStackTrace();
    }

    if (t == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "no such topic");
        return null;
    }

    if (!t.isAllowRss()) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN, "This topic is not available as RSS");
        return null;
    }

    // fetch and sort the announcements
    List<Announcement> announcements = new ArrayList<Announcement>();
    announcements.addAll(t.getPublishedAnnouncements());

    Collections.sort(announcements);

    // create the feed
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType("rss_2.0");
    feed.setTitle(t.getTitle());
    feed.setLink(request.getRequestURL().append("?topic=").append(topicId.toString()).toString());
    feed.setDescription(t.getDescription());

    List<SyndEntry> entries = new ArrayList<SyndEntry>();
    SyndEntry entry;
    SyndContent description;

    for (Announcement a : announcements) {
        entry = new SyndEntryImpl();
        entry.setTitle(a.getTitle());
        entry.setAuthor(a.getAuthor());
        if (a.getLink() != null)
            entry.setLink(a.getLink());
        entry.setPublishedDate(a.getStartDisplay());
        description = new SyndContentImpl();
        description.setType("text/plain");
        description.setValue(a.getMessage());
        entry.setDescription(description);
        entries.add(entry);
    }

    feed.setEntries(entries);

    SyndFeedOutput output = new SyndFeedOutput();
    String out;
    try {
        out = output.outputString(feed);
    } catch (FeedException e) {
        e.printStackTrace();
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error generating feed");
        return null;
    }

    response.setContentLength(out.length());
    response.getOutputStream().print(out);
    response.getOutputStream().flush();

    return null;

}

From source file:biz.taoconsulting.dominodav.methods.MOVE.java

/**
 * (non-Javadoc)// www .  j  a  va 2 s .co  m
 * 
 * @see biz.taoconsulting.dominodav.methods.AbstractDAVMethod#action()
 */
protected void action() {
    // check if locked - locked files / foldes should not be moved... (???)
    // TODO fix the path mess

    IDAVRepository rep = this.getRepository();
    // Resource-Path is stripped by the repository name!
    // String curPath = (String)
    // this.getHeaderValues().get("resource-path");
    // uri is the unique identifier on the host includes servlet and
    // repository but not server
    String curURI = (String) this.getHeaderValues().get("uri");
    LockManager lm = this.getLockManager();
    IDAVResource resource;

    String des = (String) this.getReq().getHeader("Destination");
    // des =
    // des.replaceAll(this.getReq().getRequestURL().toString().replaceAll(curPath,
    // ""), "");
    try {
        curURI = java.net.URLDecoder.decode(curURI, "UTF-8");
        des = java.net.URLDecoder.decode(des, "UTF-8");

    } catch (Exception e) {
    }
    LOGGER.info("DESTINATION ADDRESS=" + des);
    try {
        resource = rep.getResource(curURI);
        if (lm.isLocked(resource)) {
            this.setHTTPStatus(423);
            return;
        } else {
            if (resource.isReadOnly()) {
                this.setHTTPStatus(HttpServletResponse.SC_FORBIDDEN);
            } else {
                this.setHTTPStatus(rep.moveResource(curURI, des));
                return;
            }
        }
    } catch (DAVNotFoundException e) {
        this.setErrorMessage("Resource not found" + curURI, 404);
        return;
    }
}

From source file:io.wcm.caconfig.editor.impl.ConfigDataServlet.java

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response)
        throws ServletException, IOException {
    if (!editorConfig.isEnabled()) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;/*from  w w w.  ja va 2 s.co m*/
    }

    // get parameters
    String configName = request.getParameter(RP_CONFIGNAME);
    if (StringUtils.isBlank(configName)) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    boolean collection = BooleanUtils.toBoolean(request.getParameter(RP_COLLECTION));

    // output configuration
    try {
        JSONObject result = getConfiguration(request.getResource(), configName, collection);
        if (result == null) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } else {
            response.setContentType("application/json;charset=" + CharEncoding.UTF_8);
            response.getWriter().write(result.toString());
        }
    }
    /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/
        log.error("Error getting configuration for " + configName + (collection ? "[col]" : ""), ex);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
    }
}

From source file:elw.web.StudentController.java

protected HashMap<String, Object> auth(final HttpServletRequest req, final HttpServletResponse resp,
        final boolean page, final boolean verified) throws IOException {
    final HashMap<String, Object> model = super.auth(req, resp, page, verified);

    if (model == null) {
        return null;
    }/*from   ww  w  .j  a v  a2s . co  m*/

    final Auth auth = auth(model);
    final Group group = auth.getGroup();
    final Student student = auth.getStudent();
    final Admin admin = auth.getAdmin();

    Ctx ctx = Ctx.fromString(req.getParameter(R_CTX));
    ctx.resolve(queries);
    if (ctx.getGroup() == null) {
        ctx = ctx.extendGroup(group);
    }
    if (ctx.getStudent() == null) {
        ctx = ctx.extendStudent(student);
    }
    if (!ctx.resolved(Ctx.STATE_GS)) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "context path problem, please check the logs");
        return null;
    }

    if (admin == null) {
        if (!ctx.getGroup().getId().equals(group.getId())) {
            resp.sendError(HttpServletResponse.SC_FORBIDDEN, "context path refers to another group");
            return null;
        }
        if (!ctx.getStudent().getId().equals(student.getId())) {
            resp.sendError(HttpServletResponse.SC_FORBIDDEN, "context path refers to another student");
            return null;
        }
    }

    model.put(R_CTX, ctx);
    //  LATER nobody knows what's this and whether it's needed at all
    model.put("expandTriggers", req.getSession().getAttribute("viewToExpandTriggers"));

    return model;

}

From source file:eu.trentorise.smartcampus.unidataservice.controller.rest.StudentInfoController.java

@RequestMapping(method = RequestMethod.GET, value = "/getstudentdata")
public @ResponseBody StudentInfoData getStudentInfo(HttpServletRequest request, HttpServletResponse response)
        throws InvocationException {
    try {//from  w ww  .  ja  v a 2  s  .c  o m
        User user = getCurrentUser();
        String userId = getUserId(user);
        if (userId == null) {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            return null;
        }

        String token = getToken(request);
        String idAda = getIdAda(token);
        StudentInfoData sd = getStudentInfo(idAda);
        if (sd != null) {
            return sd;
        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }

        return null;
    } catch (Exception e) {
        e.printStackTrace();
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
    return null;
}

From source file:com.skilrock.lms.embedded.roleMgmt.common.PrivsInterceptor.java

/**
 * Handles a rejection by sending a 403 HTTP error
 * //from  w w  w  .  j a  va  2s  .  co  m
 * @param invocation
 *            The invocation
 * @return The result code
 * @throws Exception
 */
protected String handleRejection(ActionInvocation invocation, HttpServletResponse response) throws Exception {
    response.sendError(HttpServletResponse.SC_FORBIDDEN);
    return null;
}

From source file:com.idega.servlet.filter.WebServiceAuthorizationFilter.java

@Override
public void doFilter(ServletRequest myRequest, ServletResponse myResponse, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) myRequest;
    HttpServletResponse response = (HttpServletResponse) myResponse;

    ServletContext myServletContext = request.getSession().getServletContext();
    // getting the application context
    IWMainApplication mainApplication = IWMainApplication.getIWMainApplication(myServletContext);
    boolean doCheck = mainApplication.getIWApplicationContext().getApplicationSettings()
            .getBoolean(this.DO_BASIC_AUTHENTICATION, true);

    if (doCheck) {
        if (!requestIsValid(request)) {
            // send a 403 error
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }// w  ww  .j a  v a 2s  .c  om
    } else {
        boolean isValid = false;
        try {
            String validIP = mainApplication.getIWApplicationContext().getApplicationSettings()
                    .getProperty(this.VALID_IP, "");
            if (WS_ALLOW_ALL_IPS.equals(validIP)) {
                isValid = true;
            } else {
                String[] ips = validIP.split("\\;");
                for (int i = 0; i < ips.length; i++) {
                    if (ips[i].equals(request.getRemoteAddr())) {
                        isValid = true;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            isValid = false;
        }

        if (!isValid) {
            // send a 403 error
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
    }

    chain.doFilter(request, response);
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.datatools.dumprestore.DumpRestoreController.java

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    if (!PolicyHelper.isAuthorizedForActions(req, REQUIRED_ACTION)) {
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
    }/* w w w. j a v  a  2  s.c  o  m*/

    try {
        if (ACTION_RESTORE.equals(req.getPathInfo())) {
            long tripleCount = new RestoreModelsAction(req, resp).restoreModels();
            req.setAttribute(ATTRIBUTE_TRIPLE_COUNT, tripleCount);
            super.doGet(req, resp);
        } else {
            resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        }
    } catch (BadRequestException | RDFServiceException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestControllerTest.java

/**
 * @verifies return forbidden if logged in
 * @see BaseRestController#apiAuthenticationExceptionHandler(Exception,
 *      javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 *///from   w  ww .  j  a v a2 s . c o  m
@Test
public void apiAuthenticationExceptionHandler_shouldReturnForbiddenIfLoggedIn() throws Exception {
    controller.apiAuthenticationExceptionHandler(new APIAuthenticationException(), request, response);

    assertThat(response.getStatus(), is(HttpServletResponse.SC_FORBIDDEN));
}

From source file:com.stratelia.webactiv.servlets.RestOnlineFileServer.java

@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    RestRequest restRequest = new RestRequest(req, "");
    SilverTrace.info("peasUtil", "RestOnlineFileServer.doPost", "root.MSG_GEN_ENTER_METHOD");
    try {//from ww  w  .  j  a va 2 s  .c  o m
        OnlineFile file = getWantedFile(restRequest);
        if (file != null) {
            display(res, file);
            return;
        }
    } catch (IllegalAccessException ex) {
        res.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return;
    } catch (Exception ex) {
        throw new ServletException(ex);
    }
    displayWarningHtmlCode(res);
}