View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gwui.servlets;
7   
8   import java.io.UnsupportedEncodingException;
9   import javax.servlet.http.HttpServletRequest;
10  
11  import net.kwfgrid.gworkflowdl.structure.*;
12  
13  import java.util.Map;
14  import java.util.Iterator;
15  import java.util.Enumeration;
16  import java.io.*;
17  
18  import org.apache.log4j.Logger;
19  
20  import http.utils.multipartrequest.*;
21  
22  /***
23     Must be deployed as "/GenericDataServlet".
24   */
25  public final class GenericDataServlet extends DataServletBase {    
26      private static Logger logger = Logger.getLogger(GenericDataServlet.class);
27  
28      /*** The content type for file upload. */
29      public static final String CONTENT_TYPE_FILE = "multipart/form-data";
30  
31      /*** The maximum length of uploaded files. */
32      public static final int MAX_READ_BYTES = 1000000;
33  
34      /*** The key for the "token" parameter. */
35      public static final String KEY_TOKEN = "token";
36      
37      /*** The placeholder for the XML value of an edited token. */
38      public static final String PLACEHOLDER_TOKENXML = "//$TOKEN_XML";
39  
40      protected Logger getLogger() {
41  	return logger;
42      }
43  
44      protected Token createToken(HttpServletRequest request) 
45  	throws WorkflowFormatException, UnsupportedEncodingException, IllegalArgumentException {
46  	String tokenspec = (String)request.getParameter(KEY_TOKEN);
47  	
48  	if (tokenspec == null) {
49  	    try {
50  		MultipartRequest mp = new ServletMultipartRequest(request, MAX_READ_BYTES, MultipartRequest.ABORT_IF_MAX_BYES_EXCEEDED, null);
51  		Enumeration e = mp.getFileParameterNames();
52  
53  		if (!e.hasMoreElements()) 
54  		    throw new IllegalArgumentException("No filename in request.");
55  
56  		String fn = (String)e.nextElement();
57  		InputStream fc = mp.getFileContents(fn);
58  		
59  		if (fc == null)
60  		    throw new IllegalArgumentException("No file content for filename "+fn+".");
61  
62  		BufferedReader in = new BufferedReader(new InputStreamReader(fc));
63  		String line = null;
64  		StringBuffer buf = new StringBuffer();
65  		while ((line = in.readLine()) != null) {
66  		    buf.append(line);
67  		}
68  
69  		tokenspec = buf.toString();
70  	    } catch (MaxReadBytesException x) {
71  		IllegalArgumentException ix = new IllegalArgumentException("Too many bytes in request.");
72  		ix.initCause(x);
73  		throw ix;
74  	    } catch (IOException x) {
75  		IllegalArgumentException ix = new IllegalArgumentException("IOException when reading uploaded file.");
76  		ix.initCause(x);
77  		throw ix;
78  	    }	    
79  	} else {
80  	    tokenspec = ServletUtilities.decode(tokenspec);	    
81  	}
82  
83  	getLogger().info("Creating token: \n"+tokenspec);
84  
85      Data data = Factory.newData();
86      data.fromXML(tokenspec);
87      Token token = Factory.newToken(data);
88  
89  	return token;
90      }
91  
92      protected String replaceValues(String form, Token token) {
93          String xml;
94          if (token.getData() != null) {
95               //data token
96               xml = token.getData().toXML();
97           } else {
98               // control token
99               Boolean control = token.getControl();
100              xml = control.booleanValue() ? "<control>true</control>" : "<control>false</control>";
101          }
102     	return ServletUtilities.replace(form, PLACEHOLDER_TOKENXML, xml);
103     }
104 
105     protected String clearValues(String form) {
106 	return ServletUtilities.replace(form, PLACEHOLDER_TOKENXML, "");
107     }
108 
109     protected String replaceValues(String form, Transition transition) {
110 	return clearValues(form);
111     }
112 }