Posts Tagged ‘tomcat

28
Feb
10

Hosting a WebDAV folder using Tomcat 6

This page describes the process setting up Apache Tomcat 6 to act as a WebDAV server. After you are done you will be able to mount this WebDAV resource from windows or any other WebDAV client and modify file directly on the server. The location will look like a WebFolder to a Windows machine.

Reasons for having your webapp host WebDAV mount point

  • Allows the your users to easly upload/download documents to/from your web application.
  • Allows users to view your web application as a “Share Drive”
  • Allows you to have a “back door” into your application to view Log files.
  • Tools like “Web Drive” or “Net Drive” allow you to assign a drive letter for your application.

Requirements

  • Maven installed and configured
  • Some web security knowledge- The implementation described below will allow anyone to modify files.

Procedure

Start by creating a project from a maven archetype.

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

Answer the rest of the questions like this…

Define value for groupId: : com.test
Define value for artifactId: : webdavTest
Define value for version:  1.0-SNAPSHOT: :
Define value for package:  com.test: :

Hit enter for the rest of the defaults.

cd to the project’s folder

Next we generate the eclipse project (optional). The project is still not ready however this step makes editing the pom.xml file easier.

mvn eclipse:clean eclipse:eclipse

Import the Existing project (generated project) into eclipse.

Next steps are to typically modify the pom.xml file and insert the rest of the dependencies and regenerate the eclipse project by re-typing the above command and clicking refresh on the eclipse project.

Your pom.xml file should look like this…

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>webdavTest</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>webdavTest Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
		<groupId>org.apache.tomcat</groupId>
		<artifactId>catalina</artifactId>
		<version>6.0.24</version>
		<scope>provided</scope>
    </dependency>

  </dependencies>
  <build>
    <finalName>webdavTest</finalName>

	<plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <configuration>
          <server>tomcat</server>
          <update>true</update>
          <url>http://localhost:8080/manager</url>
        </configuration>
      </plugin>
	</plugins>
  </build>
</project>

Your web.xml file should look like this..

src/main/webapp/WEB-INF/web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Archetype Created Web Application</display-name>

	<servlet>
		<servlet-name>webdav</servlet-name>
		<servlet-class>org.apache.catalina.servlets.WebdavServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>1</param-value>
		</init-param>
		<init-param>
			<param-name>listings</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>readonly</param-name>
			<param-value>false</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>webdav</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

Test the WebDAV location

Start the tomcat server by typing

mvn tomcat:run

Navigate to the following location using your web browser to test:

http://localhost:8080/webdavTest/

if that worked then navigate using your WEBDAV client to the same location. I used windows XP and mapped a network drive. It worked fine.

if you are using linux ubuntu you can type dav://localhost:8080/webdavTest/

TODO Items

Still trying to figure out how to get Jetty server to host this site. I have tried however got an error

javax.naming.NameNotFoundException; remaining name 'Resources'
	at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:576)
	at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:663)
	at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:678)
	at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:110)

The problem was related to the following section of code: Line 262 of DefaultServlet.java

  262           // Load the proxy dir context.
  263           resources = (ProxyDirContext) getServletContext()
  264               .getAttribute(Globals.RESOURCES_ATTR);
  265           if (resources == null) {
  266               try {
  267                   resources =
  268                       (ProxyDirContext) new InitialContext()
  269                       .lookup(RESOURCES_JNDI_NAME);
  270               } catch (NamingException e) {
  271                   // Failed
  272                   throw new ServletException("No resources", e);
  273               }
  274           }
  275
  276           if (resources == null) {
  277               throw new UnavailableException("No resources");
  278           }

I think it has something to do with the difference between the Jndi Implementation available in Jetty vs the one in Tomcat 6. Sorry for the folks that are in love with jetty but for now this only works in Tomcat 6. Not sure about Websphere or other Application servers… Soon as I try I will come back to update this page.

References

