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.io.UnsupportedEncodingException;
27 import java.net.URI;
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;
108 try
109 {
110 bytes = data.getBytes( "UTF-8" );
111 }
112 catch ( UnsupportedEncodingException e )
113 {
114 throw new TransferFailedException( e.getMessage(), e );
115 }
116 inputData.getResource().setContentLength( bytes.length );
117 inputData.setInputStream( new ByteArrayInputStream( bytes ) );
118 }
119
120 @Override
121 public void fillOutputData( OutputData outputData )
122 throws TransferFailedException
123 {
124 outputData.setOutputStream( new ByteArrayOutputStream() );
125 }
126
127 @Override
128 protected void finishPutTransfer( Resource resource, InputStream input, OutputStream output )
129 throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException
130 {
131 String data;
132 try
133 {
134 data = ( (ByteArrayOutputStream) output ).toString( "UTF-8" );
135 }
136 catch ( UnsupportedEncodingException e )
137 {
138 throw new TransferFailedException( e.getMessage(), e );
139 }
140 fs.put( URI.create( resource.getName() ).getSchemeSpecificPart(), data );
141 }
142
143 }