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