Project

General

Profile

1 5272 daigle
<%@ page contentType="text/plain;charset=ISO-8859-1" import="java.io.*" %>
2
<%
3
/************************************************
4
 * This is an example implementation of a Kepler AuthNamespace web service.
5
 *
6
 * The output parameters are
7
 *   error, namespace
8
 * The optional input parameters are
9
 *   username, password, namespace
10
 *
11
 ************************************
12
 * Kepler AuthNamespace Web Service *
13
 ************************************
14
 *
15
 * The namespace that is returned shall always be unique for the given URL regardless of URI input parameters
16
 * The file type returned by the webservice shall be text/plain and shall contain the two output parameters
17
 *      formatted in accordance with the java.util.Properties specification.
18
 *
19
 * Optionally a username and password parameter may be supplied to the webservice for authenticating users
20
 *      or simply for disallowing robots from accessing the service.
21
 * Implementations of the AuthNamespace service may also optionally accept a namespace as an input,
22
 *      check to see if that namespace is available, and return it if it is available or return an error if it is not.
23
 *
24
 ************************************************/
25
try {
26
	/*
27
	 * The authority string should exactly match the URL this service is accessed by.
28
	 * This prevents having multiple URLs accessing this same JSP page which would be bad
29
	 *   since the namespaces generated by an authority must be unique to that authority.
30
	 */
31 5274 daigle
	String authority = "http://library.kepler-project.org/kepler/style/skins/kepler/keplerAuthNameSpace.jsp";
32 5272 daigle
	String accessedAuthority = request.getRequestURL().toString();
33
34
	/*
35
	 * Get username and password inputs.
36
	 */
37
	String user = request.getParameter( "username" );
38
	String pass = request.getParameter( "password" );
39
40
	/*
41
	 * Make sure the URL used to access this service is the correct authority URL.
42
	 */
43
	if ( accessedAuthority != null && accessedAuthority.equals( authority )) {
44
45
		/*
46
		 * Check to make sure the "kepler" username/password was given
47
		 * This will prevent stray robots or users from incrementing namespaces arbitrarily.
48
		 */
49 5274 daigle
		if ( user != null && user.equals("kepler") ) {
50
			if ( pass != null && pass.equals("kepler") ) {
51 5272 daigle
52
				if ( request.getParameter( "namespace" ) != null ) {
53
					// TODO query a database for the requested namespace
54
					// print namespace if it is not already taken and insert it into the database
55
					// return an error if the namespace has already been reserved
56
				} else {
57
58
					/* Store an incrementing integer in a file.
59
					 * This approach may fail due to collisions under high load conditions.
60
					 * But works fine for this example implementation.
61
					 */
62
					ServletContext context = session.getServletContext();
63 5274 daigle
					String fname = "/var/kepler/last_issued_namespace.txt";
64 5272 daigle
65
					Integer inc = new Integer(0);
66
67
					File f = new File( fname );
68
					if (f.exists()) {
69
70
						// Read in the previous Integer
71
						InputStream is = new FileInputStream(fname);
72
						ObjectInput oi = new ObjectInputStream(is);
73
						inc = (Integer) oi.readObject();
74
						oi.close();
75
76
						int i = inc.intValue() + 1;
77
						inc = new Integer( i );
78
79
					}
80
81
					// Write out the incremented integer (or 0 if the file does not exist)
82
					OutputStream os = new FileOutputStream(fname);
83
					ObjectOutputStream oos = new ObjectOutputStream(os);
84
					oos.writeObject( inc );
85
					oos.flush();
86
87
					/*
88
					 * Print out the incremented Integer as the namespace output
89
					 * in java.util.Properties format.
90
					 */
91
					out.println( "namespace=" + inc + "\n" );
92
				}
93
94
			} else {
95
				throw new Exception("The specified password is incorrect");
96
			}
97
		} else {
98
			throw new Exception("The specified username is incorrect.");
99
		}
100
	} else {
101
		throw new Exception("The specified authority URL is incorrect.");
102
	}
103
104
} catch (Exception e) {
105
106
	/*
107
	 * Print out the error in java.util.Properties format.
108
	 */
109
	out.println( "error=" + e.getMessage() + "\n" );
110
}
111
%>