View Javadoc

1   /*
2    * REPOWEB, repository manager.
3    *
4    * Terms of license - http://opensource.org/licenses/apachepl.php
5    */
6   package org.repoweb.tag;
7   import java.io.IOException;
8   import java.lang.reflect.InvocationTargetException;
9   import javax.servlet.jsp.JspException;
10  import javax.servlet.jsp.PageContext;
11  import javax.servlet.jsp.tagext.TagSupport;
12  import org.apache.commons.beanutils.PropertyUtils;
13  import org.repoweb.model.Artifact;
14  import org.repoweb.model.Group;
15  import org.repoweb.pom.ArtifactInfo;
16  /***
17   * Tag to make a link for various repoweb page.
18   */
19  public class LinkTag extends TagSupport {
20      public static final String TO_DOWNLOAD = "download";
21      public static final String TO_DETAIL = "detail";
22      public static final String TO_IBIBLIO = "ibiblio";
23      public static final String TO_GROUP = "group";
24      private static final String DOWNLOAD = "/do/download/";
25      static final String DISPLAY = "/do/maven/";
26      private String _label = null;
27      private String _to = TO_DOWNLOAD;
28      private String _property = null;
29  
30      private static String downloadUrl(final PageContext context, final Artifact art) {
31          return "/" + context.getServletContext().getServletContextName() + DOWNLOAD
32          + art.getGroupId() + "/" + art.getType() + "s/" + art.getFileName();
33      }
34  
35  
36      private static String ibiblioUrl(final Artifact art) {
37          return "http://www.ibiblio.org/maven/" + art.getGroupId() + "/" + art.getType()
38          + "s/";
39      }
40  
41  
42      public static String groupURL(final PageContext context, final Object group) {
43          Object groupId = group;
44          if (group instanceof Group) {
45              groupId = ((Group)group).getGroupId();
46          }
47          return "/" + context.getServletContext().getServletContextName() + DISPLAY
48          + groupId;
49      }
50  
51  
52      public static String detailUrl(final PageContext context, final ArtifactInfo art) {
53          final String contextName = context.getServletContext().getServletContextName();
54          return "/" + contextName + DISPLAY + art.getGroupId() + "/" + art.getType()
55          + "s/" + art.getFileName();
56      }
57  
58  
59      public int doStartTag() throws JspException {
60          try {
61              final Object art = getValue();
62              if (art != null) {
63                  pageContext.getOut().print("<a href=\"");
64                  pageContext.getOut().print(buildUrl(art));
65                  pageContext.getOut().print("\">");
66                  if (getLabel() != null) {
67                      pageContext.getOut().print(PropertyUtils.getProperty(art, getLabel()));
68                      return SKIP_BODY;
69                  }
70              }
71              else {
72                  setId(null);
73              }
74          }
75          catch (ClassCastException ex) {
76              throw new JspException("The '" + getId()
77                  + "' is not an instance of Artifact.", ex);
78          }
79          catch (Exception ex) {
80              throw new JspException("Unable to execute repoweb:link tag on " + getId()
81                  + " due to " + ex.getLocalizedMessage(), ex);
82          }
83          return EVAL_BODY_INCLUDE;
84      }
85  
86  
87      public int doEndTag() throws JspException {
88          if (getId() == null) {
89              return EVAL_PAGE;
90          }
91          try {
92              pageContext.getOut().print("</a>");
93          }
94          catch (IOException ex) {
95              throw new JspException("An IOException occurred. " + ex.getLocalizedMessage(),
96                  ex);
97          }
98          return EVAL_PAGE;
99      }
100 
101 
102     private String buildUrl(final Object art) {
103         if (TO_DETAIL.equals(_to)) {
104             return detailUrl(pageContext, (Artifact)art);
105         }
106         else if (TO_IBIBLIO.equals(_to)) {
107             return ibiblioUrl((Artifact)art);
108         }
109         else if (TO_GROUP.equals(_to)) {
110             return groupURL(pageContext, art);
111         }
112         else {
113             return downloadUrl(pageContext, (Artifact)art);
114         }
115     }
116 
117 
118     private Object getValue()
119         throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
120         final Object idBean = pageContext.getAttribute(getId());
121         if (getProperty() == null) {
122             return idBean;
123         }
124         else {
125             return PropertyUtils.getProperty(idBean, getProperty());
126         }
127     }
128 
129 
130     public String getLabel() {
131         return _label;
132     }
133 
134 
135     public void setLabel(String label) {
136         _label = label;
137     }
138 
139 
140     public String getTo() {
141         return _to;
142     }
143 
144 
145     public void setTo(String to) {
146         _to = to;
147     }
148 
149 
150     public String getProperty() {
151         return _property;
152     }
153 
154 
155     public void setProperty(String property) {
156         _property = property;
157     }
158 }