03
Sep
09

How to get Apache Tomcat 6 to log to the console

This site is a collaborative effort! The complete text and sourcecode for this is available on GitHub. Corrections and enhancements are welcome, please make the change and submit a pull request in the comment area below.

s article explains how to get apache tomcat to log to the console (system.out).

The process is quite simple. $CATALINA_HOME represents where you have tomcat version 6 installed.

Step 1

Download Apache log4j and put it into $CATALINA_HOME/lib folder.

Step 2

Download the jar files listed below

  • tomcat-juli-adapters.jar
  • tomcat-juli.jar

You may download them from here (fool proof method):

http://tomcat.apache.org/
Click on version 6 then click on Quick Navigation -> browse -> extras

Not sure if the following link will work years from now: http://www.poolsaboveground.com/apache/tomcat/tomcat-6/v6.0.20/bin/extras/ but it shows you were the files were at the time this blog was being written.

Step 3

Put tomcat-juli.jar into your $CATALINA_HOME/bin folder and the other jar file goes into the $CATALINA_HOME/lib folder.

Step 4

Put the following log4j.properties file into the $CATALINA_HOME/lib folder.

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=INFO, A1

# A1 is set to be a ConsoleAppender.

# A1 uses PatternLayout.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-5p %c - %m%n

Finally

Restart the tomcat server and you should now start seeing the output to the console.

2009-09-07 10:03:45,208 WARN  org.apache.commons.digester.Digester - [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:helloWorld' did not find a matching property.
2009-09-07 10:03:45,211 INFO  org.apache.catalina.core.AprLifecycleListener - The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/lib/jvm/java-6-sun-1.6.0.14/jre/lib/amd64/server:/usr/lib/jvm/java-6-sun-1.6.0.14/jre/lib/amd64:/usr/lib/jvm/java-6-sun-1.6.0.14/jre/../lib/amd64:/usr/lib64/xulrunner-addons:/usr/java/packages/lib/amd64:/lib:/usr/lib
2009-09-07 10:03:45,231 INFO  org.apache.coyote.http11.Http11Protocol - Initializing Coyote HTTP/1.1 on http-8080
2009-09-07 10:03:45,232 INFO  org.apache.catalina.startup.Catalina - Initialization processed in 316 ms
2009-09-07 10:03:45,254 INFO  org.apache.catalina.core.StandardService - Starting service Catalina
2009-09-07 10:03:45,255 INFO  org.apache.catalina.core.StandardEngine - Starting Servlet Engine: Apache Tomcat/6.0.18
2009-09-07 10:03:45,654 INFO  org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/helloWorld] - Initializing Spring root WebApplicationContext
2009-09-07 10:03:45,768 INFO  org.apache.coyote.http11.Http11Protocol - Starting Coyote HTTP/1.1 on http-8080
2009-09-07 10:03:45,799 INFO  org.apache.jk.common.ChannelSocket - JK: ajp13 listening on /0.0.0.0:8009
2009-09-07 10:03:45,803 INFO  org.apache.jk.server.JkMain - Jk running ID=0 time=0/12  config=null
2009-09-07 10:03:45,804 INFO  org.apache.catalina.startup.Catalina - Server startup in 572 ms

The logging is totally customizable. You can change the where the log output goes to or the Severity of errors that get logged by modifying the log4j.properties file seen above. To learn more about Apache log4j please visit the following URL: http://logging.apache.org

Note that this does not replace the custom logging properties you have within your applications. You can still specify them by putting a log4j.properties inside your applications /WEB-INF/classes folder. This logger is only at the top level. It helps in debugging issues you may encounter with the tomcat server.

This site is a collaborative effort! The complete text and sourcecode for this is available on GitHub. Corrections and enhancements are welcome, please make the change and submit a pull request in the comment area below.



Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 75 other followers

April 2017
S M T W T F S
« Mar    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Blog Stats

  • 806,836 hits