View Javadoc

1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.fridaymvc.managers.view;
18  
19  import java.io.File;
20  import java.io.UnsupportedEncodingException;
21  import java.net.URLEncoder;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import javax.servlet.ServletContext;
27  import javax.servlet.http.HttpServletRequest;
28  
29  import net.sf.fridaymvc.RunData;
30  
31  import org.springframework.web.servlet.LocaleResolver;
32  
33  /***
34   * @author <a href="mailto:arto.pastinen@ofw.fi">Arto Pastinen</a>
35   * @version $Id: LinkViewManager.java,v 1.2 2004/11/23 20:35:58 artsi Exp $
36   */
37  
38  public class LinkViewManager {
39      protected ThreadLocal attributes = new ThreadLocal() {
40          /* (non-Javadoc)
41           * @see java.lang.ThreadLocal#initialValue()
42           */
43          protected Object initialValue() {
44              return new HashMap(1);
45          }
46      };
47  
48      public String setPanel(String uid, String viewName) {
49          HttpServletRequest req = RunData.getInstance().getHttpServletRequest();        
50          StringBuffer sb = new StringBuffer(req.getContextPath());
51          synchronized(sb) {
52              sb.append(req.getServletPath());
53              sb.append("?action=setpanel&action.uid=");
54              sb.append(uid);            
55              sb.append("&action.viewname=");
56              sb.append(viewName);
57          }
58          return RunData.getInstance().getHttpServletResponse().encodeURL(sb.toString());
59      }
60      
61      public void setAttribute(String name, String value) {
62          getAttributes().put(name, value);
63      }
64      
65      public String current() throws UnsupportedEncodingException {
66          HttpServletRequest req = RunData.getInstance().getHttpServletRequest();
67          StringBuffer sb = new StringBuffer(req.getContextPath());
68          synchronized(sb) {
69              sb.append(req.getServletPath());
70              Map attributes = getAttributes();
71              if(attributes.size() > 0) {
72                  sb.append("?");
73                  String key, value;
74                  for (Iterator i = attributes.keySet().iterator(); i.hasNext();) {
75                       key = (String) i.next();
76                       value = (String) attributes.get(key);
77                       if(value == null) continue;
78                       sb.append(RunData.getInstance().getCurrentViewName());
79                       sb.append(".");
80                       sb.append(URLEncoder.encode(key, RunData.getInstance().getHttpServletResponse().getCharacterEncoding()));
81                       sb.append("=");
82                       sb.append(URLEncoder.encode(value, RunData.getInstance().getHttpServletResponse().getCharacterEncoding()));
83                       if(i.hasNext()) sb.append("&");
84                  }
85                  attributes.clear();
86              }            
87          }
88          return RunData.getInstance().getHttpServletResponse().encodeURL(sb.toString());
89      }
90      
91      /***
92       * @return Returns the attributes.
93       */
94      public Map getAttributes() {
95          return (Map) this.attributes.get();
96      }
97      /***
98       * @param attributes The attributes to set.
99       */
100     public void setAttributes(Map attributes) {
101         this.attributes.set(attributes);
102     }
103     
104     public String image(String imageName) {
105         String imageFinalName = imageName;        
106         String tmp[] = imageName.split("//.");
107         if(tmp.length != 2) return "";        
108         LocaleResolver lr = (LocaleResolver) RunData.getInstance().getBean("localeResolver");
109         if(lr != null) {
110             ServletContext sc = RunData.getInstance().getWebApplicationContext().getServletContext();
111             String tmpName = tmp[0] + "_" + lr.resolveLocale(RunData.getInstance().getHttpServletRequest()) + "." + tmp[1];            
112             File f = new File(sc.getRealPath("/images/" + tmpName));
113             if(f.exists()) imageFinalName = tmpName;            
114         }        
115         return RunData.getInstance().getHttpServletRequest().getContextPath() + "/images/" + imageFinalName;        
116     }
117 }