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.plugins.assembly.io;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.net.URL;
24  import java.nio.file.Files;
25  
26  import org.apache.commons.io.IOUtils;
27  
28  /**
29   * The URL Location, storing the URL content to a temporary local file.
30   *
31   */
32  class URLLocation extends FileLocation {
33  
34      private final URL url;
35  
36      private final String tempFilePrefix;
37  
38      private final String tempFileSuffix;
39  
40      private final boolean tempFileDeleteOnExit;
41  
42      /**
43       * @param url the URL
44       * @param specification the spec
45       * @param tempFilePrefix the prefix
46       * @param tempFileSuffix the suffix
47       * @param tempFileDeleteOnExit delete on exit
48       */
49      URLLocation(
50              URL url, String specification, String tempFilePrefix, String tempFileSuffix, boolean tempFileDeleteOnExit) {
51          super(specification);
52  
53          this.url = url;
54          this.tempFilePrefix = tempFilePrefix;
55          this.tempFileSuffix = tempFileSuffix;
56          this.tempFileDeleteOnExit = tempFileDeleteOnExit;
57      }
58  
59      @Override
60      protected void initFile() throws IOException {
61          if (unsafeGetFile() == null) {
62              File tempFile = Files.createTempFile(tempFilePrefix, tempFileSuffix).toFile();
63  
64              if (tempFileDeleteOnExit) {
65                  tempFile.deleteOnExit();
66              }
67  
68              IOUtils.copy(url, tempFile);
69  
70              setFile(tempFile);
71          }
72      }
73  }