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.io.UnsupportedEncodingException;
28  import java.net.URI;
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 e )
126         {
127             fireTransferError( resource, e, TransferEvent.REQUEST_GET );
128             cleanupGetTransfer( resource );
129             throw e;
130         }
131         catch ( ResourceDoesNotExistException e )
132         {
133             fireTransferError( resource, e, TransferEvent.REQUEST_GET );
134             cleanupGetTransfer( resource );
135             throw e;
136         }
137         catch ( AuthorizationException e )
138         {
139             fireTransferError( resource, e, TransferEvent.REQUEST_GET );
140             cleanupGetTransfer( resource );
141             throw e;
142         }
143         finally
144         {
145             if ( inputData.getInputStream() == null )
146             {
147                 cleanupGetTransfer( resource );
148             }
149         }
150         return inputData.getInputStream();
151     }
152 
153     protected void fillInputData( InputData inputData )
154         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
155     {
156         String data = getData( inputData.getResource().getName() );
157         if ( data == null )
158         {
159             throw new ResourceDoesNotExistException( "Missing resource: " + inputData.getResource().getName() );
160         }
161         byte[] bytes;
162         try
163         {
164             bytes = data.getBytes( "UTF-8" );
165         }
166         catch ( UnsupportedEncodingException e )
167         {
168             throw new TransferFailedException( e.getMessage(), e );
169         }
170         inputData.getResource().setContentLength( bytes.length );
171         inputData.setInputStream( new ByteArrayInputStream( bytes ) );
172     }
173 
174     public void put( File source, String resourceName )
175         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
176     {
177         Resource resource = new Resource( resourceName );
178         firePutInitiated( resource, source );
179         resource.setContentLength( source.length() );
180         resource.setLastModified( source.lastModified() );
181         OutputStream os = getOutputStream( resource );
182         putTransfer( resource, source, os, true );
183     }
184 
185     protected OutputStream getOutputStream( Resource resource )
186         throws TransferFailedException
187     {
188         OutputData outputData = new OutputData();
189         outputData.setResource( resource );
190         try
191         {
192             fillOutputData( outputData );
193         }
194         catch ( TransferFailedException e )
195         {
196             fireTransferError( resource, e, TransferEvent.REQUEST_PUT );
197             throw e;
198         }
199         finally
200         {
201             if ( outputData.getOutputStream() == null )
202             {
203                 cleanupPutTransfer( resource );
204             }
205         }
206 
207         return outputData.getOutputStream();
208     }
209 
210     protected void fillOutputData( OutputData outputData )
211         throws TransferFailedException
212     {
213         outputData.setOutputStream( new ByteArrayOutputStream() );
214     }
215 
216     @Override
217     protected void finishPutTransfer( Resource resource, InputStream input, OutputStream output )
218         throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException
219     {
220         String data;
221         try
222         {
223             data = ( (ByteArrayOutputStream) output ).toString( "UTF-8" );
224         }
225         catch ( UnsupportedEncodingException e )
226         {
227             throw new TransferFailedException( e.getMessage(), e );
228         }
229         fs.put( URI.create( resource.getName() ).getSchemeSpecificPart(), data );
230     }
231 
232 }