View Javadoc
1   package org.apache.maven.wagon.providers.ssh.external;
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.authorization.AuthorizationException;
30  import org.apache.maven.wagon.resource.Resource;
31  import org.codehaus.plexus.component.annotations.Component;
32  
33  import java.io.ByteArrayInputStream;
34  import java.io.ByteArrayOutputStream;
35  import java.io.File;
36  import java.io.FileOutputStream;
37  import java.io.IOException;
38  import java.io.InputStream;
39  import java.io.OutputStream;
40  import java.util.Properties;
41  
42  /**
43   * NOTE: Plexus will only pick this correctly if the Class package and name are the same as that in core. This is
44   * because the core component descriptor is read, but the class is read from the latter JAR.
45   */
46  @Component( role = org.apache.maven.wagon.Wagon.class,  hint = "scpexe", instantiationStrategy = "per-lookup" )
47  public class ScpExternalWagon
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          writeTestProperties( source.getParentFile() );
94  
95          fillOutputData( outputData );
96  
97          OutputStream os = outputData.getOutputStream();
98  
99          if ( os == null )
100         {
101             throw new TransferFailedException(
102                 getRepository().getUrl() + " - Could not open output stream for resource: '" + resource + "'" );
103         }
104 
105         putTransfer( outputData.getResource(), source, os, true );
106     }
107 
108     public void closeConnection()
109         throws ConnectionException
110     {
111     }
112 
113     public void fillInputData( InputData inputData )
114         throws TransferFailedException, ResourceDoesNotExistException
115     {
116         try
117         {
118             String resName = inputData.getResource().getName();
119             InputStream is = null;
120             if ( resName.endsWith( ".sha1" ) )
121             {
122                 is = new ByteArrayInputStream( "c96e29be962f9d8123b584b8f51d66b347d268d4".getBytes( "UTF-8" ) );
123             }
124             else if ( resName.endsWith( ".md5" ) )
125             {
126                 is = new ByteArrayInputStream( "d2b637ab8965308490bc6482c860dfc5".getBytes( "UTF-8" ) );
127             }
128             else
129             {
130                 is = new ByteArrayInputStream( "<metadata />".getBytes( "UTF-8" ) );
131             }
132             inputData.setInputStream( is );
133         }
134         catch ( IOException e )
135         {
136             throw new TransferFailedException( "Broken JVM", e );
137         }
138     }
139 
140     public void writeTestProperties( File dir )
141         throws TransferFailedException
142     {
143         Properties props = new Properties();
144         if ( getRepository().getPermissions() != null )
145         {
146             String dirPerms = getRepository().getPermissions().getDirectoryMode();
147 
148             if ( dirPerms != null )
149             {
150                 props.setProperty( "directory.mode", dirPerms );
151             }
152 
153             String filePerms = getRepository().getPermissions().getFileMode();
154             if ( filePerms != null )
155             {
156                 props.setProperty( "file.mode", filePerms );
157             }
158         }
159 
160         try
161         {
162             try ( OutputStream os = new FileOutputStream( new File( dir, "wagon.properties" ) ) )
163             {
164                 props.store( os, "MAVEN-CORE-IT-WAGON" );
165             }
166         }
167         catch ( IOException e )
168         {
169             throw new TransferFailedException( e.getMessage(), e );
170         }
171     }
172 
173     public void fillOutputData( OutputData outputData )
174         throws TransferFailedException
175     {
176         outputData.setOutputStream( new ByteArrayOutputStream() );
177     }
178 
179     public void openConnection()
180         throws ConnectionException, AuthenticationException
181     {
182     }
183 }