Wednesday, August 25, 2010

FileUpload Streaming

I use Apache Commons FileUpload to upload files. I was doing it in the "traditional" way, using DiskFileItemFactory. One requirement I have is that I need to convert the stream to DataHandler, because it will then be passed to call web services with MTOM. This works perfectly fine with DiskFileItermFactory. However, with DiskFileItemFactory, you still need to setup a temp folder to save the file and also FileCleaningTracker to clean up. So I decided to give the streaming API a try.

I had to create FileItemStreamDataSource to wrap FileItemSteam for the purpose of creating a DataHandler.
package com.myapp.fileupload;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataSource;

import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileItemStreamDataSource implements DataSource {
/**
* log.
*/
private Log log = LogFactory.getLog(getClass());

private FileItemStream stream;

public FileItemStreamDataSource(final FileItemStream stream) {
this.stream = stream;
}

@Override
public String getContentType() {
return stream.getContentType();
}

@Override
public InputStream getInputStream() throws IOException {
log.info("******** open stream **********");
return stream.openStream();
}

@Override
public String getName() {
return stream.getName();
}

@Override
public OutputStream getOutputStream() throws IOException {
throw new IOException();
}

}



There was no error in tests. However, when trying to open the file on the other end of web service, I found the content was either corrupted or empty (only 1KB). I wonder if this is because the stream was not read in full somehow. Maybe it is the same as the discussion here? Could it be that the stream is not kept open in web service calls? Anyway, I did not have enough time to get it work now and will need to revisit it later.

Friday, August 13, 2010

Enable Oracle SSO for an application

Assuming that SSO is enabled for OAS. (This requires a conf file generated from the SSO provided and installed in C:\product\10.1.3.1\OracleAS_1\Apache\Apache\conf\osso on your OAS)

After deploying your application (myapp), add the following to your "mod_osso.conf" file C:\product\10.1.3.1\OracleAS_1\Apache\Apache\conf\mod_osso.conf.

    <Location /myapp> 
require valid-user
AuthType Basic
</Location>

Wednesday, August 11, 2010

Spring AOP Interception

According to this post, most important - proxy-based AOP only works from "outside". Internal method calls are never intercepted.

This means that only the methods of your Spring beans can be intercepted. If this is not a problem, then here is an example.

Java
package com.my.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.ThrowsAdvice;

/**
* A Spring AOP advice to log service exception.
*/
@Aspect
public final class LoggingAdvice implements ThrowsAdvice {

/**
* Log.
*/
private Log log = LogFactory.getLog(this.getClass());

/**
* Log AxisFault detail after it is thrown.
*
* @param method calling method
* @param args method arguments
* @param target target
* @param ex AxisFault
* @throws Throwable throwable
*/
@AfterThrowing(pointcut="execution(* com.my.service.MyService.*(..))", throwing="ex")
public void afterThrowing(final Throwable ex) {
log.error(System.getProperty("line.separator") + "*****" + ex.getMessage());
}
}


Spring applicationContext.xml
<aop:aspectj-autoproxy/>
<bean id="loggingAdvice" class="my.com.aop.LoggingAdvice" />