1   package org.apache.maven.repository.legacy;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.UnsupportedEncodingException;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.maven.wagon.ConnectionException;
29  import org.apache.maven.wagon.InputData;
30  import org.apache.maven.wagon.OutputData;
31  import org.apache.maven.wagon.ResourceDoesNotExistException;
32  import org.apache.maven.wagon.StreamWagon;
33  import org.apache.maven.wagon.TransferFailedException;
34  import org.apache.maven.wagon.Wagon;
35  import org.apache.maven.wagon.authentication.AuthenticationException;
36  import org.apache.maven.wagon.authorization.AuthorizationException;
37  import org.apache.maven.wagon.resource.Resource;
38  import org.codehaus.plexus.component.annotations.Component;
39  
40  @Component(role=Wagon.class,hint="string")
41  public class StringWagon
42      extends StreamWagon
43  {
44      private Map<String, String> expectedContent = new HashMap<String, String>();
45      
46      public void addExpectedContent( String resourceName, String expectedContent )
47      {
48          this.expectedContent.put( resourceName, expectedContent );
49      }
50  
51      public String[] getSupportedProtocols()
52      {
53          return new String[] { "string" };
54      }
55  
56      @Override
57      public void closeConnection()
58          throws ConnectionException
59      {
60      }
61  
62      @Override
63      public void fillInputData( InputData inputData )
64          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
65      {
66          Resource resource = inputData.getResource();
67  
68          String content = expectedContent.get( resource.getName() );
69          
70          if ( content != null )
71          {
72              resource.setContentLength( content.length() );
73              resource.setLastModified( System.currentTimeMillis() );
74  
75              try
76              {
77                  inputData.setInputStream( new ByteArrayInputStream( content.getBytes( "UTF-8" ) ) );
78              }
79              catch ( UnsupportedEncodingException e )
80              {
81                  throw new Error( "broken JVM", e );
82              }
83          }
84          else
85          {
86              throw new ResourceDoesNotExistException( "No content provided for " + resource.getName() );
87          }
88      }
89  
90      @Override
91      public void fillOutputData( OutputData outputData )
92          throws TransferFailedException
93      {
94          outputData.setOutputStream( new ByteArrayOutputStream() );
95      }
96  
97      @Override
98      protected void openConnectionInternal()
99          throws ConnectionException, AuthenticationException
100     {
101     }
102 
103     public void clearExpectedContent()
104     {
105         expectedContent.clear();        
106     }
107 }