View Javadoc

1   package org.apache.continuum.webdav;
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 org.apache.continuum.buildagent.configuration.BuildAgentConfigurationService;
23  import org.apache.continuum.webdav.util.WebdavMethodUtil;
24  import org.apache.continuum.webdav.util.WorkingCopyPathUtil;
25  import org.apache.jackrabbit.webdav.DavException;
26  import org.apache.jackrabbit.webdav.DavResource;
27  import org.apache.jackrabbit.webdav.DavResourceFactory;
28  import org.apache.jackrabbit.webdav.DavResourceLocator;
29  import org.apache.jackrabbit.webdav.DavServletRequest;
30  import org.apache.jackrabbit.webdav.DavServletResponse;
31  import org.apache.jackrabbit.webdav.DavSession;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import java.io.File;
36  import javax.activation.MimetypesFileTypeMap;
37  import javax.servlet.http.HttpServletResponse;
38  
39  /**
40   * @plexus.component role="org.apache.continuum.webdav.ContinuumBuildAgentDavResourceFactory"
41   */
42  public class ContinuumBuildAgentDavResourceFactory
43      implements DavResourceFactory
44  {
45      private static final Logger log = LoggerFactory.getLogger( ContinuumBuildAgentDavResourceFactory.class );
46  
47      private static final MimetypesFileTypeMap mimeTypes;
48  
49      /**
50       * @plexus.requirement
51       */
52      private BuildAgentConfigurationService buildAgentConfigurationService;
53  
54      static
55      {
56          mimeTypes = new MimetypesFileTypeMap();
57          mimeTypes.addMimeTypes( "application/java-archive jar war ear" );
58          mimeTypes.addMimeTypes( "application/java-class class" );
59          mimeTypes.addMimeTypes( "image/png png" );
60      }
61  
62      public DavResource createResource( final DavResourceLocator locator, final DavSession davSession )
63          throws DavException
64      {
65          ContinuumBuildAgentDavResourceLocator continuumLocator = checkLocatorIsInstanceOfContinuumBuildAgentLocator(
66              locator );
67  
68          String logicalResource = WorkingCopyPathUtil.getLogicalResource( locator.getResourcePath() );
69          if ( logicalResource.startsWith( "/" ) )
70          {
71              logicalResource = logicalResource.substring( 1 );
72          }
73  
74          File resourceFile = getResourceFile( continuumLocator.getProjectId(), logicalResource );
75  
76          if ( !resourceFile.exists() || ( continuumLocator.getHref( false ).endsWith( "/" ) &&
77              !resourceFile.isDirectory() ) )
78          {
79              // force a resource not found
80              log.error( "Resource file '" + resourceFile.getAbsolutePath() + "' does not exist" );
81              throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource does not exist" );
82          }
83          else
84          {
85              return createResource( resourceFile, logicalResource, davSession, continuumLocator );
86          }
87      }
88  
89      public DavResource createResource( DavResourceLocator locator, DavServletRequest request,
90                                         DavServletResponse response )
91          throws DavException
92      {
93          ContinuumBuildAgentDavResourceLocator continuumLocator = checkLocatorIsInstanceOfContinuumBuildAgentLocator(
94              locator );
95  
96          if ( !WebdavMethodUtil.isReadMethod( request.getMethod() ) )
97          {
98              throw new DavException( HttpServletResponse.SC_METHOD_NOT_ALLOWED,
99                                      "Write method not allowed in working copy" );
100         }
101 
102         String logicalResource = WorkingCopyPathUtil.getLogicalResource( continuumLocator.getResourcePath() );
103         if ( logicalResource.startsWith( "/" ) )
104         {
105             logicalResource = logicalResource.substring( 1 );
106         }
107 
108         File resourceFile = getResourceFile( continuumLocator.getProjectId(), logicalResource );
109 
110         if ( !resourceFile.exists() || ( continuumLocator.getHref( false ).endsWith( "/" ) &&
111             !resourceFile.isDirectory() ) )
112         {
113             // force a resource not found
114             log.error( "Resource file '" + resourceFile.getAbsolutePath() + "' does not exist" );
115             throw new DavException( HttpServletResponse.SC_NOT_FOUND, "Resource does not exist" );
116         }
117         else
118         {
119             return createResource( resourceFile, logicalResource, request.getDavSession(), continuumLocator );
120         }
121     }
122 
123     public BuildAgentConfigurationService getBuildAgentConfigurationService()
124     {
125         return buildAgentConfigurationService;
126     }
127 
128     public MimetypesFileTypeMap getMimeTypes()
129     {
130         return mimeTypes;
131     }
132 
133     public void setBuildAgentConfigurationService( BuildAgentConfigurationService buildAgentConfigurationService )
134     {
135         this.buildAgentConfigurationService = buildAgentConfigurationService;
136     }
137 
138     private ContinuumBuildAgentDavResourceLocator checkLocatorIsInstanceOfContinuumBuildAgentLocator(
139         DavResourceLocator locator )
140         throws DavException
141     {
142         if ( !( locator instanceof ContinuumBuildAgentDavResourceLocator ) )
143         {
144             throw new DavException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
145                                     "Locator does not implement ContinuumBuildAgentLocator" );
146         }
147 
148         // Hidden paths
149         if ( locator.getResourcePath().startsWith( ContinuumBuildAgentDavResource.HIDDEN_PATH_PREFIX ) )
150         {
151             throw new DavException( HttpServletResponse.SC_NOT_FOUND );
152         }
153 
154         ContinuumBuildAgentDavResourceLocator continuumLocator = (ContinuumBuildAgentDavResourceLocator) locator;
155         if ( continuumLocator.getProjectId() <= 0 )
156         {
157             log.error( "Invalid project id: " + continuumLocator.getProjectId() );
158             throw new DavException( HttpServletResponse.SC_NO_CONTENT );
159         }
160 
161         return continuumLocator;
162     }
163 
164     protected File getResourceFile( int projectId, String logicalResource )
165     {
166         File workingDir = buildAgentConfigurationService.getWorkingDirectory( projectId );
167 
168         return new File( workingDir, logicalResource );
169     }
170 
171     protected DavResource createResource( File resourceFile, String logicalResource, DavSession session,
172                                           ContinuumBuildAgentDavResourceLocator locator )
173     {
174         return new ContinuumBuildAgentDavResource( resourceFile.getAbsolutePath(), logicalResource, session, locator,
175                                                    this, mimeTypes );
176     }
177 }