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.File;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.net.URI;
28 import java.nio.charset.StandardCharsets;
29 import java.util.Map;
30 import java.util.Properties;
31
32 import org.apache.maven.wagon.AbstractWagon;
33 import org.apache.maven.wagon.ConnectionException;
34 import org.apache.maven.wagon.InputData;
35 import org.apache.maven.wagon.OutputData;
36 import org.apache.maven.wagon.ResourceDoesNotExistException;
37 import org.apache.maven.wagon.TransferFailedException;
38 import org.apache.maven.wagon.authentication.AuthenticationException;
39 import org.apache.maven.wagon.authorization.AuthorizationException;
40 import org.apache.maven.wagon.events.TransferEvent;
41 import org.apache.maven.wagon.resource.Resource;
42
43
44
45 public class MemWagon
46 extends AbstractWagon
47 implements Configurable
48 {
49
50 private Map<String, String> fs;
51
52 private Properties headers;
53
54 private Object config;
55
56 public void setConfiguration( Object config )
57 {
58 this.config = config;
59 }
60
61 public Object getConfiguration()
62 {
63 return config;
64 }
65
66 public void setHttpHeaders( Properties httpHeaders )
67 {
68 headers = httpHeaders;
69 }
70
71 @Override
72 protected void openConnectionInternal()
73 throws ConnectionException, AuthenticationException
74 {
75 fs =
76 MemWagonUtils.openConnection( this, getAuthenticationInfo(),
77 getProxyInfo( "mem", getRepository().getHost() ), headers );
78 }
79
80 @Override
81 protected void closeConnection()
82 throws ConnectionException
83 {
84 fs = null;
85 }
86
87 private String getData( String resource )
88 {
89 return fs.get( URI.create( resource ).getSchemeSpecificPart() );
90 }
91
92 @Override
93 public boolean resourceExists( String resourceName )
94 throws TransferFailedException, AuthorizationException
95 {
96 String data = getData( resourceName );
97 return data != null;
98 }
99
100 public void get( String resourceName, File destination )
101 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
102 {
103 getIfNewer( resourceName, destination, 0 );
104 }
105
106 public boolean getIfNewer( String resourceName, File destination, long timestamp )
107 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
108 {
109 Resource resource = new Resource( resourceName );
110 fireGetInitiated( resource, destination );
111 resource.setLastModified( timestamp );
112 getTransfer( resource, destination, getInputStream( resource ) );
113 return true;
114 }
115
116 protected InputStream getInputStream( Resource resource )
117 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
118 {
119 InputData inputData = new InputData();
120 inputData.setResource( resource );
121 try
122 {
123 fillInputData( inputData );
124 }
125 catch ( TransferFailedException e )
126 {
127 fireTransferError( resource, e, TransferEvent.REQUEST_GET );
128 cleanupGetTransfer( resource );
129 throw e;
130 }
131 catch ( ResourceDoesNotExistException e )
132 {
133 fireTransferError( resource, e, TransferEvent.REQUEST_GET );
134 cleanupGetTransfer( resource );
135 throw e;
136 }
137 catch ( AuthorizationException e )
138 {
139 fireTransferError( resource, e, TransferEvent.REQUEST_GET );
140 cleanupGetTransfer( resource );
141 throw e;
142 }
143 finally
144 {
145 if ( inputData.getInputStream() == null )
146 {
147 cleanupGetTransfer( resource );
148 }
149 }
150 return inputData.getInputStream();
151 }
152
153 protected void fillInputData( InputData inputData )
154 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
155 {
156 String data = getData( inputData.getResource().getName() );
157 if ( data == null )
158 {
159 throw new ResourceDoesNotExistException( "Missing resource: " + inputData.getResource().getName() );
160 }
161 byte[] bytes = data.getBytes( StandardCharsets.UTF_8 );
162 inputData.getResource().setContentLength( bytes.length );
163 inputData.setInputStream( new ByteArrayInputStream( bytes ) );
164 }
165
166 public void put( File source, String resourceName )
167 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
168 {
169 Resource resource = new Resource( resourceName );
170 firePutInitiated( resource, source );
171 resource.setContentLength( source.length() );
172 resource.setLastModified( source.lastModified() );
173 OutputStream os = getOutputStream( resource );
174 putTransfer( resource, source, os, true );
175 }
176
177 protected OutputStream getOutputStream( Resource resource )
178 throws TransferFailedException
179 {
180 OutputData outputData = new OutputData();
181 outputData.setResource( resource );
182 try
183 {
184 fillOutputData( outputData );
185 }
186 catch ( TransferFailedException e )
187 {
188 fireTransferError( resource, e, TransferEvent.REQUEST_PUT );
189 throw e;
190 }
191 finally
192 {
193 if ( outputData.getOutputStream() == null )
194 {
195 cleanupPutTransfer( resource );
196 }
197 }
198
199 return outputData.getOutputStream();
200 }
201
202 protected void fillOutputData( OutputData outputData )
203 throws TransferFailedException
204 {
205 outputData.setOutputStream( new ByteArrayOutputStream() );
206 }
207
208 @Override
209 protected void finishPutTransfer( Resource resource, InputStream input, OutputStream output )
210 throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException
211 {
212 String data = new String( ( (ByteArrayOutputStream) output ).toByteArray(), StandardCharsets.UTF_8 );
213 fs.put( URI.create( resource.getName() ).getSchemeSpecificPart(), data );
214 }
215
216 }