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());
}
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;
}
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);
}
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);
}
}
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;
}
Aggregations