continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From och...@apache.org
Subject svn commit: r1141484 - in /continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client: pom.xml src/main/java/org/apache/continuum/buildagent/webdav/client/WorkingCopyWebdavClient.java
Date Thu, 30 Jun 2011 11:29:30 GMT
Author: oching
Date: Thu Jun 30 11:29:30 2011
New Revision: 1141484

URL: http://svn.apache.org/viewvc?rev=1141484&view=rev
Log:
added sample when getting/listing resources using jackrabbit's webdav client

Modified:
    continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client/pom.xml
    continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client/src/main/java/org/apache/continuum/buildagent/webdav/client/WorkingCopyWebdavClient.java

Modified: continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client/pom.xml
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client/pom.xml?rev=1141484&r1=1141483&r2=1141484&view=diff
==============================================================================
--- continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client/pom.xml (original)
+++ continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client/pom.xml Thu Jun
30 11:29:30 2011
@@ -30,6 +30,11 @@
       <version>4.1.1</version>
     </dependency>
     <dependency>
+      <groupId>org.apache.jackrabbit</groupId>
+      <artifactId>jackrabbit-webdav</artifactId>
+      <version>2.2.7</version>
+    </dependency>
+    <dependency>
       <groupId>commons-logging</groupId>
       <artifactId>commons-logging</artifactId>
       <version>1.1.1</version>

Modified: continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client/src/main/java/org/apache/continuum/buildagent/webdav/client/WorkingCopyWebdavClient.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client/src/main/java/org/apache/continuum/buildagent/webdav/client/WorkingCopyWebdavClient.java?rev=1141484&r1=1141483&r2=1141484&view=diff
==============================================================================
--- continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client/src/main/java/org/apache/continuum/buildagent/webdav/client/WorkingCopyWebdavClient.java
(original)
+++ continuum/trunk/continuum-buildagent/continuum-buildagent-webdav-client/src/main/java/org/apache/continuum/buildagent/webdav/client/WorkingCopyWebdavClient.java
Thu Jun 30 11:29:30 2011
@@ -1,6 +1,34 @@
 package org.apache.continuum.buildagent.webdav.client;
 
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.HostConfiguration;
+import org.apache.commons.httpclient.HttpConnectionManager;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpVersion;
@@ -24,9 +52,18 @@ import org.apache.http.params.HttpParams
 import org.apache.http.params.HttpProtocolParams;
 import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.util.EntityUtils;
+import org.apache.jackrabbit.webdav.DavConstants;
+import org.apache.jackrabbit.webdav.DavException;
+import org.apache.jackrabbit.webdav.MultiStatus;
+import org.apache.jackrabbit.webdav.MultiStatusResponse;
+import org.apache.jackrabbit.webdav.client.methods.DavMethod;
+import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
+import org.apache.jackrabbit.webdav.property.DavProperty;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 
 public class WorkingCopyWebdavClient
@@ -36,8 +73,17 @@ public class WorkingCopyWebdavClient
     {
         System.out.println( "Running webdav client.." );
 
+        // get resource
+        getResourceUsingHttpclient( args[0], "", args[1] );
+
+        // list resources
+        getResourcesUsingJackrabbit( args[0], "", args[1] );
+    }
+
+    private static void getResourceUsingHttpclient( String resourceUrl, String username,
String password )
+        throws URISyntaxException, IOException
+    {
         SchemeRegistry schemeRegistry = new SchemeRegistry();
-        // http scheme
         schemeRegistry.register( new Scheme( "http", PlainSocketFactory.getSocketFactory(),
80 ) );
 
         HttpParams params = new BasicHttpParams();
@@ -50,12 +96,12 @@ public class WorkingCopyWebdavClient
 
         DefaultHttpClient httpClient = new DefaultHttpClient( cm, params );
 
-        URL url = new URL( args[0] );
+        URL url = new URL( resourceUrl );
         URI uri = url.toURI();
         HttpGet httpGet = new HttpGet( uri );
 
         httpClient.getCredentialsProvider().setCredentials( new AuthScope( uri.getHost(),
uri.getPort() ),
-                                                                            new UsernamePasswordCredentials(
"", args[1] ) );
+                                                new UsernamePasswordCredentials( username,
password ) );
 
         HttpHost targetHost = new HttpHost( url.getHost(), url.getPort(), url.getProtocol()
);
 
