001package org.apache.maven.repository.legacy;
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.UnsupportedEncodingException;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.apache.maven.wagon.ConnectionException;
029import org.apache.maven.wagon.InputData;
030import org.apache.maven.wagon.OutputData;
031import org.apache.maven.wagon.ResourceDoesNotExistException;
032import org.apache.maven.wagon.StreamWagon;
033import org.apache.maven.wagon.TransferFailedException;
034import org.apache.maven.wagon.Wagon;
035import org.apache.maven.wagon.authentication.AuthenticationException;
036import org.apache.maven.wagon.authorization.AuthorizationException;
037import org.apache.maven.wagon.resource.Resource;
038import org.codehaus.plexus.component.annotations.Component;
039
040@Component(role=Wagon.class,hint="string")
041public class StringWagon
042    extends StreamWagon
043{
044    private Map<String, String> expectedContent = new HashMap<String, String>();
045
046    public void addExpectedContent( String resourceName, String expectedContent )
047    {
048        this.expectedContent.put( resourceName, expectedContent );
049    }
050
051    public String[] getSupportedProtocols()
052    {
053        return new String[] { "string" };
054    }
055
056    @Override
057    public void closeConnection()
058        throws ConnectionException
059    {
060    }
061
062    @Override
063    public void fillInputData( InputData inputData )
064        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
065    {
066        Resource resource = inputData.getResource();
067
068        String content = expectedContent.get( resource.getName() );
069
070        if ( content != null )
071        {
072            resource.setContentLength( content.length() );
073            resource.setLastModified( System.currentTimeMillis() );
074
075            try
076            {
077                inputData.setInputStream( new ByteArrayInputStream( content.getBytes( "UTF-8" ) ) );
078            }
079            catch ( UnsupportedEncodingException e )
080            {
081                throw new Error( "broken JVM", e );
082            }
083        }
084        else
085        {
086            throw new ResourceDoesNotExistException( "No content provided for " + resource.getName() );
087        }
088    }
089
090    @Override
091    public void fillOutputData( OutputData outputData )
092        throws TransferFailedException
093    {
094        outputData.setOutputStream( new ByteArrayOutputStream() );
095    }
096
097    @Override
098    protected void openConnectionInternal()
099        throws ConnectionException, AuthenticationException
100    {
101    }
102
103    public void clearExpectedContent()
104    {
105        expectedContent.clear();
106    }
107}