public void testDecoratedByApplicationConvention() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest();
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource(
        "WEB-INF/grails-app/views/layouts/application.gsp",
        "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");

    MockHttpServletRequest request = new MockHttpServletRequest("GET", "orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();

    // create mock controller
    GroovyObject controller =
        (GroovyObject)
            gcl.parseClass(
                    "class FooController {\n"
                        + "def controllerName = 'foo'\n"
                        + "def actionUri = '/foo/fooAction'\n"
                        + "}")
                .newInstance();

    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    Config c = new Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Foo 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("/WEB-INF/grails-app/views/layouts/application.gsp", d.getPage());
    assertEquals("application", d.getName());
  }
  private static GrailsDomainClass getDomainClass(Object instance) {
    GrailsDomainClass domainClass =
        GrailsMetaClassUtils.getPropertyIfExists(
            instance, GrailsDomainClassProperty.DOMAIN_CLASS, GrailsDomainClass.class);
    if (domainClass == null) {
      GrailsWebRequest webRequest = GrailsWebRequest.lookup();
      if (webRequest != null) {
        ApplicationContext applicationContext = webRequest.getApplicationContext();

        GrailsApplication grailsApplication =
            applicationContext.containsBean(GrailsApplication.APPLICATION_ID)
                ? applicationContext.getBean(
                    GrailsApplication.APPLICATION_ID, GrailsApplication.class)
                : null;
        if (grailsApplication != null) {
          domainClass =
              (GrailsDomainClass)
                  grailsApplication.getArtefact(
                      DomainClassArtefactHandler.TYPE, instance.getClass().getName());
        }
      }
    }

    return domainClass;
  }
示例#3
0
  private boolean renderTemplate(
      Object target, GroovyObject controller, GrailsWebRequest webRequest, Map argMap, Writer out) {
    boolean renderView;
    String templateName = argMap.get(ARGUMENT_TEMPLATE).toString();
    String contextPath = getContextPath(webRequest, argMap);

    String var = (String) argMap.get(ARGUMENT_VAR);
    // get the template uri
    String templateUri = GroovyPageUtils.getTemplateURI(controller, templateName);

    // retrieve gsp engine
    GroovyPagesTemplateEngine engine =
        (GroovyPagesTemplateEngine)
            webRequest.getApplicationContext().getBean(GroovyPagesTemplateEngine.BEAN_ID);
    try {
      Resource r = engine.getResourceForUri(contextPath + templateUri);
      if (!r.exists()) {
        r = engine.getResourceForUri(contextPath + "/grails-app/views/" + templateUri);
      }

      Template t = engine.createTemplate(r); // templateUri);

      if (t == null) {
        throw new ControllerExecutionException(
            "Unable to load template for uri [" + templateUri + "]. Template not found.");
      }
      Map binding = new HashMap();

      if (argMap.containsKey(ARGUMENT_BEAN)) {
        Object bean = argMap.get(ARGUMENT_BEAN);
        if (argMap.containsKey(ARGUMENT_MODEL)) {
          Object modelObject = argMap.get(ARGUMENT_MODEL);
          if (modelObject instanceof Map) binding.putAll((Map) modelObject);
        }
        renderTemplateForBean(t, binding, bean, var, out);
      } else if (argMap.containsKey(ARGUMENT_COLLECTION)) {
        Object colObject = argMap.get(ARGUMENT_COLLECTION);
        if (argMap.containsKey(ARGUMENT_MODEL)) {
          Object modelObject = argMap.get(ARGUMENT_MODEL);
          if (modelObject instanceof Map) binding.putAll((Map) modelObject);
        }
        renderTemplateForCollection(t, binding, colObject, var, out);
      } else if (argMap.containsKey(ARGUMENT_MODEL)) {
        Object modelObject = argMap.get(ARGUMENT_MODEL);
        renderTemplateForModel(t, modelObject, target, out);
      } else {
        Writable w = t.make(new BeanMap(target));
        w.writeTo(out);
      }
      renderView = false;
    } catch (GroovyRuntimeException gre) {
      throw new ControllerExecutionException(
          "Error rendering template [" + templateName + "]: " + gre.getMessage(), gre);
    } catch (IOException ioex) {
      throw new ControllerExecutionException(
          "I/O error executing render method for arguments [" + argMap + "]: " + ioex.getMessage(),
          ioex);
    }
    return renderView;
  }
示例#4
0
  private String getContextPath(GrailsWebRequest webRequest, Map argMap) {
    Object cp = argMap.get(ARGUMENT_CONTEXTPATH);
    String contextPath = (cp != null ? cp.toString() : "");

    Object pluginName = argMap.get(ARGUMENT_PLUGIN);
    if (pluginName != null) {
      ApplicationContext applicationContext = webRequest.getApplicationContext();
      GrailsPluginManager pluginManager =
          (GrailsPluginManager) applicationContext.getBean(GrailsPluginManager.BEAN_NAME);
      GrailsPlugin plugin = pluginManager.getGrailsPlugin(pluginName.toString());
      if (plugin != null && !plugin.isBasePlugin()) contextPath = plugin.getPluginPath();
    }
    return contextPath;
  }
示例#5
0
  private void bindWithRequestAndPropertyValues(
      ServletRequest request, MutablePropertyValues mpvs) {
    GrailsWebRequest webRequest = GrailsWebRequest.lookup((HttpServletRequest) request);
    if (webRequest != null) {
      final ApplicationContext applicationContext = webRequest.getApplicationContext();
      if (applicationContext != null) {
        final Map<String, BindEventListener> bindEventListenerMap =
            applicationContext.getBeansOfType(BindEventListener.class);
        for (BindEventListener bindEventListener : bindEventListenerMap.values()) {
          bindEventListener.doBind(getTarget(), mpvs, getTypeConverter());
        }
      }
    }
    preProcessMutablePropertyValues(mpvs);

    if (request instanceof MultipartHttpServletRequest) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      bindMultipartFiles(multipartRequest.getFileMap(), mpvs);
    }
    doBind(mpvs);
  }
  /*
   * Test method for 'org.codehaus.groovy.grails.web.sitemesh.GrailsLayoutDecoratorMapper.getDecorator(HttpServletRequest, Page)'
   */
  public void testGetDecoratorHttpServletRequestPage() throws Exception {
    GrailsWebRequest webRequest = buildMockRequest();
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource(
        "WEB-INF/grails-app/views/layouts/test.gsp", "<html><body><g:layoutBody /></body></html>");

    MockHttpServletRequest request = new MockHttpServletRequest("GET", "orders/list");
    ServletContext context = webRequest.getServletContext();
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    Config c = new Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html =
        "<html><head><title>Test title</title><meta name=\"layout\" content=\"test\"></meta></head><body>here is the body</body></html>";

    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/WEB-INF/grails-app/views/layouts/test.gsp", d.getPage());
    assertEquals("test", d.getName());
  }
  public void testDecoratedByApplicationConventionForViewsNotRenderedByAController()
      throws Exception {
    GrailsWebRequest webRequest = buildMockRequest();
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource(
        "WEB-INF/grails-app/views/layouts/application.gsp",
        "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");

    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();

    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    Config c = new Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Foo 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("/WEB-INF/grails-app/views/layouts/application.gsp", d.getPage());
    assertEquals("application", d.getName());
  }