View Javadoc
1   package org.apache.maven.plugin.trac;
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 org.apache.maven.plugin.issues.Issue;
23  import org.apache.maven.project.MavenProject;
24  import org.apache.xmlrpc.XmlRpcException;
25  import org.apache.xmlrpc.client.XmlRpcClient;
26  import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
27  import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  import java.net.MalformedURLException;
31  import java.net.URL;
32  import java.text.ParseException;
33  import java.text.SimpleDateFormat;
34  import java.util.ArrayList;
35  import java.util.Calendar;
36  import java.util.Date;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Map;
40  
41  /**
42   * Get issues from a Trac installation.
43   *
44   * @author Dennis Lundberg
45   * @version $Id: TracDownloader.java 1579078 2014-03-18 22:44:41Z dennisl $
46   * @since 2.4
47   */
48  public class TracDownloader
49  {
50      /** The Maven project. */
51      private MavenProject project;
52      /** The Trac query for searching for tickets. */
53      private String query;
54      /** The password for authentication into a private Trac installation. */
55      private String tracPassword;
56      /** The username for authentication into a private Trac installation. */
57      private String tracUser;
58  
59      private Issue createIssue( Object[] ticketObj )
60      {
61          Issue issue = new Issue();
62  
63          issue.setId( String.valueOf( ticketObj[0] ) );
64  
65          issue.setKey( String.valueOf( ticketObj[0] ) );
66  
67          issue.setLink( getUrl() + "/ticket/" + String.valueOf( ticketObj[0] ) );
68  
69          issue.setCreated( parseDate( String.valueOf( ticketObj[1] ) ) );
70  
71          issue.setUpdated( parseDate( String.valueOf( ticketObj[2] ) ) );
72  
73          Map attributes = (Map) ticketObj[3];
74  
75          issue.setType( (String) attributes.get( "type" ) );
76  
77          issue.setSummary( (String) attributes.get( "summary" ) );
78  
79          issue.setStatus( (String) attributes.get( "status" ) );
80  
81          issue.setResolution( (String) attributes.get( "resolution" ) );
82  
83          issue.setAssignee( (String) attributes.get( "owner" ) );
84  
85          issue.addFixVersion( (String) attributes.get( "milestone" ) );
86  
87          issue.setPriority( (String) attributes.get( "priority" ) );
88  
89          issue.setReporter( (String) attributes.get( "reporter" ) );
90  
91          issue.addComponent( (String) attributes.get( "component" ) );
92  
93          return issue;
94      }
95  
96      public List<Issue> getIssueList() throws MalformedURLException, XmlRpcException
97      {
98          // Create and configure an XML-RPC client
99          XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
100 
101         try
102         {
103             config.setServerURL( new URL( getUrl() + "/login/xmlrpc" ) );
104         }
105         catch ( MalformedURLException e )
106         {
107             throw new MalformedURLException( "The Trac URL is incorrect." );
108         }
109         config.setBasicUserName( tracUser );
110         config.setBasicPassword( tracPassword );
111 
112         XmlRpcClient client = new XmlRpcClient();
113 
114         client.setConfig( config );
115 
116         client.setTransportFactory( new XmlRpcCommonsTransportFactory( client ) );
117 
118         // Fetch issues
119         String qstr = "";
120 
121         if ( !StringUtils.isEmpty( query ) )
122         {
123             qstr = query;
124         }
125 
126         Object[] params = new Object[] {qstr};
127         Object[] queryResult;
128         ArrayList<Issue> issueList = new ArrayList<Issue>();
129         try
130         {
131             queryResult = (Object[]) client.execute( "ticket.query", params );
132 
133             for ( Object aQueryResult : queryResult )
134             {
135                 params = new Object[]{aQueryResult};
136                 Object[] ticketGetResult;
137                 ticketGetResult = (Object[]) client.execute( "ticket.get", params );
138                 issueList.add( createIssue( ticketGetResult ) );
139             }
140         }
141         catch ( XmlRpcException e )
142         {
143             throw new XmlRpcException( "XmlRpc Error.", e );
144         }
145         return issueList;
146     }
147 
148     private String getUrl()
149     {
150 
151         String url = project.getIssueManagement().getUrl();
152 
153         if ( url.endsWith( "/" ) )
154         {
155             url = url.substring( 0, url.length() - 1 );
156         }
157 
158         return url;
159     }
160 
161     public void setProject( MavenProject project )
162     {
163         this.project = project;
164     }
165 
166     public void setQuery( String query )
167     {
168         this.query = query;
169     }
170 
171     public void setTracPassword( String tracPassword )
172     {
173         this.tracPassword = tracPassword;
174     }
175 
176     public void setTracUser( String tracUser )
177     {
178         this.tracUser = tracUser;
179     }
180 
181     private Date parseDate( String timeCreated )
182         throws RuntimeException
183     {
184         try
185         {
186             long millis = Long.parseLong( timeCreated );
187             Calendar cld = Calendar.getInstance();
188             cld.setTimeInMillis( millis * 1000L );
189             return cld.getTime();
190         }
191         catch ( NumberFormatException e )
192         {
193             SimpleDateFormat format = new SimpleDateFormat( "EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH );
194             try
195             {
196                 return format.parse( timeCreated );
197             }
198             catch ( ParseException e1 )
199             {
200                 throw new RuntimeException( "Failed to parse date '" + timeCreated + "' as a date.", e1 );
201             }
202         }
203     }
204 }