001package org.eclipse.aether.transport.wagon;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.ByteArrayInputStream;
023import java.io.ByteArrayOutputStream;
024import java.io.File;
025import java.io.InputStream;
026import java.io.OutputStream;
027import java.io.UnsupportedEncodingException;
028import java.net.URI;
029import java.util.Map;
030import java.util.Properties;
031
032import org.apache.maven.wagon.AbstractWagon;
033import org.apache.maven.wagon.ConnectionException;
034import org.apache.maven.wagon.InputData;
035import org.apache.maven.wagon.OutputData;
036import org.apache.maven.wagon.ResourceDoesNotExistException;
037import org.apache.maven.wagon.TransferFailedException;
038import org.apache.maven.wagon.authentication.AuthenticationException;
039import org.apache.maven.wagon.authorization.AuthorizationException;
040import org.apache.maven.wagon.events.TransferEvent;
041import org.apache.maven.wagon.resource.Resource;
042
043/**
044 */
045public class MemWagon
046    extends AbstractWagon
047    implements Configurable
048{
049
050    private Map<String, String> fs;
051
052    private Properties headers;
053
054    private Object config;
055
056    public void setConfiguration( Object config )
057    {
058        this.config = config;
059    }
060
061    public Object getConfiguration()
062    {
063        return config;
064    }
065
066    public void setHttpHeaders( Properties httpHeaders )
067    {
068        headers = httpHeaders;
069    }
070
071    @Override
072    protected void openConnectionInternal()
073        throws ConnectionException, AuthenticationException
074    {
075        fs =
076            MemWagonUtils.openConnection( this, getAuthenticationInfo(),
077                                          getProxyInfo( "mem", getRepository().getHost() ), headers );
078    }
079
080    @Override
081    protected void closeConnection()
082        throws ConnectionException
083    {
084        fs = null;
085    }
086
087    private String getData( String resource )
088    {
089        return fs.get( URI.create( resource ).getSchemeSpecificPart() );
090    }
091
092    @Override
093    public boolean resourceExists( String resourceName )
094        throws TransferFailedException, AuthorizationException
095    {
096        String data = getData( resourceName );
097        return data != null;
098    }
099
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;
162        try
163        {
164            bytes = data.getBytes( "UTF-8" );
165        }
166        catch ( UnsupportedEncodingException e )
167        {
168            throw new TransferFailedException( e.getMessage(), e );
169        }
170        inputData.getResource().setContentLength( bytes.length );
171        inputData.setInputStream( new ByteArrayInputStream( bytes ) );
172    }
173
174    public void put( File source, String resourceName )
175        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
176    {
177        Resource resource = new Resource( resourceName );
178        firePutInitiated( resource, source );
179        resource.setContentLength( source.length() );
180        resource.setLastModified( source.lastModified() );
181        OutputStream os = getOutputStream( resource );
182        putTransfer( resource, source, os, true );
183    }
184
185    protected OutputStream getOutputStream( Resource resource )
186        throws TransferFailedException
187    {
188        OutputData outputData = new OutputData();
189        outputData.setResource( resource );
190        try
191        {
192            fillOutputData( outputData );
193        }
194        catch ( TransferFailedException e )
195        {
196            fireTransferError( resource, e, TransferEvent.REQUEST_PUT );
197            throw e;
198        }
199        finally
200        {
201            if ( outputData.getOutputStream() == null )
202            {
203                cleanupPutTransfer( resource );
204            }
205        }
206
207        return outputData.getOutputStream();
208    }
209
210    protected void fillOutputData( OutputData outputData )
211        throws TransferFailedException
212    {
213        outputData.setOutputStream( new ByteArrayOutputStream() );
214    }
215
216    @Override
217    protected void finishPutTransfer( Resource resource, InputStream input, OutputStream output )
218        throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException
219    {
220        String data;
221        try
222        {
223            data = ( (ByteArrayOutputStream) output ).toString( "UTF-8" );
224        }
225        catch ( UnsupportedEncodingException e )
226        {
227            throw new TransferFailedException( e.getMessage(), e );
228        }
229        fs.put( URI.create( resource.getName() ).getSchemeSpecificPart(), data );
230    }
231
232}