View Javadoc
1   package org.apache.maven.wagon.providers.coreit;
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.wagon.AbstractWagon;
23  import org.apache.maven.wagon.ConnectionException;
24  import org.apache.maven.wagon.InputData;
25  import org.apache.maven.wagon.OutputData;
26  import org.apache.maven.wagon.ResourceDoesNotExistException;
27  import org.apache.maven.wagon.TransferFailedException;
28  import org.apache.maven.wagon.authentication.AuthenticationException;
29  import org.apache.maven.wagon.authentication.AuthenticationInfo;
30  import org.apache.maven.wagon.authorization.AuthorizationException;
31  import org.apache.maven.wagon.resource.Resource;
32  import org.codehaus.plexus.component.annotations.Component;
33  
34  import java.io.ByteArrayInputStream;
35  import java.io.ByteArrayOutputStream;
36  import java.io.File;
37  import java.io.FileOutputStream;
38  import java.io.IOException;
39  import java.io.InputStream;
40  import java.io.OutputStream;
41  import java.util.Properties;
42  
43  /**
44   * Shamelessly copied from ScpExternalWagon in this same project...
45   */
46  @Component( role = org.apache.maven.wagon.Wagon.class,  hint = "coreit", instantiationStrategy = "per-lookup" )
47  public class CoreItWagon
48      extends AbstractWagon
49  {
50      public void get( String resourceName, File destination )
51          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
52      {
53          InputData inputData = new InputData();
54  
55          Resource resource = new Resource( resourceName );
56  
57          fireGetInitiated( resource, destination );
58  
59          inputData.setResource( resource );
60  
61          fillInputData( inputData );
62  
63          InputStream is = inputData.getInputStream();
64  
65          if ( is == null )
66          {
67              throw new TransferFailedException(
68                  getRepository().getUrl() + " - Could not open input stream for resource: '" + resource + "'" );
69          }
70  
71          createParentDirectories( destination );
72  
73          getTransfer( inputData.getResource(), destination, is );
74      }
75  
76      public boolean getIfNewer( String resourceName, File destination, long timestamp )
77          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
78      {
79          return false;
80      }
81  
82      public void put( File source, String resourceName )
83          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
84      {
85          OutputData outputData = new OutputData();
86  
87          Resource resource = new Resource( resourceName );
88  
89          firePutInitiated( resource, source );
90  
91          outputData.setResource( resource );
92  
93          fillOutputData( outputData );
94  
95          OutputStream os = outputData.getOutputStream();
96  
97          if ( os == null )
98          {
99              throw new TransferFailedException(
100                 getRepository().getUrl() + " - Could not open output stream for resource: '" + resource + "'" );
101         }
102 
103         putTransfer( outputData.getResource(), source, os, true );
104     }
105 
106     public void closeConnection()
107         throws ConnectionException
108     {
109         File f = new File( "target/wagon-data" );
110         try
111         {
112             f.getParentFile().mkdirs();
113             f.createNewFile();
114         }
115         catch ( IOException e )
116         {
117             throw new ConnectionException( e.getMessage(), e );
118         }
119     }
120 
121     public void fillInputData( InputData inputData )
122         throws TransferFailedException, ResourceDoesNotExistException
123     {
124         try
125         {
126             String resName = inputData.getResource().getName();
127             InputStream is = null;
128             if ( resName.endsWith( ".sha1" ) )
129             {
130                 is = new ByteArrayInputStream( "c96e29be962f9d8123b584b8f51d66b347d268d4".getBytes( "UTF-8" ) );
131             }
132             else if ( resName.endsWith( ".md5" ) )
133             {
134                 is = new ByteArrayInputStream( "d2b637ab8965308490bc6482c860dfc5".getBytes( "UTF-8" ) );
135             }
136             else
137             {
138                 is = new ByteArrayInputStream( "<metadata />".getBytes( "UTF-8" ) );
139             }
140             inputData.setInputStream( is );
141         }
142         catch ( IOException e )
143         {
144             throw new TransferFailedException( "Broken JVM", e );
145         }
146     }
147 
148     public void fillOutputData( OutputData outputData )
149         throws TransferFailedException
150     {
151         Properties props = new Properties();
152 
153         if ( getRepository().getPermissions() != null )
154         {
155             String dirPerms = getRepository().getPermissions().getDirectoryMode();
156             put( props, "directory.mode", dirPerms );
157 
158             String filePerms = getRepository().getPermissions().getFileMode();
159             put( props, "file.mode", filePerms );
160         }
161 
162         AuthenticationInfo auth = getAuthenticationInfo();
163         if ( auth != null )
164         {
165             put( props, "username", auth.getUserName() );
166             put( props, "password", auth.getPassword() );
167             put( props, "privateKey", auth.getPrivateKey() );
168             put( props, "passphrase", auth.getPassphrase() );
169         }
170 
171         try
172         {
173             File file = new File( System.getProperty( "user.dir" ), "target/wagon.properties" ).getAbsoluteFile();
174             file.getParentFile().mkdirs();
175 
176             try ( OutputStream os = new FileOutputStream( file ) )
177             {
178                 props.store( os, "MAVEN-CORE-IT-WAGON" );
179             }
180         }
181         catch ( IOException e )
182         {
183             throw new TransferFailedException( e.getMessage(), e );
184         }
185 
186         outputData.setOutputStream( new ByteArrayOutputStream() );
187     }
188 
189     public void openConnection()
190         throws ConnectionException, AuthenticationException
191     {
192         // ignore
193 
194     }
195 
196     private void put( Properties props, String key, String value )
197     {
198         if ( value != null )
199         {
200             props.setProperty( key, value );
201         }
202     }
203 
204 }