1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.legacy;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.nio.charset.StandardCharsets;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.maven.wagon.ConnectionException;
28 import org.apache.maven.wagon.InputData;
29 import org.apache.maven.wagon.OutputData;
30 import org.apache.maven.wagon.ResourceDoesNotExistException;
31 import org.apache.maven.wagon.StreamWagon;
32 import org.apache.maven.wagon.TransferFailedException;
33 import org.apache.maven.wagon.Wagon;
34 import org.apache.maven.wagon.authentication.AuthenticationException;
35 import org.apache.maven.wagon.authorization.AuthorizationException;
36 import org.apache.maven.wagon.resource.Resource;
37 import org.codehaus.plexus.component.annotations.Component;
38
39 @Component(role = Wagon.class, hint = "string")
40 public class StringWagon extends StreamWagon {
41 private Map<String, String> expectedContent = new HashMap<>();
42
43 public void addExpectedContent(String resourceName, String expectedContent) {
44 this.expectedContent.put(resourceName, expectedContent);
45 }
46
47 public String[] getSupportedProtocols() {
48 return new String[] {"string"};
49 }
50
51 @Override
52 public void closeConnection() throws ConnectionException {}
53
54 @Override
55 public void fillInputData(InputData inputData)
56 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
57 Resource resource = inputData.getResource();
58
59 String content = expectedContent.get(resource.getName());
60
61 if (content != null) {
62 resource.setContentLength(content.length());
63 resource.setLastModified(System.currentTimeMillis());
64
65 inputData.setInputStream(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
66 } else {
67 throw new ResourceDoesNotExistException("No content provided for " + resource.getName());
68 }
69 }
70
71 @Override
72 public void fillOutputData(OutputData outputData) throws TransferFailedException {
73 outputData.setOutputStream(new ByteArrayOutputStream());
74 }
75
76 @Override
77 protected void openConnectionInternal() throws ConnectionException, AuthenticationException {}
78
79 public void clearExpectedContent() {
80 expectedContent.clear();
81 }
82 }