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

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

solr should only be accessible locally (from localhost, specifically 127.0.0.1) which means over http. This conflicted with the previous design of the properties file for working with http and/or https. Now we have tomcat.port.https and localhost.port.http, both always set. In place of server.protocol that used to contain the default protocol, we now have server.protocols which can be set to a comma separated list of one or both of http and https. Drastic restructuring followed. I think I've tested all but https certification stuff.

File size: 5.2 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
32import org.greenstone.util.ProtocolPortProperties;
33
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");
90 ProtocolPortProperties protocolPortProps = new ProtocolPortProperties(buildProps); // can throw Exception
91 String protocol = protocolPortProps.getProtocol();
92 String port = protocolPortProps.getPort();
93 int portNum = Integer.parseInt(port);
94
95 // Appending &excerptid=gs_content will get just the <div ... id="gs_content"/> from the final web page:
96 String urlSuffix = "/greenstone3/library?a=s&sa=authenticated-ping&excerptid=gs_content&un="+username+"&pw="+password;
97 if(collection != null) {
98 urlSuffix = urlSuffix + "&col="+collection;
99 }
100 URL authenticationUrl = new URL(protocol, servername, portNum, urlSuffix);
101
102 HttpURLConnection conn = (HttpURLConnection)authenticationUrl.openConnection();
103 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
104 String result = "";
105 String line = null;
106
107 while((line = reader.readLine()) != null) {
108 result += line;
109 }
110
111 //System.err.println("** Sent: " + authenticationUrl);
112 //System.err.println("** Got result:\n" + result);
113
114 // Parse out the content nested inside <div ... id="gs_content"> </div>
115 int start = result.indexOf("id=\"gs_content\"");
116 if(start != -1) {
117 start = result.indexOf(">", start);
118 int end = result.indexOf("<", start);
119 result = result.substring(start+1, end);
120 result = result.trim();
121 }
122
123 // Now we finally have what we actually want to print out for the caller to use
124 System.out.print(result); // don't add newline to end
125
126 } catch (Exception ex) {
127 System.err.println("Got exception " + ex.getMessage());
128 ex.printStackTrace();
129 } finally {
130 if (input != null) {
131 try {
132 input.close();
133 } catch (IOException e) {
134 e.printStackTrace();
135 }
136 }
137 }
138
139 }
140
141}
Note: See TracBrowser for help on using the repository browser.