Example usage for javax.servlet.http HttpServletRequest getAttribute

List of usage examples for javax.servlet.http HttpServletRequest getAttribute

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getAttribute.

Prototype

public Object getAttribute(String name);

Source Link

Document

Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

Usage

From source file:eu.supersede.gr.rest.ResourcesRest.java

@RequestMapping("/**")
public byte[] getResource(final HttpServletRequest request) {

    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);

    AntPathMatcher apm = new AntPathMatcher();
    String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path);

    //      System.out.println( "Serving page " + finalPath );

    Resource resource = resourceLoader.getResource("classpath:static/ahprp/" + finalPath);

    //      try {
    //         System.out.println( resource.getFile().getAbsolutePath() );
    //      } catch (IOException e1) {
    //         e1.printStackTrace();
    //      }//  w  w w .  ja  v  a2s .  co m
    //      Resource resource = resourceLoader.getResource("classpath:static/game.html");

    try {
        InputStream is = resource.getInputStream();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            int read = is.read();
            while (read != -1) {
                byteArrayOutputStream.write(read);
                read = is.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = byteArrayOutputStream.toByteArray();
        return bytes;

    } catch (IOException e) {
        e.printStackTrace();
    }

    // Something went wrong
    return ("Failed to load resource '" + finalPath).getBytes();
}

From source file:com.googlecode.psiprobe.controllers.ErrorHandlerController.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    String originalURI = (String) request.getAttribute("javax.servlet.error.request_uri");
    if (originalURI != null && originalURI.endsWith(ajaxExtension)) {
        return new ModelAndView(ajaxViewName);
    } else {/*from w  w w .  ja  va 2s  .  c  o m*/
        return new ModelAndView(viewName);
    }
}

From source file:org.craftercms.engine.macro.impl.ScopeAttributeMacro.java

@Override
protected String getMacroValue(String str) {
    HttpServletRequest request = RequestContext.getCurrent().getRequest();
    if (requestScope) {
        return (String) request.getAttribute(attributeName);
    } else {//  w  ww.j a  v  a  2 s  .  c o m
        return (String) request.getSession().getAttribute(attributeName);
    }
}

From source file:com.netflix.genie.web.controllers.ControllerUtilsUnitTests.java

/**
 * Test the getRemainingPath method.//  w  ww . j av  a2 s.  c o m
 */
@Test
public void canGetRemainingPath() {
    final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).thenReturn(null);
    Assert.assertNull(ControllerUtils.getRemainingPath(request));

    Mockito.when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE))
            .thenReturn("/api/v3/jobs/1234/output/genie/log.out");
    Mockito.when(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE))
            .thenReturn("/api/v3/jobs/{id}/output/**");
    Assert.assertThat(ControllerUtils.getRemainingPath(request), Matchers.is("genie/log.out"));

    Mockito.when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE))
            .thenReturn("/api/v3/jobs/1234/output");
    Mockito.when(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE))
            .thenReturn("/api/v3/jobs/{id}/output");
    Assert.assertThat(ControllerUtils.getRemainingPath(request), Matchers.is(""));

    Mockito.when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE))
            .thenReturn("/api/v3/jobs/1234/output/");
    Mockito.when(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE))
            .thenReturn("/api/v3/jobs/{id}/output/");
    Assert.assertThat(ControllerUtils.getRemainingPath(request), Matchers.is(""));

    Mockito.when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE))
            .thenReturn("/api/v3/jobs/1234/output/stdout");
    Mockito.when(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE))
            .thenReturn("/api/v3/jobs/{id}/output/**");
    Assert.assertThat(ControllerUtils.getRemainingPath(request), Matchers.is("stdout"));
}

From source file:com.baron.bm.controller.ErrorController.java

/**
 *  ?  web.xml? ?? ?/*from ww  w .  j  a  v a  2  s .  co m*/
 */
@RequestMapping("/error")
public String throwException(HttpServletRequest request, HttpServletResponse response, Model model) {
    int statusCode = (int) request.getAttribute("javax.servlet.error.status_code");

    String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
    if (requestUri == null) {
        requestUri = "   "; //Unknown
    }

    String message;

    if (statusCode == 400) {
        message = MessageFormat.format(ErrorType.ERROR_400.getMessage(), requestUri);
    } else if (statusCode == 403) {
        message = MessageFormat.format(ErrorType.ERROR_403.getMessage(), requestUri);
    } else if (statusCode == 404) {
        message = MessageFormat.format(ErrorType.ERROR_404.getMessage(), requestUri);
    } else if (statusCode == 500) {
        message = MessageFormat.format(ErrorType.ERROR_500.getMessage(), requestUri);
    } else {
        message = MessageFormat.format(ErrorType.ERROR_ELSE.getMessage(), requestUri);
    }

    model.addAttribute("errorMessage", message);
    return "/common/error";
}

From source file:org.dawnsci.marketplace.controllers.PageController.java

@RequestMapping(value = "/pages/**", method = RequestMethod.GET)
@ResponseBody// ww w  .j  a v  a 2s  . c  o m
public ResponseEntity<FileSystemResource> picture(HttpServletRequest request) {

    String resource = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE))
            .substring(1);
    Path path = fileService.getPageFile(resource).toPath();

    File file = path.toAbsolutePath().toFile();

    if (file.exists() && file.isFile()) {
        try {
            String detect = tika.detect(file);
            MediaType mediaType = MediaType.parseMediaType(detect);
            return ResponseEntity.ok().contentLength(file.length()).contentType(mediaType)
                    .body(new FileSystemResource(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        throw new ResourceNotFoundException();
    }
    return null;
}

From source file:org.hdiv.web.servlet.support.ThymeleafHdivRequestDataValueProcessor.java

@Override
public String processAction(HttpServletRequest request, String action, String method) {

    IDataComposer dataComposer = (IDataComposer) request.getAttribute(HDIVUtil.DATACOMPOSER_REQUEST_KEY);

    if (dataComposer != null && dataComposer.isRequestStarted()) {
        // End with the last form
        dataComposer.endRequest();/*  w  ww.ja  va 2  s .  co m*/
    }

    // Start with the new form
    return super.processAction(request, action, method);
}

From source file:am.ik.categolj2.core.web.accesslog.AccessLogMethodArgumentResolver.java

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
    String xTrack = StringUtils.substring((String) request.getAttribute("X-Track"), 0, 32);
    String method = request.getMethod();
    String uri = StringUtils.substring(request.getRequestURI(), 0, 128);
    String query = StringUtils.substring(request.getQueryString(), 0, 128);
    String remoteAddress = RemoteAddresses.getRemoteAddress(request);
    String userAgent = UserAgents.getUserAgent(request);
    DateTime accessDate = dateFactory.newDateTime();
    return new AccessLog(null, method, uri, query, remoteAddress, userAgent, xTrack, accessDate);
}

From source file:org.focusns.common.web.widget.WidgetDispatcherServlet.java

@Override
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    ///*w ww .  j  a  v  a  2  s  .  com*/
    String requestType = (String) request.getAttribute("requestType");
    if (!"action".equals(requestType)) {
        super.render(mv, request, response);
    }
}