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.datatypes.QualifiedName;
046: import org.deegree.framework.log.ILogger;
047: import org.deegree.framework.log.LoggerFactory;
048: import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
049: import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilitiesDocument;
050: import org.deegree.ogcwebservices.wfs.capabilities.WFSFeatureType;
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 requestable featuretypes of a WFS 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 WFSFeatureTypeImporter {
067:
068: private static final ILogger LOG = LoggerFactory
069: .getLogger(WFSFeatureTypeImporter.class);
070:
071: private Configuration configuration;
072:
073: private SecurityAccessManager manager;
074:
075: /**
076: *
077: * @param configuration
078: */
079: public WFSFeatureTypeImporter(Configuration configuration) {
080: this .configuration = configuration;
081: }
082:
083: /**
084: *
085: * @param param
086: * @throws Exception
087: */
088: public WFSFeatureTypeImporter(Map<String, String> param)
089: throws Exception {
090: this .configuration = new Configuration(param);
091: }
092:
093: /**
094: * initializes access to the security and rights db
095: *
096: * @throws GeneralSecurityException
097: * @return admin user
098: */
099: private User setUp() throws GeneralSecurityException {
100: Properties properties = new Properties();
101: properties
102: .setProperty("driver", configuration.getSecDBDriver());
103: properties.setProperty("url", configuration.secDBURL);
104: properties
105: .setProperty("user", configuration.getSecDBUserName());
106: properties.setProperty("password", configuration
107: .getSecDBUserPw());
108: System.out.println(properties);
109: try {
110: manager = SecurityAccessManager.getInstance();
111: } catch (GeneralSecurityException e) {
112: try {
113: System.out.println(properties);
114: SecurityAccessManager.initialize(
115: "org.deegree.security.drm.SQLRegistry",
116: properties, 60 * 1000);
117: manager = SecurityAccessManager.getInstance();
118: } catch (GeneralSecurityException e1) {
119: e1.printStackTrace();
120: }
121: }
122: User user = manager.getUserByName("SEC_ADMIN");
123: user.authenticate(configuration.getSecAdminPw());
124: return user;
125: }
126:
127: /**
128: * start reading, parsing WFSCapabilites and adding requestable featuretypes into rights DB
129: *
130: * @throws Exception
131: */
132: public void perform() throws Exception {
133:
134: // initialize access to rights DB
135: User user = setUp();
136:
137: URL url = new URL(configuration.getWfsAddress()
138: + "?request=GetCapabilities&service=WFS");
139: WFSCapabilitiesDocument doc = new WFSCapabilitiesDocument();
140: doc.load(url);
141:
142: WFSCapabilities caps = (WFSCapabilities) doc
143: .parseCapabilities();
144: WFSFeatureType[] fts = caps.getFeatureTypeList()
145: .getFeatureTypes();
146: for (int i = 0; i < fts.length; i++) {
147: addFeatureTypeToRightsDB(fts[i], user);
148: }
149: }
150:
151: /**
152: *
153: * @param ft
154: * @param user
155: * @throws UnauthorizedException
156: * @throws GeneralSecurityException
157: */
158: private void addFeatureTypeToRightsDB(WFSFeatureType ft, User user)
159: throws UnauthorizedException, GeneralSecurityException {
160: QualifiedName qn = ft.getName();
161: SecurityTransaction transaction = manager
162: .acquireTransaction(user);
163: try {
164: transaction.getSecuredObjectByName(qn.getFormattedString(),
165: "Featuretype");
166: } catch (UnknownException e) {
167: LOG.logInfo("add featuretype: " + qn.getFormattedString());
168: transaction.registerSecuredObject("Featuretype", qn
169: .getFormattedString(), ft.getTitle());
170: return;
171: } finally {
172: manager.commitTransaction(transaction);
173: }
174:
175: LOG.logInfo("skip featuretype: " + qn.getFormattedString()
176: + " because it is already registered to rights DB");
177:
178: }
179:
180: private static void printHelp() {
181: System.out.println("following parameters must be set: ");
182: System.out
183: .println("-WFSAddress : must be a valid URL to a WFS");
184: System.out.println("-Driver : JDBC database driver class");
185: System.out
186: .println("-URL : JDBC URL of the rights managment DB ");
187: System.out.println("-DBUserName : name of DB-user");
188: System.out.println("-DBUserPassword : password of DB-user");
189: System.out
190: .println("-SecAdminPassword : password of rights managment admin");
191: System.out.println();
192: System.out.println("example:");
193: System.out
194: .println("java -classpath .;$ADD LIBS HERE org.deegree.tools.security.WFSFeatureTypeImporter ");
195: System.out
196: .println(" -WFSAddress http://demo.deegree.org/deegree-wfs/services ");
197: System.out
198: .println(" -Driver org.postgresql.Driver -URL jdbc:postgresql://localhost:5432/security ");
199: System.out
200: .println(" -DBUserName postgres -DBUserPassword postgres -SecAdminPassword JOSE67");
201: }
202:
203: /**
204: * @param args
205: * @throws Exception
206: */
207: public static void main(String[] args) throws Exception {
208:
209: Map<String, String> map = new HashMap<String, String>();
210: for (int i = 0; i < args.length; i += 2) {
211: if (args[i].equals("-h") || args[i].equals("-?")) {
212: printHelp();
213: return;
214: }
215: map.put(args[i], args[i + 1]);
216: }
217: WFSFeatureTypeImporter imp = new WFSFeatureTypeImporter(map);
218: imp.perform();
219: System.exit(0);
220: }
221:
222: /**
223: * <code>Configuration</code> bean to hold relevant data of an underlying datasource.
224: *
225: * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
226: *
227: * @author last edited by: $Author:$
228: *
229: * @version $Revision:$, $Date:$
230: *
231: */
232: public class Configuration {
233:
234: private String wfsAddress;
235:
236: private String secDBDriver;
237:
238: String secDBURL;
239:
240: private String secDBUserPw;
241:
242: private String secDBUserName;
243:
244: private String secAdminPw;
245:
246: /**
247: * @param wfsAddress
248: * @param secDBDriver
249: * @param secDBURL
250: * @param secDBUserName
251: * @param secDBUserPw
252: * @param secAdminPw
253: */
254: public Configuration(String wfsAddress, String secDBDriver,
255: String secDBURL, String secDBUserName,
256: String secDBUserPw, String secAdminPw) {
257: this .wfsAddress = wfsAddress;
258: this .secDBDriver = secDBDriver;
259: this .secDBURL = secDBURL;
260: this .secDBUserName = secDBUserName;
261: this .secDBUserPw = secDBUserPw;
262: this .secAdminPw = secAdminPw;
263: }
264:
265: /**
266: * @param map map with commandline options (eg. key: "-URL", value: "http://...")
267: * @throws Exception
268: */
269: public Configuration(Map<String, String> map) throws Exception {
270: validate(map);
271: wfsAddress = map.get("-WFSAddress");
272: secDBDriver = map.get("-Driver");
273: secDBURL = map.get("-URL");
274: secDBUserName = map.get("-DBUserName");
275: secDBUserPw = map.get("-DBUserPassword");
276: secAdminPw = map.get("-SecAdminPassword");
277: }
278:
279: private void validate(Map<String, String> map) throws Exception {
280: if (map.get("-WFSAddress") == null) {
281: throw new Exception("Parameter -WFSAddress must be set");
282: }
283: try {
284: new URL(map.get("-WFSAddress"));
285: } catch (Exception e) {
286: throw new Exception(
287: "Parameter -WFSAddress must be a valid URL");
288: }
289: if (map.get("-Driver") == null) {
290: throw new Exception("Parameter -Driver must be set");
291: }
292: if (map.get("-URL") == null) {
293: throw new Exception("Parameter -URL must be set");
294: }
295: if (map.get("-DBUserName") == null) {
296: throw new Exception("Parameter -DBUserName must be set");
297: }
298: if (map.get("-DBUserPassword") == null) {
299: throw new Exception(
300: "Parameter -DBUserPassword must be set");
301: }
302: if (map.get("-SecAdminPassword") == null) {
303: throw new Exception(
304: "Parameter -SecAdminPassword must be set");
305: }
306: }
307:
308: /**
309: *
310: * @return database driver class
311: */
312: public String getSecDBDriver() {
313: return secDBDriver;
314: }
315:
316: /**
317: *
318: * @return database URL
319: */
320: public String getSecDBURL() {
321: return secDBURL;
322: }
323:
324: /**
325: *
326: * @return address/URL of the WFS
327: */
328: public String getWfsAddress() {
329: return wfsAddress;
330: }
331:
332: /**
333: *
334: * @return rights management admin password
335: */
336: public String getSecAdminPw() {
337: return secAdminPw;
338: }
339:
340: /**
341: *
342: * @return rights db user name
343: */
344: public String getSecDBUserName() {
345: return secDBUserName;
346: }
347:
348: /**
349: *
350: * @return rights db user's passowrod
351: */
352: public String getSecDBUserPw() {
353: return secDBUserPw;
354: }
355:
356: }
357:
358: }
|