View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.transport.wagon;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.net.URI;
26  import java.nio.charset.StandardCharsets;
27  import java.util.Map;
28  import java.util.Properties;
29  
30  import org.apache.maven.wagon.ConnectionException;
31  import org.apache.maven.wagon.InputData;
32  import org.apache.maven.wagon.OutputData;
33  import org.apache.maven.wagon.ResourceDoesNotExistException;
34  import org.apache.maven.wagon.StreamWagon;
35  import org.apache.maven.wagon.TransferFailedException;
36  import org.apache.maven.wagon.authentication.AuthenticationException;
37  import org.apache.maven.wagon.authorization.AuthorizationException;
38  import org.apache.maven.wagon.resource.Resource;
39  
40  /**
41   */
42  public class MemStreamWagon extends StreamWagon implements Configurable {
43  
44      private Map<String, String> fs;
45  
46      private Properties headers;
47  
48      private Object config;
49  
50      public void setConfiguration(Object config) {
51          this.config = config;
52      }
53  
54      public Object getConfiguration() {
55          return config;
56      }
57  
58      public void setHttpHeaders(Properties httpHeaders) {
59          headers = httpHeaders;
60      }
61  
62      @Override
63      protected void openConnectionInternal() throws ConnectionException, AuthenticationException {
64          fs = MemWagonUtils.openConnection(
65                  this,
66                  getAuthenticationInfo(),
67                  getProxyInfo("mem", getRepository().getHost()),
68                  headers);
69      }
70  
71      @Override
72      public void closeConnection() throws ConnectionException {
73          fs = null;
74      }
75  
76      private String getData(String resource) {
77          return fs.get(URI.create(resource).getSchemeSpecificPart());
78      }
79  
80      @Override
81      public boolean resourceExists(String resourceName) throws TransferFailedException, AuthorizationException {
82          String data = getData(resourceName);
83          return data != null;
84      }
85  
86      @Override
87      public void fillInputData(InputData inputData)
88              throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
89          String data = getData(inputData.getResource().getName());
90          if (data == null) {
91              throw new ResourceDoesNotExistException(
92                      "Missing resource: " + inputData.getResource().getName());
93          }
94          byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
95          inputData.getResource().setContentLength(bytes.length);
96          inputData.setInputStream(new ByteArrayInputStream(bytes));
97      }
98  
99      @Override
100     public void fillOutputData(OutputData outputData) throws TransferFailedException {
101         outputData.setOutputStream(new ByteArrayOutputStream());
102     }
103 
104     @Override
105     protected void finishPutTransfer(Resource resource, InputStream input, OutputStream output)
106             throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException {
107         String data = new String(((ByteArrayOutputStream) output).toByteArray(), StandardCharsets.UTF_8);
108         fs.put(URI.create(resource.getName()).getSchemeSpecificPart(), data);
109     }
110 }