001: //$HeadURL$
002: /*---------------- FILE HEADER ------------------------------------------
003: This file is part of deegree.
004: Copyright (C) 2001-2008 by:
005: Department of Geography, University of Bonn
006: http://www.giub.uni-bonn.de/deegree/
007: lat/lon GmbH
008: http://www.lat-lon.de
009:
010: This library is free software; you can redistribute it and/or
011: modify it under the terms of the GNU Lesser General Public
012: License as published by the Free Software Foundation; either
013: version 2.1 of the License, or (at your option) any later version.
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018: You should have received a copy of the GNU Lesser General Public
019: License along with this library; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: Contact:
022:
023: Andreas Poth
024: lat/lon GmbH
025: Aennchenstr. 19
026: 53177 Bonn
027: Germany
028: E-Mail: poth@lat-lon.de
029:
030: Prof. Dr. Klaus Greve
031: Department of Geography
032: University of Bonn
033: Meckenheimer Allee 166
034: 53115 Bonn
035: Germany
036: E-Mail: greve@giub.uni-bonn.de
037: ---------------------------------------------------------------------------*/
038: package org.deegree.tools.security;
039:
040: import java.net.URL;
041: import java.util.HashMap;
042: import java.util.Map;
043: import java.util.Properties;
044:
045: import org.deegree.framework.log.ILogger;
046: import org.deegree.framework.log.LoggerFactory;
047: import org.deegree.ogcwebservices.wms.capabilities.Layer;
048: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilities;
049: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocument;
050: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocumentFactory;
051: import org.deegree.security.GeneralSecurityException;
052: import org.deegree.security.UnauthorizedException;
053: import org.deegree.security.drm.SecurityAccessManager;
054: import org.deegree.security.drm.SecurityTransaction;
055: import org.deegree.security.drm.UnknownException;
056: import org.deegree.security.drm.model.User;
057:
058: /**
059: * Tool for adding all layers (which can be requested) of a WMS into deegree's user and rights management system
060: *
061: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
062: * @author last edited by: $Author: poth $
063: *
064: * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
065: */
066: public class WMSLayerImporter {
067:
068: private static final ILogger LOG = LoggerFactory
069: .getLogger(WMSLayerImporter.class);
070:
071: private Configuration configuration;
072:
073: private SecurityAccessManager manager;
074:
075: /**
076: *
077: * @param configuration
078: */
079: public WMSLayerImporter(Configuration configuration) {
080: this .configuration = configuration;
081: }
082:
083: /**
084: *
085: * @param param
086: * @throws IllegalArgumentException
087: * if map is missing a required parameter
088: */
089: public WMSLayerImporter(Map<String, String> param)
090: throws IllegalArgumentException {
091: this .configuration = new Configuration(param);
092: }
093:
094: /**
095: * initializes access to the security and rights db
096: *
097: * @throws GeneralSecurityException
098: * @return admin user
099: */
100: private User setUp() throws GeneralSecurityException {
101: Properties properties = new Properties();
102: properties
103: .setProperty("driver", configuration.getSecDBDriver());
104: properties.setProperty("url", configuration.secDBURL);
105: properties
106: .setProperty("user", configuration.getSecDBUserName());
107: properties.setProperty("password", configuration
108: .getSecDBUserPw());
109: System.out.println(properties);
110: try {
111: manager = SecurityAccessManager.getInstance();
112: } catch (GeneralSecurityException e) {
113: try {
114: System.out.println(properties);
115: SecurityAccessManager.initialize(
116: "org.deegree.security.drm.SQLRegistry",
117: properties, 60 * 1000);
118: manager = SecurityAccessManager.getInstance();
119: } catch (GeneralSecurityException e1) {
120: e1.printStackTrace();
121: }
122: }
123: User user = manager.getUserByName("SEC_ADMIN");
124: user.authenticate(configuration.getSecAdminPw());
125: return user;
126: }
127:
128: /**
129: * start reading, parsing WMSCapabilites and adding requestable layers into rights DB
130: *
131: * @throws Exception
132: */
133: public void perform() throws Exception {
134:
135: // initialize access to rights DB
136: User user = setUp();
137:
138: URL url = new URL(configuration.getWmsAddress()
139: + "?request=GetCapabilities&service=WMS");
140: WMSCapabilitiesDocument doc = WMSCapabilitiesDocumentFactory
141: .getWMSCapabilitiesDocument(url);
142:
143: WMSCapabilities caps = (WMSCapabilities) doc
144: .parseCapabilities();
145: Layer layer = caps.getLayer();
146: traverseLayer(layer, user);
147: }
148:
149: /**
150: *
151: * @param layer
152: * @throws GeneralSecurityException
153: * @throws UnauthorizedException
154: */
155: private void traverseLayer(Layer layer, User user)
156: throws UnauthorizedException, GeneralSecurityException {
157: if (layer.getName() != null) {
158: // just layers having a name can be considered because just these layers
159: // can be requests in a GetMap or GetFeatureInfo request
160: addLayerToRightsDB(layer, user);
161: }
162: Layer[] layers = layer.getLayer();
163: if (layers != null) {
164: for (int i = 0; i < layers.length; i++) {
165: traverseLayer(layers[i], user);
166: }
167: }
168: }
169:
170: /**
171: *
172: * @param layer
173: * @param user
174: * @throws UnauthorizedException
175: * @throws GeneralSecurityException
176: */
177: private void addLayerToRightsDB(Layer layer, User user)
178: throws UnauthorizedException, GeneralSecurityException {
179:
180: SecurityTransaction transaction = manager
181: .acquireTransaction(user);
182: try {
183: transaction
184: .getSecuredObjectByName(layer.getName(), "Layer");
185: } catch (UnknownException e) {
186: LOG.logInfo("add layer: " + layer.getName());
187: transaction.registerSecuredObject("Layer", layer.getName(),
188: layer.getTitle());
189: return;
190: } finally {
191: manager.commitTransaction(transaction);
192: }
193:
194: LOG.logInfo("skip layer: " + layer.getName()
195: + " because it is already registered to rights DB");
196:
197: }
198:
199: private static void printHelp() {
200: System.out.println("following parameters must be set: ");
201: System.out
202: .println("-WMSAddress : must be a valid URL to a WMS");
203: System.out.println("-Driver : JDBC database driver class");
204: System.out
205: .println("-URL : JDBC URL of the rights managment DB ");
206: System.out.println("-DBUserName : name of DB-user");
207: System.out.println("-DBUserPassword : password of DB-user");
208: System.out
209: .println("-SecAdminPassword : password of rights managment admin");
210: System.out.println();
211: System.out.println("example:");
212: System.out
213: .println("java -classpath .;$ADD LIBS HERE org.deegree.tools.security.WMSLayerImporter ");
214: System.out
215: .println(" -WMSAddress http://demo.deegree.org/deegree-wms/services ");
216: System.out
217: .println(" -Driver org.postgresql.Driver -URL jdbc:postgresql://localhost:5432/security ");
218: System.out
219: .println(" -DBUserName postgres -DBUserPassword postgres -SecAdminPassword JOSE67");
220: }
221:
222: /**
223: * @param args
224: * @throws Exception
225: */
226: public static void main(String[] args) throws Exception {
227:
228: Map<String, String> map = new HashMap<String, String>();
229: for (int i = 0; i < args.length; i += 2) {
230: if (args[i].equals("-h") || args[i].equals("-?")) {
231: printHelp();
232: return;
233: }
234: map.put(args[i], args[i + 1]);
235: }
236: WMSLayerImporter imp = new WMSLayerImporter(map);
237: imp.perform();
238: System.exit(0);
239: }
240:
241: /**
242: *
243: * <code>Configuration</code> which holds values for a given layer.
244: *
245: * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
246: *
247: * @author last edited by: $Author:$
248: *
249: * @version $Revision:$, $Date:$
250: *
251: */
252: public class Configuration {
253:
254: private String wmsAddress;
255:
256: private String secDBDriver;
257:
258: String secDBURL;
259:
260: private String secDBUserPw;
261:
262: private String secDBUserName;
263:
264: private String secAdminPw;
265:
266: /**
267: *
268: * @param wmsAddress
269: * the address of the remote wms.
270: * @param secDBDriver
271: * the type of database
272: * @param secDBURL
273: * database url
274: * @param secDBUserName
275: * of the database user.
276: * @param secDBUserPw
277: * password of the database user.
278: * @param secAdminPw
279: * password of the security administrator.
280: */
281: public Configuration(String wmsAddress, String secDBDriver,
282: String secDBURL, String secDBUserName,
283: String secDBUserPw, String secAdminPw) {
284: this .wmsAddress = wmsAddress;
285: this .secDBDriver = secDBDriver;
286: this .secDBURL = secDBURL;
287: this .secDBUserName = secDBUserName;
288: this .secDBUserPw = secDBUserPw;
289: this .secAdminPw = secAdminPw;
290: }
291:
292: /**
293: *
294: * @param map
295: * containing the the arguments given to the main.
296: * @throws IllegalArgumentException
297: * if one of the required parameters was not set.
298: */
299: public Configuration(Map<String, String> map)
300: throws IllegalArgumentException {
301: validate(map);
302: wmsAddress = map.get("-WMSAddress");
303: secDBDriver = map.get("-Driver");
304: secDBURL = map.get("-URL");
305: secDBUserName = map.get("-DBUserName");
306: secDBUserPw = map.get("-DBUserPassword");
307: secAdminPw = map.get("-SecAdminPassword");
308: }
309:
310: private void validate(Map<String, String> map)
311: throws IllegalArgumentException {
312: if (map.get("-WMSAddress") == null) {
313: throw new IllegalArgumentException(
314: "Parameter -WMSAddress must be set");
315: }
316: try {
317: new URL(map.get("-WMSAddress"));
318: } catch (Exception e) {
319: throw new IllegalArgumentException(
320: "Parameter -WMSAddress must be a valid URL");
321: }
322: if (map.get("-Driver") == null) {
323: throw new IllegalArgumentException(
324: "Parameter -Driver must be set");
325: }
326: if (map.get("-URL") == null) {
327: throw new IllegalArgumentException(
328: "Parameter -URL must be set");
329: }
330: if (map.get("-DBUserName") == null) {
331: throw new IllegalArgumentException(
332: "Parameter -DBUserName must be set");
333: }
334: if (map.get("-DBUserPassword") == null) {
335: throw new IllegalArgumentException(
336: "Parameter -DBUserPassword must be set");
337: }
338: if (map.get("-SecAdminPassword") == null) {
339: throw new IllegalArgumentException(
340: "Parameter -SecAdminPassword must be set");
341: }
342: }
343:
344: /**
345: *
346: * @return database driver class
347: */
348: public String getSecDBDriver() {
349: return secDBDriver;
350: }
351:
352: /**
353: *
354: * @return database URL
355: */
356: public String getSecDBURL() {
357: return secDBURL;
358: }
359:
360: /**
361: *
362: * @return address/URL of the WMS
363: */
364: public String getWmsAddress() {
365: return wmsAddress;
366: }
367:
368: /**
369: *
370: * @return rights management administrator password
371: */
372: public String getSecAdminPw() {
373: return secAdminPw;
374: }
375:
376: /**
377: *
378: * @return rights db user name
379: */
380: public String getSecDBUserName() {
381: return secDBUserName;
382: }
383:
384: /**
385: *
386: * @return rights database user's password
387: */
388: public String getSecDBUserPw() {
389: return secDBUserPw;
390: }
391:
392: }
393:
394: }
|