View Javadoc

1   package org.apache.maven.continuum.web.view;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.opensymphony.xwork2.ActionContext;
23  import org.apache.maven.continuum.security.ContinuumRoleConstants;
24  import org.apache.maven.continuum.web.model.ProjectSummary;
25  import org.apache.struts2.views.util.UrlHelper;
26  import org.codehaus.plexus.PlexusConstants;
27  import org.codehaus.plexus.PlexusContainer;
28  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
29  import org.codehaus.plexus.redback.authorization.AuthorizationException;
30  import org.codehaus.plexus.redback.system.SecuritySession;
31  import org.codehaus.plexus.redback.system.SecuritySystem;
32  import org.codehaus.plexus.redback.system.SecuritySystemConstants;
33  import org.extremecomponents.table.bean.Column;
34  import org.extremecomponents.table.cell.DisplayCell;
35  import org.extremecomponents.table.core.TableModel;
36  
37  import java.util.HashMap;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  import javax.servlet.jsp.PageContext;
41  
42  /**
43   * Used in Summary view
44   *
45   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
46   * @version $Id: BuildCell.java 1372260 2012-08-13 04:29:09Z brett $
47   * @deprecated use of cells is discouraged due to lack of i18n and design in java code.
48   *             Use jsp:include instead.
49   */
50  public class BuildCell
51      extends DisplayCell
52  {
53      protected String getCellValue( TableModel tableModel, Column column )
54      {
55          ProjectSummary project = (ProjectSummary) tableModel.getCurrentRowBean();
56          String contextPath = tableModel.getContext().getContextPath();
57  
58          int buildNumber = project.getBuildNumber();
59  
60          String result = "<div align=\"center\">";
61  
62          if ( project.isInBuildingQueue() )
63          {
64              result +=
65                  "<img src=\"" + contextPath + "/images/inqueue.gif\" alt=\"In Queue\" title=\"In Queue\" border=\"0\">";
66          }
67          else if ( project.isInCheckoutQueue() )
68          {
69              result += "<img src=\"" + contextPath +
70                  "/images/checkingout.gif\" alt=\"Checking Out sources\" title=\"Checking Out sources\" border=\"0\">";
71          }
72          else
73          {
74              if ( project.getState() == 1 || project.getState() == 10 || project.getState() == 2 ||
75                  project.getState() == 3 || project.getState() == 4 )
76              {
77                  if ( buildNumber > 0 )
78                  {
79                      HashMap<String, Object> params = new HashMap<String, Object>();
80  
81                      params.put( "projectId", project.getId() );
82  
83                      params.put( "projectName", project.getName() );
84  
85                      params.put( "buildId", project.getBuildInSuccessId() );
86  
87                      params.put( "projectGroupId", project.getProjectGroupId() );
88  
89                      PageContext pageContext = (PageContext) tableModel.getContext().getContextObject();
90  
91                      HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
92  
93                      HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
94  
95                      String url = UrlHelper.buildUrl( "/buildResult.action", request, response, params );
96  
97                      if ( isAuthorized( project ) )
98                      {
99                          // we are authzd so act normally
100                         result += "<a href=\"" + url + "\">" + buildNumber + "</a>";
101                     }
102                     else
103                     {
104                         result += buildNumber;
105                     }
106                 }
107                 else
108                 {
109                     result += "&nbsp;";
110                 }
111             }
112             else if ( project.getState() == 6 )
113             {
114                 result += "<img src=\"" + contextPath +
115                     "/images/building.gif\" alt=\"Building\" title=\"Building\" border=\"0\">";
116             }
117             else if ( project.getState() == 7 )
118             {
119                 result += "<img src=\"" + contextPath +
120                     "/images/checkingout.gif\" alt=\"Checking Out sources\" title=\"Checking Out sources\" border=\"0\">";
121             }
122             else if ( project.getState() == 8 )
123             {
124                 result += "<img src=\"" + contextPath +
125                     "/images/checkingout.gif\" alt=\"Updating sources\" title=\"Updating sources\" border=\"0\">";
126             }
127             else
128             {
129                 result += "<img src=\"" + contextPath +
130                     "/images/inqueue.gif\" alt=\"In Queue\" title=\"In Queue\" border=\"0\">";
131             }
132         }
133 
134         return result + "</div>";
135     }
136 
137     private boolean isAuthorized( ProjectSummary project )
138     {
139         // do the authz bit
140         ActionContext context = ActionContext.getContext();
141 
142         PlexusContainer container = (PlexusContainer) context.getApplication().get( PlexusConstants.PLEXUS_KEY );
143         SecuritySession securitySession = (SecuritySession) context.getSession().get(
144             SecuritySystemConstants.SECURITY_SESSION_KEY );
145 
146         try
147         {
148             SecuritySystem securitySystem = (SecuritySystem) container.lookup( SecuritySystem.ROLE );
149 
150             if ( !securitySystem.isAuthorized( securitySession, ContinuumRoleConstants.CONTINUUM_VIEW_GROUP_OPERATION,
151                                                project.getProjectGroupName() ) )
152             {
153                 return false;
154             }
155         }
156         catch ( ComponentLookupException cle )
157         {
158             return false;
159         }
160         catch ( AuthorizationException ae )
161         {
162             return false;
163         }
164 
165         return true;
166     }
167 }