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

Last change on this file since 32357 was 32357, checked in by ak19, 6 years ago

GS3 Java code has moved away from using tomcat.port to using tomcat.port.<protocol>.

File size: 5.4 KB
RevLine 
[28958]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
[32357]32import org.greenstone.util.ProtocolPortProperties;
33
[28958]34/**
35 * Commandline script that is used by gliserver.pl to authenticate a username and password and
36 * return the user's groups, while the derby server is running. Because 2 JVM instances can't
37 * access the same embedded derby server at the same time, gliserver can't call usersDB2txt.java.
38 * If a collection parameter is additionally provided, this script will check the user's groups
39 * to see if any of these allow the user to edit that collection.
40 *
41 * Run as java org.greenstone.gsdl3.util.ServletRealmCheck <GS3SRCHOME> <un> <pwd> [colname]
42 *
43 * GS3\src\java>"C:\Program Files\Java\jdk1.6.0_22\bin\java"
44 * -classpath "GS3\web\WEB-INF\lib\gsdl3.jar;GS3\web\WEB-INF\lib\derby.jar"
45 * org.greenstone.gsdl3.util.ServletRealmCheck "GS3" admin <pw> 2>&1
46 *
47 * Tries URL: http://hostname:8383/greenstone3/library?a=s&sa=authenticated-ping&excerptid=gs_content&un=admin&pw=<pw>[&col=demo]
48 * The &excerptid=gs_content in the URL will return just the <div id="gs_content" /> part of the
49 * page that we're interested in.
50 *
51 * Result: either prints out an error message ("Authentication failed...") or a positive result,
52 * which is the user's groups. For the admin user example: administrator,all-collections-editor.
53 *
54*/
55public class ServletRealmCheck
56{
57 public static void main(String[] args) {
58
59 if (args.length < 3 || args.length > 4){
60 System.out.println("Run with: <GSDL3SRCHOME> <un> <pwd> [collection-name]");
61 System.exit(0);
62 }
63
64 String gsdl3srchome = args[0];
65 String username = args[1];
66 String password = args[2];
67 String collection = (args.length > 3) ? args[3] : null;
68
69 //System.err.println("gsdl3srchome: " + gsdl3srchome);
70 //System.err.println("username: " + username);
71 //System.err.println("password: " + password);
72 //System.err.println("collection: " + collection);
73
74
75 // Load the build.properties file, get the GS3 server URL and send authenticated-ping and print the return result
76
77 //http://www.mkyong.com/java/java-properties-file-examples/
78 Properties buildProps = new Properties();
79 InputStream input = null;
80
81 try {
82 File buildPropsFile = new File(gsdl3srchome, "build.properties");
83 input = new FileInputStream(buildPropsFile);
84
85 // load a properties file
86 buildProps.load(input);
87
88 // get the property value and print it out
89 String servername = buildProps.getProperty("tomcat.server");
[32357]90 //String protocol = buildProps.getProperty("server.protocol", "http");
91 //String port = buildProps.getProperty("tomcat.port"+protocol, "8383");
92
93 ProtocolPortProperties protocolPortProps = new ProtocolPortProperties(buildProps);
94 if(protocolPortProps.hadError()) {
95 throw new Exception("**** ERROR with port and/or protocol in build.properties:\n" + protocolPortProps.getErrorMsg());
96 }
97 String protocol = protocolPortProps.getProtocol();
98 String port = protocolPortProps.getPort();
[28958]99 int portNum = Integer.parseInt(port);
100
101 // Appending &excerptid=gs_content will get just the <div ... id="gs_content"/> from the final web page:
102 String urlSuffix = "/greenstone3/library?a=s&sa=authenticated-ping&excerptid=gs_content&un="+username+"&pw="+password;
103 if(collection != null) {
104 urlSuffix = urlSuffix + "&col="+collection;
105 }
[32357]106 URL authenticationUrl = new URL(protocol, servername, portNum, urlSuffix);
[28958]107
108 HttpURLConnection conn = (HttpURLConnection)authenticationUrl.openConnection();
109 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
110 String result = "";
111 String line = null;
112
113 while((line = reader.readLine()) != null) {
114 result += line;
115 }
116
117 //System.err.println("** Sent: " + authenticationUrl);
118 //System.err.println("** Got result:\n" + result);
119
120 // Parse out the content nested inside <div ... id="gs_content"> </div>
[28959]121 int start = result.indexOf("id=\"gs_content\"");
[28958]122 if(start != -1) {
123 start = result.indexOf(">", start);
124 int end = result.indexOf("<", start);
125 result = result.substring(start+1, end);
126 result = result.trim();
127 }
128
129 // Now we finally have what we actually want to print out for the caller to use
130 System.out.print(result); // don't add newline to end
131
[32357]132 } catch (Exception ex) {
[28958]133 ex.printStackTrace();
134 } finally {
135 if (input != null) {
136 try {
137 input.close();
138 } catch (IOException e) {
139 e.printStackTrace();
140 }
141 }
142 }
143
144 }
145
146}
Note: See TracBrowser for help on using the repository browser.