Wednesday, October 22, 2008

Sample Code for File Upload Using Commons FileUpload

to use this jsp you will need to have io commons and fileupload commons in your classpath

----------
<%@ page import="org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.util.*,org.apache.commons.fileupload.servlet.*,org.apache.commons.fileupload.*,java.io.*,java.util.*;"%>
<html>
<body>

<%
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
boolean writeToFile = false;

if (isMultipart) {

DiskFileItemFactory factory = new DiskFileItemFactory();
//factory.setSizeThreshold(yourMaxMemorySize); you may want to set this
factory.setRepository(new File("/tmp"));
ServletFileUpload upload = new ServletFileUpload(factory);
//upload.setSizeMax(yourMaxRequestSize); you may want to set this as well
List items = upload.parseRequest(request);

Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
String name = item.getFieldName();

if (item.isFormField()) {
out.println("Form field " + name + " with value "
+ item.getString() + ".<br/>");
if (name.equals("writeToFile") && item.getString().equals("TRUE")) {
writeToFile = true;
}
} else {
out.println("File field " + name + " with file name "
+ item.getName() + " detected.<br/>");
if (writeToFile) {
File uploadedFile = new File("/tmp/"+System.currentTimeMillis()+"."+item.getName());
item.write(uploadedFile);
out.print("File saved to " + uploadedFile.getName()+"<br/>");
} else {
out.print("File contents: <pre>");
InputStream uploadedStream = item.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(uploadedStream));
String line = "";
while (line != null) {
line = br.readLine();
out.println(line);
}
uploadedStream.close();
out.print("</pre>");
}
}
}

}
else {
%>
<form ACTION="index.jsp" METHOD="post" ENCTYPE="multipart/form-data">
writeToFile <input TYPE="text" NAME="writeToFile" VALUE="TRUE" ><br/>
field1 <input TYPE="text" NAME="field1" VALUE="some value"><br/>
file <input TYPE="file" NAME="fileToUpload" VALUE=""><br/>
<input TYPE="submit" VALUE="upload file">
</form>
<%
}
%>

</body>
</html>

Labels:

Monday, July 31, 2006

ActiveBPEL Persistent Configuration won't start

environment:
tomcat 5.5.12
mysql 4.1
activebpel 2.1 (1473)

problem:
- activebel engine with persistent configuration won't start.

symptoms:

- activebpel status says:
Error: Storage not available, see storage page for details.

- tomcat logs say:
Jul 31, 2006 11:18:26 AM org.activebpel.rt.AeException logError
SEVERE: org.activebpel.rt.bpel.server.engine.storage.AeStorageException: Error initializing the persistent store factory.
at org.activebpel.rt.bpel.server.engine.storage.AePersistentStoreFactory.init(AePersistentStoreFactory.java:84)
at org.activebpel.rt.bpel.server.engine.AeEngineFactory.initializePersistentStoreFactory(AeEngineFactory.java:484)
at org.activebpel.rt.bpel.server.engine.AeEngineFactory.initializeStorage(AeEngineFactory.java:274)
at org.activebpel.rt.bpel.server.engine.AeEngineFactory.preInit(AeEngineFactory.java:183)
at org.activebpel.rt.bpel.server.engine.AeEngineLifecycleWrapper.init(AeEngineLifecycleWrapper.java:73)
at org.activebpel.rt.axis.bpel.web.AeProcessEngineServlet.init(AeProcessEngineServlet.java:133)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:925)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3880)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4141)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:603)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:535)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:470)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1118)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1020)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:680)
at org.apache.catalina.startup.Catalina.start(Catalina.java:536)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:275)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.activebpel.rt.AeException: Error creating Storage Provider.
at org.activebpel.rt.bpel.server.engine.storage.AeStorageFactory.createStorageProviderFactory(AeStorageFactory.java:117)
at org.activebpel.rt.bpel.server.engine.storage.AeStorageFactory.setConfiguration(AeStorageFactory.java:77)
at org.activebpel.rt.bpel.server.engine.storage.AePersistentStoreFactory.init(AePersistentStoreFactory.java:75)
... 31 more
Caused by: java.lang.UnsupportedOperationException
at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:116)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:554)
at org.activebpel.rt.bpel.server.engine.storage.sql.AeDataSource.getConnection(AeDataSource.java:323)
at org.activebpel.rt.bpel.server.engine.storage.sql.AeJNDIDataSource.getConnection(AeJNDIDataSource.java:116)
at org.activebpel.rt.bpel.server.engine.storage.sql.AeSQLObject.getConnection(AeSQLObject.java:48)
at org.activebpel.rt.bpel.server.engine.storage.sql.AeSQLDatabaseType.getDatabaseType(AeSQLDatabaseType.java:68)
at org.activebpel.rt.bpel.server.engine.storage.sql.AeSQLStorageProviderFactory.validateDatabaseType(AeSQLStorageProviderFactory.java:175)
at org.activebpel.rt.bpel.server.engine.storage.sql.AeSQLStorageProviderFactory.setConfiguration(AeSQLStorageProviderFactory.java:93)
at org.activebpel.rt.bpel.server.engine.storage.sql.AeSQLStorageProviderFactory.(AeSQLStorageProviderFactory.java:60)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.activebpel.rt.bpel.server.engine.storage.AeStorageFactory.createStorageProviderFactory(AeStorageFactory.java:108)
... 33 more


