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.InputStream;
25  import java.io.OutputStream;
26  import java.net.URI;
27  import java.nio.charset.StandardCharsets;
28  import java.util.Map;
29  import java.util.Properties;
30  
31  import org.apache.maven.wagon.ConnectionException;
32  import org.apache.maven.wagon.InputData;
33  import org.apache.maven.wagon.OutputData;
34  import org.apache.maven.wagon.ResourceDoesNotExistException;
35  import org.apache.maven.wagon.StreamWagon;
36  import org.apache.maven.wagon.TransferFailedException;
37  import org.apache.maven.wagon.authentication.AuthenticationException;
38  import org.apache.maven.wagon.authorization.AuthorizationException;
39  import org.apache.maven.wagon.resource.Resource;
40  
41  /**
42   */
43  public class MemStreamWagon
44      extends StreamWagon
45      implements Configurable
46  {
47  
48      private Map<String, String> fs;
49  
50      private Properties headers;
51  
52      private Object config;
53  
54      public void setConfiguration( Object config )
55      {
56          this.config = config;
57      }
58  
59      public Object getConfiguration()
60      {
61          return config;
62      }
63  
64      public void setHttpHeaders( Properties httpHeaders )
65      {
66          headers = httpHeaders;
67      }
68  
69      @Override
70      protected void openConnectionInternal()
71          throws ConnectionException, AuthenticationException
72      {
73          fs =
74              MemWagonUtils.openConnection( this, getAuthenticationInfo(),
75                                            getProxyInfo( "mem", getRepository().getHost() ), headers );
76      }
77  
78      @Override
79      public void closeConnection()
80          throws ConnectionException
81      {
82          fs = null;
83      }
84  
85      private String getData( String resource )
86      {
87          return fs.get( URI.create( resource ).getSchemeSpecificPart() );
88      }
89  
90      @Override
91      public boolean resourceExists( String resourceName )
92          throws TransferFailedException, AuthorizationException
93      {
94          String data = getData( resourceName );
95          return data != null;
96      }
97  
98      @Override
99      public void fillInputData( InputData inputData )
100         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
101     {
102         String data = getData( inputData.getResource().getName() );
103         if ( data == null )
104         {
105             throw new ResourceDoesNotExistException( "Missing resource: " + inputData.getResource().getName() );
106         }
107         byte[] bytes = data.getBytes( StandardCharsets.UTF_8 );
108         inputData.getResource().setContentLength( bytes.length );
109         inputData.setInputStream( new ByteArrayInputStream( bytes ) );
110     }
111 
112     @Override
113     public void fillOutputData( OutputData outputData )
114         throws TransferFailedException
115     {
116         outputData.setOutputStream( new ByteArrayOutputStream() );
117     }
118 
119     @Override
120     protected void finishPutTransfer( Resource resource, InputStream input, OutputStream output )
121         throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException
122     {
123         String data = new String( ( (ByteArrayOutputStream) output ).toByteArray(), StandardCharsets.UTF_8 );
124         fs.put( URI.create( resource.getName() ).getSchemeSpecificPart(), data );
125     }
126 
127 }