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