@@ -66,6 +112,8 @@ public class WorkingCopyWebdavClient
         BasicHttpContext localcontext = new BasicHttpContext();
         localcontext.setAttribute( ClientContext.AUTH_CACHE, authCache );
 
+        System.out.println( "Retrieving resource '" + url.toString() + "' using HttpClient's
get method.." );
+
         HttpResponse httpResponse = httpClient.execute( targetHost, httpGet, localcontext
);
 
         System.out.println( "Response status code :: " + httpResponse.getStatusLine().getStatusCode()
);
@@ -77,4 +125,97 @@ public class WorkingCopyWebdavClient
         System.out.println( "Content :: " + content );
     }
 
+    private static void getResourcesUsingJackrabbit( String filePath, String username, String
password )
+        throws IOException, URISyntaxException, DavException
+    {
+        int idx = filePath.lastIndexOf( "/" );
+        if( idx != -1 )
+        {
+            filePath = StringUtils.substring( filePath, 0, idx + 1 );
+        }
+
+        System.out.println( "\nRetrieve resources from '" + filePath + "' using Jackrabbit's
webdav client.." );
+
+        URL url = new URL( filePath );
+        URI uri = url.toURI();
+
+        DavMethod pFind = new PropFindMethod( uri.toString(), DavConstants.PROPFIND_ALL_PROP,
DavConstants.DEPTH_1 );
+
+        executeMethod( username, password, uri, pFind );
+
+        MultiStatus multiStatus = pFind.getResponseBodyAsMultiStatus();
+        MultiStatusResponse[] responses = multiStatus.getResponses();
+        MultiStatusResponse currResponse;
+        System.out.println("Folders and files in " + filePath + ":");
+
+        for( int i = 0; i < responses.length; i++ )
+        {
+            currResponse = responses[i];
+            if ( !( currResponse.getHref().equals( uri.toString() ) || currResponse.getHref().equals(
uri.toString() + "/") ) )
+            {
+                String currResponseHref = StringUtils.trim( currResponse.getHref() );
+
+                System.out.println( "\nResource url :: " + currResponseHref );
+
+                DavProperty displayNameDavProperty = currResponse.getProperties( HttpStatus.SC_OK
).get( "displayname" );
+                String displayName;
+                if( displayNameDavProperty != null )
+                {
+                    displayName = (String) displayNameDavProperty.getValue();
+                }
+                else
+                {
+                    displayName = StringUtils.substring( currResponseHref, currResponseHref.lastIndexOf(
"/" ) );
+                }
+
+                HttpMethod httpGet = new GetMethod( currResponseHref );
+
+                URL resourceUrl = new URL( currResponseHref );
+
+                executeMethod( username, password, resourceUrl.toURI(), httpGet );
+
+                System.out.println( "Returned status code :: " + httpGet.getStatusCode()
);
+
+                if( httpGet.getStatusCode() == HttpStatus.SC_OK )
+                {
+                    InputStream is = httpGet.getResponseBodyAsStream();
+
+                    try
+                    {
+                        System.out.println( "Contents of file '" + displayName + "' :: "
+ IOUtils.toString( is ) );
+                    }
+                    finally
+                    {
+                        IOUtils.closeQuietly( is );
+                    }
+                }
+            }
+        }
+    }
+
+    private static void executeMethod( String username, String password, URI uri, HttpMethod
pFind )
+        throws IOException
+    {
+        HostConfiguration hostConfig = new HostConfiguration();
+        hostConfig.setHost( uri.getHost() );
+
+        int maxHostConnections = 20;
+
+        HttpConnectionManagerParams params = new HttpConnectionManagerParams();
+        params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
+
+        HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
+        connectionManager.setParams(params);
+
+        org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient(connectionManager);
+
+        Credentials creds = new org.apache.commons.httpclient.UsernamePasswordCredentials(
username, password );
+
+        client.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY,
creds);
+        client.setHostConfiguration(hostConfig);
+        client.getParams().setAuthenticationPreemptive( true );
+
+        client.executeMethod( hostConfig, pFind );
+    }
+
 }



Mime
View raw message