solution:
make sure that the database username is not specified in both tomcat context datasource definition and the activebpel engine configuration (aeEngineConfig.xml)

Monday, July 17, 2006

BT Deployment Woes: Requested registry access is not allowed

Problem:
Uncaught exception (see the 'inner exception' below) has suspended an instance of service 'CreateFacility.CreateFacilityFlow(9dc89e4a-8ade-a4c9-7f35-ca44c0335810)'.
The service instance will remain suspended until administratively resumed or terminated.
If resumed the instance will continue from its last persisted state and may re-throw the same unexpected exception.
InstanceId: c688d7b3-faf0-4b5c-a612-9f2ec41add2f
Shape name:
ShapeId: 00000000-0000-0000-0000-000000000000
Exception thrown from: segment 2, progress 21
Inner exception: Requested registry access is not allowed.

Exception type: SecurityException
Source: mscorlib
Target Site: Void ThrowSecurityException(System.ExceptionResource)
The following is a stack trace that identifies the location where the exception occured

at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData)
at System.Diagnostics.EventLog.VerifyAndCreateSource(String sourceName, String currentMachineName)
at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String source, String message)
at CreateFacility.CreateFacilityFlow.segment2(StopConditions stopOn)
at Microsoft.XLANGs.Core.SegmentScheduler.RunASegment(Segment s, StopConditions stopCond, Exception& exp)



For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

Diagnosis:
The application is trying to write to the Windows Event Log but does not have sufficient rights.

Solution:
Gave BT Host Users and BT Isolated Host Users write access to the event log registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog

Thursday, June 15, 2006

Windows Authentication and Tomcat 5

references:
- Tomcat and NTLM Authentication
- [jcifs] jcifs encoding issue with NtlmHttpFilter and IE 6.0

tools used:
jcifs - an SMB client library written entirely in Java

problems encountered:
problem: java.io.UnsupportedEncodingException: Cp850 occurs when using when using internet explorer. works fine with firefox.
solution: in the web.xml set jcifs.encoding to cp1252

mobile247's development blog

i hate to admit it but more often than not i over estimate my capacity to remember things. as a result, while working on a project, if i run into an issue i've solved before i can't remember the solution and i hate wasting time time doing research that i've already done before. this blog will be my attempt to address this problem. i will blog about all the tips, tricks, problems, solutions, resources, and references i encounter when on a development project. hopefully, this won't be a one time thing. well, we'll see... =)