View Javadoc
1   package org.eclipse.aether.transport.file;
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.nio.file.Files;
24  
25  import org.eclipse.aether.repository.RemoteRepository;
26  import org.eclipse.aether.spi.connector.transport.AbstractTransporter;
27  import org.eclipse.aether.spi.connector.transport.GetTask;
28  import org.eclipse.aether.spi.connector.transport.PeekTask;
29  import org.eclipse.aether.spi.connector.transport.PutTask;
30  import org.eclipse.aether.spi.connector.transport.TransportTask;
31  import org.eclipse.aether.transfer.NoTransporterException;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * A transporter using {@link java.io.File}.
37   */
38  final class FileTransporter
39      extends AbstractTransporter
40  {
41  
42      private static final Logger LOGGER = LoggerFactory.getLogger( FileTransporter.class );
43  
44      private final File basedir;
45  
46      FileTransporter( RemoteRepository repository )
47          throws NoTransporterException
48      {
49          if ( !"file".equalsIgnoreCase( repository.getProtocol() ) )
50          {
51              throw new NoTransporterException( repository );
52          }
53          basedir = new File( PathUtils.basedir( repository.getUrl() ) ).getAbsoluteFile();
54      }
55  
56      File getBasedir()
57      {
58          return basedir;
59      }
60  
61      public int classify( Throwable error )
62      {
63          if ( error instanceof ResourceNotFoundException )
64          {
65              return ERROR_NOT_FOUND;
66          }
67          return ERROR_OTHER;
68      }
69  
70      @Override
71      protected void implPeek( PeekTask task )
72          throws Exception
73      {
74          getFile( task, true );
75      }
76  
77      @Override
78      protected void implGet( GetTask task )
79          throws Exception
80      {
81          File file = getFile( task, true );
82          utilGet( task, Files.newInputStream( file.toPath() ), true, file.length(), false );
83      }
84  
85      @Override
86      protected void implPut( PutTask task )
87          throws Exception
88      {
89          File file = getFile( task, false );
90          file.getParentFile().mkdirs();
91          try
92          {
93              utilPut( task, Files.newOutputStream( file.toPath() ), true );
94          }
95          catch ( Exception e )
96          {
97              if ( !file.delete() && file.exists() )
98              {
99                  LOGGER.debug( "Could not delete partial file {}", file );
100             }
101             throw e;
102         }
103     }
104 
105     private File getFile( TransportTask task, boolean required )
106         throws Exception
107     {
108         String path = task.getLocation().getPath();
109         if ( path.contains( "../" ) )
110         {
111             throw new IllegalArgumentException( "illegal resource path: " + path );
112         }
113         File file = new File( basedir, path );
114         if ( required && !file.exists() )
115         {
116             throw new ResourceNotFoundException( "Could not locate " + file );
117         }
118         return file;
119     }
120 
121     @Override
122     protected void implClose()
123     {
124     }
125 
126 }