1 package org.eclipse.aether.transport.wagon;
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.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.ConnectionException;
32 import org.apache.maven.wagon.InputData;
33 import org.apache.maven.wagon.OutputData;
34 import org.apache.maven.wagon.ResourceDoesNotExistException;
35 import org.apache.maven.wagon.StreamWagon;
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.resource.Resource;
40
41
42
43 public class MemStreamWagon
44 extends StreamWagon
45 implements Configurable
46 {
47
48 private Map<String, String> fs;
49
50 private Properties headers;
51
52 private Object config;
53
54 public void setConfiguration( Object config )
55 {
56 this.config = config;
57 }
58
59 public Object getConfiguration()
60 {
61 return config;
62 }
63
64 public void setHttpHeaders( Properties httpHeaders )
65 {
66 headers = httpHeaders;
67 }
68
69 @Override
70 protected void openConnectionInternal()
71 throws ConnectionException, AuthenticationException
72 {
73 fs =
74 MemWagonUtils.openConnection( this, getAuthenticationInfo(),
75 getProxyInfo( "mem", getRepository().getHost() ), headers );
76 }
77
78 @Override
79 public void closeConnection()
80 throws ConnectionException
81 {
82 fs = null;
83 }
84
85 private String getData( String resource )
86 {
87 return fs.get( URI.create( resource ).getSchemeSpecificPart() );
88 }
89
90 @Override
91 public boolean resourceExists( String resourceName )
92 throws TransferFailedException, AuthorizationException
93 {
94 String data = getData( resourceName );
95 return data != null;
96 }
97
98 @Override
99 public void fillInputData( InputData inputData )
100 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
101 {
102 String data = getData( inputData.getResource().getName() );
103 if ( data == null )
104 {
105 throw new ResourceDoesNotExistException( "Missing resource: " + inputData.getResource().getName() );
106 }
107 byte[] bytes = data.getBytes( StandardCharsets.UTF_8 );
108 inputData.getResource().setContentLength( bytes.length );
109 inputData.setInputStream( new ByteArrayInputStream( bytes ) );
110 }
111
112 @Override
113 public void fillOutputData( OutputData outputData )
114 throws TransferFailedException
115 {
116 outputData.setOutputStream( new ByteArrayOutputStream() );
117 }
118
119 @Override
120 protected void finishPutTransfer( Resource resource, InputStream input, OutputStream output )
121 throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException
122 {
123 String data = new String( ( (ByteArrayOutputStream) output ).toByteArray(), StandardCharsets.UTF_8 );
124 fs.put( URI.create( resource.getName() ).getSchemeSpecificPart(), data );
125 }
126
127 }