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