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