1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }