Entries tagged [java]

You're viewing entries tagged [java] in the weblog safari.

Configure a WebDAV folder using Tomcat 6

Tomcat 6 comes prebundled with a WebDAV servlet. To enable the servlet, add the following to web.xml

<servlet>
  <servlet-name>webdav</servlet-name>
  <servlet-class>org.apache.catalina.servlets.WebdavServlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>0</param-value>
  </init-param>
  <init-param>
    <param-name>listings</param-name>
    <param-value>true</param-value>
  </init-param>

  <!-- The following for read-write access -->
  <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>

 

To provide security for WebDAV using Tomcat realms, add the following to web.xml

<security-constraint>
        <web-resource-collection>
                <web-resource-name>webdavapp</web-resource-name>
                <url-pattern>/*</url-pattern>
                <http-method>PROPFIND</http-method>
                <http-method>PROPPATCH</http-method>
                <http-method>COPY</http-method>
                <http-method>MOVE</http-method>
                <http-method>LOCK</http-method>
                <http-method>UNLOCK</http-method>
        </web-resource-collection>

        <auth-constraint>
                <role-name>*</role-name>
        </auth-constraint>
</security-constraint>
<login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>webdavapp</realm-name>
</login-config>
<security-role>
        <description>WebDAV User</description>
        <role-name>webdav</role-name>
</security-role>

and add a new role and user to conf/tomcat-users.xml:

<role rolename="webdav"/>
<user username="webdavuser" password="password" roles="webdav"/>

TomcatMBeanViewer

Preliminary information

Tomcat 4 uses already the JMX-API. But there is no easy way to get a list of all available MBeans. JMX stands for Java Management extensions.

Tested Environment

  • Java 1.4
  • Tomcat 4.1

MBeanViewer web application

Insert this code in your servlet or JSP.

    MBeanServer mBeanServer = null;
    ArrayList arrayList = MBeanServerFactory.findMBeanServer(null);
    if (arrayList.size() > 0) {
        mBeanServer = (MBeanServer)arrayList.get(0);
    } else {
        mBeanServer = MBeanServerFactory.createMBeanServer();
    }

    Set allMBeans = mBeanServer.queryNames(null,null);
    for(Iterator i = allMBeans.iterator(); i.hasNext(); ) {
       ObjectName objectName = (ObjectName)i.next();

       log.debug(objectName.getDomain()); 
       log.debug(objectName.getCanonicalName());
    }

Results

Domain Canonical Name
Catalina Catalina:name=channelSocket,type=JkHandler
Catalina Catalina:host=localhost,name=users,path=/manager,resourcetype=Context,service=Tomcat-Standalone,type=ResourceLink
Catalina Catalina:resourcetype=Global,type=NamingResources
... ...

Log4jSessionFilter

Preliminary information

Log4J allows to attach information to the current thread. Log4J calls this NDC. NDC stands for "Nested Diagnostic Context".

Tested Environment

  • Java 1.4 /1.5
  • Log4j 1.2.x
  • Servlet API 2.3/2.4

Instructions

1. To include in every log message the current session-ID add a ServletFilter which attachs the session-ID to the NDC

public class Log4JFilter implements Filter {

    public void doFilter(
            ServletRequest request,
            ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {

        String contextInformation = "";

        if (request instanceof HttpServletRequest) {
            HttpServletRequest httpRequest = (HttpServletRequest)request;
            contextInformation += httpRequest.getSession().getId();
        }

        // push information
        NDC.push(contextInformation);

        try {
            chain.doFilter(request, response);
        } finally {
            // clean up NDC
            NDC.remove();
        }
    }
}

2. Enable the filter in web.xml

  <filter>
    <filter-name>Log4JFilter</filter-name>
    <display-name>Log4JFilter</display-name>
    <description>Intercepting Filter that provides Log4J context information</description>
    <filter-class>Log4JFilter </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>Log4JContextFilter</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>

3. Add the context information to the log4j-Pattern. Relevant to NDC is "%x".

   <param name="ConversionPattern" value="%d [%x] %-5p %c - %m"/>

Resulting logfile

2006-05-26 17:02:45,171 [R0rhSjqecO1gSGBDjj9omt] INFO com.Foo - bar() has been called 

Posted on by Peter Eichenauer
Tags: java log4j ndc