1   package org.apache.maven.artifact.manager;
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.File;
23  import java.io.InputStream;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.maven.wagon.ConnectionException;
28  import org.apache.maven.wagon.InputData;
29  import org.apache.maven.wagon.OutputData;
30  import org.apache.maven.wagon.ResourceDoesNotExistException;
31  import org.apache.maven.wagon.StreamWagon;
32  import org.apache.maven.wagon.TransferFailedException;
33  import org.apache.maven.wagon.authentication.AuthenticationException;
34  import org.apache.maven.wagon.authorization.AuthorizationException;
35  import org.apache.maven.wagon.resource.Resource;
36  import org.codehaus.plexus.util.IOUtil;
37  import org.codehaus.plexus.util.StringInputStream;
38  import org.codehaus.plexus.util.StringOutputStream;
39  
40  public class StringWagon
41      extends StreamWagon
42  {
43      private Map expectedContent = new HashMap();
44      
45      public void addExpectedContent( String resourceName, String expectedContent )
46      {
47          this.expectedContent.put( resourceName, expectedContent );
48      }
49  
50      public String[] getSupportedProtocols()
51      {
52          return new String[] { "string" };
53      }
54  
55      public void closeConnection()
56          throws ConnectionException
57      {
58      }
59  
60      public void fillInputData( InputData inputData )
61          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
62      {
63          Resource resource = inputData.getResource();
64  
65          String content = (String) expectedContent.get( resource.getName() );
66          
67          if ( content != null )
68          {
69              resource.setContentLength( content.length() );
70              resource.setLastModified( System.currentTimeMillis() );
71  
72              inputData.setInputStream( new StringInputStream( content ) );
73          }
74          else
75          {
76              throw new ResourceDoesNotExistException( "No content provided for " + resource.getName() );
77          }
78      }
79  
80      public void fillOutputData( OutputData outputData )
81          throws TransferFailedException
82      {
83          outputData.setOutputStream( new StringOutputStream() );
84      }
85  
86      protected void openConnectionInternal()
87          throws ConnectionException, AuthenticationException
88      {
89      }
90  
91      public void clearExpectedContent()
92      {
93          expectedContent.clear();        
94      }
95  }