View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.io.IOException;
22  import java.util.Map;
23  
24  import javax.servlet.jsp.tagext.BodyTagSupport;
25  
26  import org.springframework.extensions.surf.render.RenderContext;
27  import org.springframework.extensions.surf.taglib.ResourceTag;
28  import org.springframework.extensions.surf.util.StringBuilderWriter;
29  
30  import freemarker.core.Environment;
31  import freemarker.template.TemplateDirectiveBody;
32  import freemarker.template.TemplateException;
33  import freemarker.template.TemplateModel;
34  
35  /**
36   * Outputs the requested url representation of a resource
37   * 
38   * Named Resource Examples:
39   * 
40   *    <@res name="resourceName" />
41   *    <@res id="<protocol>://<endpoint>/<object id>" />
42   *    <@res protocol="<protocol>" endpoint="<endpoint>" object="<object id>" />
43   *    
44   *    <@res name="resourceName" payload="metadata" />
45   *    <@res name="resourceName" payload="content" />
46   * 
47   * @author muzquiano
48   */
49  public class ResourceFreemarkerTagDirective extends FreemarkerTagSupportDirective
50  {
51      public ResourceFreemarkerTagDirective(RenderContext context)
52      {
53          super(context);
54      }
55  
56      public void execute(Environment env, Map params, TemplateModel[] loopVars,
57              TemplateDirectiveBody body) throws TemplateException, IOException
58      {
59          // instantiate the tag class
60          ResourceTag tag = new ResourceTag();
61          
62          // resource name
63          tag.setName(getStringParameter(params, "name"));
64          
65          // resource id
66          tag.setId(getStringParameter(params, "id"));
67          
68          // resource protocol, endpoint and object
69          tag.setProtocol(getStringParameter(params, "protocol"));
70          tag.setEndpoint(getStringParameter(params, "endpoint"));
71          tag.setObject(getStringParameter(params, "object"));
72          
73          // resource payload
74          tag.setPayload(getStringParameter(params, "payload"));
75          
76          // copy in body content (if there is any)
77          String bodyContentString = null;
78          if (body != null)
79          {
80              if (tag instanceof BodyTagSupport)
81              {
82                  // dump out the Freemarker body content
83                  StringBuilderWriter bodyStringWriter = new StringBuilderWriter(256);
84                  body.render(bodyStringWriter);
85                  bodyContentString = bodyStringWriter.toString();
86              }
87          }
88          
89          /**
90           * Execute the tag
91           */
92          try
93          {
94              String output = executeTag(tag, bodyContentString);
95              env.getOut().write(output);
96          }
97          catch (Exception ex)
98          {
99              throw new TemplateException("Unable to process tag and commit output", ex, env);
100         }        
101     }
102 }