Search in sources :

Example 76 with ServletContext

use of javax.servlet.ServletContext in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testDecoratedByControllerConvention.

public void testDecoratedByControllerConvention() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest(null);
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/test.gsp", "<html><body><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    // create mock controller
    GroovyObject controller = (GroovyObject) gcl.parseClass("class TestController {\n" + "def controllerName = 'test'\n" + "def actionUri = '/test/testAction'\n" + "}").newInstance();
    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Test title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/test.gsp", d.getPage());
    assertEquals("test", d.getName());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Decorator(com.opensymphony.module.sitemesh.Decorator) ServletContext(javax.servlet.ServletContext) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 77 with ServletContext

use of javax.servlet.ServletContext in project eweb4j-framework by laiweiwei.

the class EWebFilter method initContext.

/**
	 * 初始化
	 * 
	 * @param req
	 * @param res
	 * @throws Exception
	 */
private Context initContext(HttpServletRequest request, HttpServletResponse response) throws Exception {
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html");
    Context context = new Context(servletContext, request, response, null, null, null, null);
    String ip = getIpAddr(request);
    context.setIp(ip);
    // 将request的请求参数转到另外一个map中去
    Map<String, String[]> qpMap = new HashMap<String, String[]>();
    qpMap.putAll(ParamUtil.copyReqParams(context.getRequest()));
    context.setQueryParamMap(qpMap);
    //将上传的表单元素注入到context中
    if (isMultipartContent(context.getRequest()))
        UploadUtil.handleUpload(context);
    return context;
}
Also used : ServletContext(javax.servlet.ServletContext) HashMap(java.util.HashMap)

Example 78 with ServletContext

use of javax.servlet.ServletContext in project eweb4j-framework by laiweiwei.

the class EWebFilter method parseBaseUrl.

private void parseBaseUrl(Context context) throws Exception {
    ServletContext servletContext = context.getServletContext();
    HttpServletRequest request = context.getRequest();
    String uri = context.getUri();
    String parseType = MVCConfigConstant.BASE_URL_PARSE_TYPE;
    //只要不是固定的,都是用动态策略
    if (!"fixed".equalsIgnoreCase(parseType)) {
        MVCConfigConstant.BASE_URL = URLDecoder.decode(request.getRequestURL().toString(), "utf-8").replace(uri, "");
        servletContext.setAttribute(MVCConfigConstant.BASE_URL_KEY, MVCConfigConstant.BASE_URL);
    } else {
        if (servletContext.getAttribute(MVCConfigConstant.BASE_URL_KEY) == null) {
            MVCConfigConstant.BASE_URL = URLDecoder.decode(request.getRequestURL().toString(), "utf-8").replace(uri, "");
            servletContext.setAttribute(MVCConfigConstant.BASE_URL_KEY, MVCConfigConstant.BASE_URL);
        }
    }
    LogFactory.getMVCLogger(EWebFilter.class).debug("${" + MVCConfigConstant.BASE_URL_KEY + "} -> " + MVCConfigConstant.BASE_URL);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext)

Example 79 with ServletContext

use of javax.servlet.ServletContext in project eweb4j-framework by laiweiwei.

the class EWebServlet method parseBaseUrl.

private void parseBaseUrl(Context context) throws Exception {
    ServletContext servletContext = context.getServletContext();
    HttpServletRequest request = context.getRequest();
    String uri = context.getUri();
    if (servletContext.getAttribute(MVCConfigConstant.BASE_URL_KEY) == null) {
        String url = URLDecoder.decode(request.getRequestURL().toString(), "utf-8");
        String baseUrl = url.replace(uri, "");
        MVCConfigConstant.BASE_URL = baseUrl;
        servletContext.setAttribute(MVCConfigConstant.BASE_URL_KEY, baseUrl);
        LogFactory.getMVCLogger(EWebFilter.class).debug("${" + MVCConfigConstant.BASE_URL_KEY + "} -> " + baseUrl);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext)

Example 80 with ServletContext

use of javax.servlet.ServletContext in project spring-framework by spring-projects.

the class SpringWildcardServletTilesApplicationContext method getResources.

@Override
public Collection<ApplicationResource> getResources(String path) {
    Resource[] resources;
    try {
        resources = this.resolver.getResources(path);
    } catch (IOException ex) {
        ((ServletContext) getContext()).log("Resource retrieval failed for path: " + path, ex);
        return Collections.emptyList();
    }
    if (ObjectUtils.isEmpty(resources)) {
        ((ServletContext) getContext()).log("No resources found for path pattern: " + path);
        return Collections.emptyList();
    }
    Collection<ApplicationResource> resourceList = new ArrayList<>(resources.length);
    for (Resource resource : resources) {
        try {
            URL url = resource.getURL();
            resourceList.add(new URLApplicationResource(url.toExternalForm(), url));
        } catch (IOException ex) {
            // Shouldn't happen with the kind of resources we're using
            throw new IllegalArgumentException("No URL for " + resource, ex);
        }
    }
    return resourceList;
}
Also used : URLApplicationResource(org.apache.tiles.request.locale.URLApplicationResource) ApplicationResource(org.apache.tiles.request.ApplicationResource) URLApplicationResource(org.apache.tiles.request.locale.URLApplicationResource) ApplicationResource(org.apache.tiles.request.ApplicationResource) URLApplicationResource(org.apache.tiles.request.locale.URLApplicationResource) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) ServletContext(javax.servlet.ServletContext) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

ServletContext (javax.servlet.ServletContext)1059 Test (org.junit.Test)231 HttpServletRequest (javax.servlet.http.HttpServletRequest)171 IOException (java.io.IOException)138 HttpServletResponse (javax.servlet.http.HttpServletResponse)127 ServletException (javax.servlet.ServletException)95 File (java.io.File)75 ServletConfig (javax.servlet.ServletConfig)68 FilterConfig (javax.servlet.FilterConfig)65 HashMap (java.util.HashMap)63 Enumeration (java.util.Enumeration)52 InputStream (java.io.InputStream)51 ArrayList (java.util.ArrayList)49 URL (java.net.URL)47 HttpSession (javax.servlet.http.HttpSession)43 Map (java.util.Map)38 PrintWriter (java.io.PrintWriter)32 List (java.util.List)32 RequestDispatcher (javax.servlet.RequestDispatcher)30 WebApplicationContext (org.springframework.web.context.WebApplicationContext)28