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