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