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 javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  import java.nio.charset.StandardCharsets;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import org.apache.maven.wagon.ConnectionException;
31  import org.apache.maven.wagon.InputData;
32  import org.apache.maven.wagon.OutputData;
33  import org.apache.maven.wagon.ResourceDoesNotExistException;
34  import org.apache.maven.wagon.StreamWagon;
35  import org.apache.maven.wagon.TransferFailedException;
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  
40  @Named("string")
41  @Singleton
42  public class StringWagon extends StreamWagon {
43      private Map<String, String> expectedContent = new HashMap<>();
44  
45      public void addExpectedContent(String resourceName, String expectedContent) {
46          this.expectedContent.put(resourceName, expectedContent);
47      }
48  
49      public String[] getSupportedProtocols() {
50          return new String[] {"string"};
51      }
52  
53      @Override
54      public void closeConnection() throws ConnectionException {}
55  
56      @Override
57      public void fillInputData(InputData inputData)
58              throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
59          Resource resource = inputData.getResource();
60  
61          String content = expectedContent.get(resource.getName());
62  
63          if (content != null) {
64              resource.setContentLength(content.length());
65              resource.setLastModified(System.currentTimeMillis());
66  
67              inputData.setInputStream(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
68          } else {
69              throw new ResourceDoesNotExistException("No content provided for " + resource.getName());
70          }
71      }
72  
73      @Override
74      public void fillOutputData(OutputData outputData) throws TransferFailedException {
75          outputData.setOutputStream(new ByteArrayOutputStream());
76      }
77  
78      @Override
79      protected void openConnectionInternal() throws ConnectionException, AuthenticationException {}
80  
81      public void clearExpectedContent() {
82          expectedContent.clear();
83      }
84  }