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.continuum.model.project.ProjectScmRoot;
24  import org.apache.maven.continuum.project.ContinuumProjectState;
25  import org.apache.maven.continuum.security.ContinuumRoleConstants;
26  import org.apache.maven.continuum.web.model.ProjectSummary;
27  import org.apache.maven.continuum.web.util.StateGenerator;
28  import org.apache.struts2.ServletActionContext;
29  import org.apache.struts2.views.util.UrlHelper;
30  import org.codehaus.plexus.PlexusConstants;
31  import org.codehaus.plexus.PlexusContainer;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  import org.codehaus.plexus.redback.authorization.AuthorizationException;
34  import org.codehaus.plexus.redback.system.SecuritySession;
35  import org.codehaus.plexus.redback.system.SecuritySystem;
36  import org.codehaus.plexus.redback.system.SecuritySystemConstants;
37  import org.extremecomponents.table.bean.Column;
38  import org.extremecomponents.table.cell.DisplayCell;
39  import org.extremecomponents.table.core.TableModel;
40  
41  import java.util.HashMap;
42  
43  /**
44   * Used in Summary view
45   *
46   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
47   * @version $Id: StateCell.java 1372260 2012-08-13 04:29:09Z brett $
48   * @deprecated use of cells is discouraged due to lack of i18n and design in java code.
49   *             Use jsp:include instead.
50   */
51  public class StateCell
52      extends DisplayCell
53  {
54      protected String getCellValue( TableModel tableModel, Column column )
55      {
56          if ( tableModel.getCurrentRowBean() instanceof ProjectSummary )
57          {
58              ProjectSummary project = (ProjectSummary) tableModel.getCurrentRowBean();
59  
60              switch ( project.getState() )
61              {
62                  case ContinuumProjectState.NEW:
63                  case ContinuumProjectState.OK:
64                  case ContinuumProjectState.FAILED:
65                  case ContinuumProjectState.ERROR:
66                  case ContinuumProjectState.BUILDING:
67                  case ContinuumProjectState.UPDATING:
68                  case ContinuumProjectState.CHECKING_OUT:
69                  {
70                      String state = StateGenerator.generate( project.getState(),
71                                                              tableModel.getContext().getContextPath() );
72  
73                      if ( project.getLatestBuildId() != -1 && !StateGenerator.NEW.equals( state ) &&
74                          project.getState() != ContinuumProjectState.UPDATING )
75                      {
76                          if ( isAuthorized( project.getProjectGroupName() ) )
77                          {
78                              return createActionLink( "buildResult", project, state );
79                          }
80                          else
81                          {
82                              return state;
83                          }
84                      }
85                      else
86                      {
87                          return state;
88                      }
89                  }
90  
91                  default:
92                  {
93                      return "&nbsp;";
94                  }
95              }
96          }
97          else
98          {
99              ProjectScmRoot projectScmRoot = (ProjectScmRoot) tableModel.getCurrentRowBean();
100 
101             switch ( projectScmRoot.getState() )
102             {
103                 case ContinuumProjectState.UPDATING:
104                 case ContinuumProjectState.UPDATED:
105                 case ContinuumProjectState.ERROR:
106                 {
107                     String state = StateGenerator.generate( projectScmRoot.getState(),
108                                                             tableModel.getContext().getContextPath() );
109 
110                     if ( !StateGenerator.NEW.equals( state ) )
111                     {
112                         if ( isAuthorized( projectScmRoot.getProjectGroup().getName() ) &&
113                             projectScmRoot.getState() == ContinuumProjectState.ERROR )
114                         {
115                             return createActionLink( "scmResult", projectScmRoot, state );
116                         }
117                         else
118                         {
119                             return state;
120                         }
121                     }
122                     else
123                     {
124                         return state;
125                     }
126                 }
127 
128                 default:
129                 {
130                     return "&nbsp;";
131                 }
132             }
133         }
134     }
135 
136     private static String createActionLink( String action, ProjectSummary project, String state )
137     {
138         HashMap<String, Object> params = new HashMap<String, Object>();
139 
140         params.put( "projectId", project.getId() );
141 
142         params.put( "projectName", project.getName() );
143 
144         params.put( "buildId", project.getLatestBuildId() );
145 
146         params.put( "projectGroupId", project.getProjectGroupId() );
147 
148         String url = UrlHelper.buildUrl( "/" + action + ".action", ServletActionContext.getRequest(),
149                                          ServletActionContext.getResponse(), params );
150 
151         return "<a href=\"" + url + "\">" + state + "</a>";
152     }
153 
154     private static String createActionLink( String action, ProjectScmRoot scmRoot, String state )
155     {
156         HashMap<String, Object> params = new HashMap<String, Object>();
157 
158         params.put( "projectGroupId", scmRoot.getProjectGroup().getId() );
159 
160         params.put( "projectScmRootId", scmRoot.getId() );
161 
162         String url = UrlHelper.buildUrl( "/" + action + ".action", ServletActionContext.getRequest(),
163                                          ServletActionContext.getResponse(), params );
164 
165         return "<a href=\"" + url + "\">" + state + "</a>";
166     }
167 
168     private boolean isAuthorized( String projectGroupName )
169     {
170         // do the authz bit
171         ActionContext context = ActionContext.getContext();
172 
173         PlexusContainer container = (PlexusContainer) context.getApplication().get( PlexusConstants.PLEXUS_KEY );
174         SecuritySession securitySession = (SecuritySession) context.getSession().get(
175             SecuritySystemConstants.SECURITY_SESSION_KEY );
176 
177         try
178         {
179             SecuritySystem securitySystem = (SecuritySystem) container.lookup( SecuritySystem.ROLE );
180 
181             if ( !securitySystem.isAuthorized( securitySession, ContinuumRoleConstants.CONTINUUM_VIEW_GROUP_OPERATION,
182                                                projectGroupName ) )
183             {
184                 return false;
185             }
186         }
187         catch ( ComponentLookupException cle )
188         {
189             return false;
190         }
191         catch ( AuthorizationException ae )
192         {
193             return false;
194         }
195 
196         return true;
197     }
198 }