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