source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/ServletRealmCheck.java@ 28958

Last change on this file since 28958 was 28958, checked in by davidb, 10 years ago

Remote Greenstone user authenticaton stopped working, because the code working with the DerbyWrapper had changed, and now gliserver.pl could no longer instantiate another JVM that would access the Derby DB (via the users2DBtxt.java) when wanting to check if a user authenticates. Instead, a new GS3 service has been written, Authentication.remoteAuthentication(). This is called from the authentication-ping system action URL that the new ServletRealmCheck.java pings when it is called by gliserver.pl

File size: 4.9 KB
Line 
1/*
2 * ServletRealmCheck.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import java.io.BufferedReader;
22import java.io.File;
23import java.io.FileInputStream;
24import java.io.InputStream;
25import java.io.InputStreamReader;
26import java.io.IOException;
27import java.net.HttpURLConnection;
28import java.net.URL;
29import java.net.URLConnection;
30import java.util.Properties;
31
32/**
33 * Commandline script that is used by gliserver.pl to authenticate a username and password and
34 * return the user's groups, while the derby server is running. Because 2 JVM instances can't
35 * access the same embedded derby server at the same time, gliserver can't call usersDB2txt.java.
36 * If a collection parameter is additionally provided, this script will check the user's groups
37 * to see if any of these allow the user to edit that collection.
38 *
39 * Run as java org.greenstone.gsdl3.util.ServletRealmCheck <GS3SRCHOME> <un> <pwd> [colname]
40 *
41 * GS3\src\java>"C:\Program Files\Java\jdk1.6.0_22\bin\java"
42 * -classpath "GS3\web\WEB-INF\lib\gsdl3.jar;GS3\web\WEB-INF\lib\derby.jar"
43 * org.greenstone.gsdl3.util.ServletRealmCheck "GS3" admin <pw> 2>&1
44 *
45 * Tries URL: http://hostname:8383/greenstone3/library?a=s&sa=authenticated-ping&excerptid=gs_content&un=admin&pw=<pw>[&col=demo]
46 * The &excerptid=gs_content in the URL will return just the <div id="gs_content" /> part of the
47 * page that we're interested in.
48 *
49 * Result: either prints out an error message ("Authentication failed...") or a positive result,
50 * which is the user's groups. For the admin user example: administrator,all-collections-editor.
51 *
52*/
53public class ServletRealmCheck
54{
55 public static void main(String[] args) {
56
57 if (args.length < 3 || args.length > 4){
58 System.out.println("Run with: <GSDL3SRCHOME> <un> <pwd> [collection-name]");
59 System.exit(0);
60 }
61
62 String gsdl3srchome = args[0];
63 String username = args[1];
64 String password = args[2];
65 String collection = (args.length > 3) ? args[3] : null;
66
67 //System.err.println("gsdl3srchome: " + gsdl3srchome);
68 //System.err.println("username: " + username);
69 //System.err.println("password: " + password);
70 //System.err.println("collection: " + collection);
71
72
73 // Load the build.properties file, get the GS3 server URL and send authenticated-ping and print the return result
74
75 //http://www.mkyong.com/java/java-properties-file-examples/
76 Properties buildProps = new Properties();
77 InputStream input = null;
78
79 try {
80 File buildPropsFile = new File(gsdl3srchome, "build.properties");
81 input = new FileInputStream(buildPropsFile);
82
83 // load a properties file
84 buildProps.load(input);
85
86 // get the property value and print it out
87 String servername = buildProps.getProperty("tomcat.server");
88 String port = buildProps.getProperty("tomcat.port");
89 int portNum = Integer.parseInt(port);
90
91 // Appending &excerptid=gs_content will get just the <div ... id="gs_content"/> from the final web page:
92 String urlSuffix = "/greenstone3/library?a=s&sa=authenticated-ping&excerptid=gs_content&un="+username+"&pw="+password;
93 if(collection != null) {
94 urlSuffix = urlSuffix + "&col="+collection;
95 }
96 URL authenticationUrl = new URL("http", servername, portNum, urlSuffix);
97
98 HttpURLConnection conn = (HttpURLConnection)authenticationUrl.openConnection();
99 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
100 String result = "";
101 String line = null;
102
103 while((line = reader.readLine()) != null) {
104 result += line;
105 }
106
107 //System.err.println("** Sent: " + authenticationUrl);
108 //System.err.println("** Got result:\n" + result);
109
110 // Parse out the content nested inside <div ... id="gs_content"> </div>
111 int start = result.indexOf("id=\"gs_content\">");
112 if(start != -1) {
113 start = result.indexOf(">", start);
114 int end = result.indexOf("<", start);
115 result = result.substring(start+1, end);
116 result = result.trim();
117 }
118
119 // Now we finally have what we actually want to print out for the caller to use
120 System.out.print(result); // don't add newline to end
121
122 } catch (IOException ex) {
123 ex.printStackTrace();
124 } finally {
125 if (input != null) {
126 try {
127 input.close();
128 } catch (IOException e) {
129 e.printStackTrace();
130 }
131 }
132 }
133
134 }
135
136}
Note: See TracBrowser for help on using the repository browser.