View Javadoc
1   package org.eclipse.aether.transport.wagon;
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 java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.net.URI;
28  import java.nio.charset.StandardCharsets;
29  import java.util.Map;
30  import java.util.Properties;
31  
32  import org.apache.maven.wagon.AbstractWagon;
33  import org.apache.maven.wagon.ConnectionException;
34  import org.apache.maven.wagon.InputData;
35  import org.apache.maven.wagon.OutputData;
36  import org.apache.maven.wagon.ResourceDoesNotExistException;
37  import org.apache.maven.wagon.TransferFailedException;
38  import org.apache.maven.wagon.authentication.AuthenticationException;
39  import org.apache.maven.wagon.authorization.AuthorizationException;
40  import org.apache.maven.wagon.events.TransferEvent;
41  import org.apache.maven.wagon.resource.Resource;
42  
43  /**
44   */
45  public class MemWagon
46      extends AbstractWagon
47      implements Configurable
48  {
49  
50      private Map<String, String> fs;
51  
52      private Properties headers;
53  
54      private Object config;
55  
56      public void setConfiguration( Object config )
57      {
58          this.config = config;
59      }
60  
61      public Object getConfiguration()
62      {
63          return config;
64      }
65  
66      public void setHttpHeaders( Properties httpHeaders )
67      {
68          headers = httpHeaders;
69      }
70  
71      @Override
72      protected void openConnectionInternal()
73          throws ConnectionException, AuthenticationException
74      {
75          fs =
76              MemWagonUtils.openConnection( this, getAuthenticationInfo(),
77                                            getProxyInfo( "mem", getRepository().getHost() ), headers );
78      }
79  
80      @Override
81      protected void closeConnection()
82          throws ConnectionException
83      {
84          fs = null;
85      }
86  
87      private String getData( String resource )
88      {
89          return fs.get( URI.create( resource ).getSchemeSpecificPart() );
90      }
91  
92      @Override
93      public boolean resourceExists( String resourceName )
94          throws TransferFailedException, AuthorizationException
95      {
96          String data = getData( resourceName );
97          return data != null;
98      }
99  
100     public void get( String resourceName, File destination )
101         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
102     {
103         getIfNewer( resourceName, destination, 0 );
104     }
105 
106     public boolean getIfNewer( String resourceName, File destination, long timestamp )
107         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
108     {
109         Resource resource = new Resource( resourceName );
110         fireGetInitiated( resource, destination );
111         resource.setLastModified( timestamp );
112         getTransfer( resource, destination, getInputStream( resource ) );
113         return true;
114     }
115 
116     protected InputStream getInputStream( Resource resource )
117         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
118     {
119         InputData inputData = new InputData();
120         inputData.setResource( resource );
121         try
122         {
123             fillInputData( inputData );
124         }
125         catch ( TransferFailedException | AuthorizationException | ResourceDoesNotExistException e )
126         {
127             fireTransferError( resource, e, TransferEvent.REQUEST_GET );
128             cleanupGetTransfer( resource );
129             throw e;
130         }
131         finally
132         {
133             if ( inputData.getInputStream() == null )
134             {
135                 cleanupGetTransfer( resource );
136             }
137         }
138         return inputData.getInputStream();
139     }
140 
141     protected void fillInputData( InputData inputData )
142         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
143     {
144         String data = getData( inputData.getResource().getName() );
145         if ( data == null )
146         {
147             throw new ResourceDoesNotExistException( "Missing resource: " + inputData.getResource().getName() );
148         }
149         byte[] bytes = data.getBytes( StandardCharsets.UTF_8 );
150         inputData.getResource().setContentLength( bytes.length );
151         inputData.setInputStream( new ByteArrayInputStream( bytes ) );
152     }
153 
154     public void put( File source, String resourceName )
155         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
156     {
157         Resource resource = new Resource( resourceName );
158         firePutInitiated( resource, source );
159         resource.setContentLength( source.length() );
160         resource.setLastModified( source.lastModified() );
161         OutputStream os = getOutputStream( resource );
162         putTransfer( resource, source, os, true );
163     }
164 
165     protected OutputStream getOutputStream( Resource resource )
166         throws TransferFailedException
167     {
168         OutputData outputData = new OutputData();
169         outputData.setResource( resource );
170         try
171         {
172             fillOutputData( outputData );
173         }
174         catch ( TransferFailedException e )
175         {
176             fireTransferError( resource, e, TransferEvent.REQUEST_PUT );
177             throw e;
178         }
179         finally
180         {
181             if ( outputData.getOutputStream() == null )
182             {
183                 cleanupPutTransfer( resource );
184             }
185         }
186 
187         return outputData.getOutputStream();
188     }
189 
190     protected void fillOutputData( OutputData outputData )
191         throws TransferFailedException
192     {
193         outputData.setOutputStream( new ByteArrayOutputStream() );
194     }
195 
196     @Override
197     protected void finishPutTransfer( Resource resource, InputStream input, OutputStream output )
198         throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException
199     {
200         String data = new String( ( (ByteArrayOutputStream) output ).toByteArray(), StandardCharsets.UTF_8 );
201         fs.put( URI.create( resource.getName() ).getSchemeSpecificPart(), data );
202     }
203 
204 }