001: /*
002: * (C) Copyright 2000 - 2005 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.ws.interceptor.security;
020:
021: import java.io.IOException;
022: import java.io.StringReader;
023:
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026:
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.xml.sax.InputSource;
030:
031: import com.nabhinc.condition.Condition;
032: import com.nabhinc.condition.ConditionFactory;
033: import com.nabhinc.ws.core.WebServiceException;
034: import com.nabhinc.ws.core.WebServiceSecurityException;
035: import com.nabhinc.ws.server.Interceptor;
036: import com.nabhinc.ws.server.InterceptorChain;
037: import com.nabhinc.ws.server.InterceptorUnavailableException;
038: import com.nabhinc.ws.server.RequestInfo;
039: import com.nabhinc.ws.server.ServerObjectImpl;
040:
041: /**
042: * Provides a access control mechanism for Web services via <code>Condition</code>
043: * classes defined in <code>com.nabhinc.condition</code> package. The acess control
044: * condition is specified as the value of "condition" init parameter. The condition
045: * is specified using a XML format described in Stringbeans portal configuration
046: * reference manual.
047: *
048: * @author Padmanabh Dabke
049: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
050: */
051: public class PreconditionAccessController extends ServerObjectImpl
052: implements Interceptor {
053: private transient Condition pacPrecondition = null;
054: private String pacConditionStr = null;
055:
056: public String getCondition() {
057: return this .pacConditionStr;
058: }
059:
060: public void setCondition(String cond) throws WebServiceException {
061: Element root = null;
062:
063: try {
064:
065: DocumentBuilderFactory docFactory = DocumentBuilderFactory
066: .newInstance();
067: docFactory.setNamespaceAware(true);
068: DocumentBuilder docBuilder = docFactory
069: .newDocumentBuilder();
070: Document doc = docBuilder.parse(new InputSource(
071: new StringReader(cond)));
072: root = doc.getDocumentElement();
073: pacPrecondition = ConditionFactory.create(root);
074: pacConditionStr = cond;
075: } catch (Exception ex) {
076: throw new WebServiceException("Failed to parse condition.",
077: ex);
078: }
079:
080: }
081:
082: public void intercept(RequestInfo reqInfo, InterceptorChain chain)
083: throws WebServiceException,
084: InterceptorUnavailableException, IOException {
085: boolean isSatisfied = false;
086: try {
087: isSatisfied = pacPrecondition.isSatisfied(
088: reqInfo.webServiceRequest,
089: reqInfo.serviceInfo.webService);
090: } catch (Exception ex) {
091: throw new InterceptorUnavailableException(ex);
092: }
093:
094: if (isSatisfied) {
095: chain.doIntercept(reqInfo);
096: } else {
097: throw new WebServiceSecurityException("AccessDenied");
098:
099: }
100:
101: }
102:
103: }
|