/*
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.identity.entitlement.mediator;
import java.lang.Exception;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseException;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.base.ServerConfiguration;
import org.wso2.carbon.utils.CarbonUtils;
public class EntitlementMediator extends AbstractMediator {
private static final Log log = LogFactory.getLog(EntitlementMediator.class);
private boolean remote = true;
private String remoteServiceUserName;
private String remoteServicePassword;
private String remoteServiceUrl;
private String callbackClass;
ConfigurationContext cfgCtx = null;
private String clientRepository = null;
private String axis2xml = null;
public final static String DEFAULT_CLIENT_REPO = "./samples/axis2Client/client_repo";
public final static String DEFAULT_AXIS2_XML = "./samples/axis2Client/client_repo/conf/axis2.xml";
public String getClientRepository() {
return clientRepository;
}
public void setClientRepository(String clientRepository) {
this.clientRepository = clientRepository;
}
public String getAxis2xml() {
return axis2xml;
}
public void setAxis2xml(String axis2xml) {
this.axis2xml = axis2xml;
}
public String getCallbackClass() {
return callbackClass;
}
public void setCallbackClass(String callbackClass) {
this.callbackClass = callbackClass;
}
public boolean isRemote() {
return remote;
}
public void setRemote(boolean remote) {
this.remote = remote;
}
public String getRemoteServiceUserName() {
return remoteServiceUserName;
}
public void setRemoteServiceUserName(String remoteServiceUserName) {
this.remoteServiceUserName = remoteServiceUserName;
}
public String getRemoteServicePassword() {
return remoteServicePassword;
}
public void setRemoteServicePassword(String remoteServicePassword) {
this.remoteServicePassword = remoteServicePassword;
}
public String getRemoteServiceUrl() {
return remoteServiceUrl;
}
public void setRemoteServiceUrl(String remoteServiceUrl) {
this.remoteServiceUrl = remoteServiceUrl;
}
/**
* {@inheritDoc}
*/
public boolean mediate(MessageContext synCtx) {
EntitlementServiceClient client = null;
String decision = null;
ConfigurationContext configContext = null;
String serverUrl = null;
org.apache.axis2.context.MessageContext msgContext;
Axis2MessageContext axis2Msgcontext = null;
axis2Msgcontext = (Axis2MessageContext) synCtx;
msgContext = axis2Msgcontext.getAxis2MessageContext();
serverUrl = ServerConfiguration.getInstance().getFirstProperty(CarbonConstants.SERVER_URL);
String userName = null;
String serviceName = null;
String operationName = null;
String action = null;
EntitlementCallbackHandler callback = null;
String resourceName = null;
String[] env = null;
if (log.isDebugEnabled()) {
log.debug("Mediation for Entitlement started");
}
try {
if (callbackClass != null && callbackClass.trim().length() > 0) {
callback = getCallbackHandler(callbackClass);
} else {
callback = new UTEntitlementCallbackHandler();
}
userName = callback.getUserName(synCtx);
serviceName = callback.findServiceName(synCtx);
operationName = callback.findOperationName(synCtx);
action = callback.findAction(synCtx);
env = callback.findEnvironment(synCtx);
if (userName == null) {
log.error("User name not provided for the Entitlement mediator - can't proceed");
throw new SynapseException(
"User name not provided for the Entitlement mediator - can't proceed");
}
if (operationName != null) {
resourceName = serviceName + "/" + operationName;
} else {
resourceName = serviceName;
}
if (env == null) {
env = new String[0];
}
configContext = cfgCtx;
serverUrl = getServerURL(serverUrl, msgContext.getConfigurationContext());
client = new EntitlementServiceClient(remoteServiceUrl, configContext,
remoteServiceUserName, remoteServicePassword, serverUrl);
decision = client.getDecision(userName, resourceName, action, env);
if ("Permit".equals(decision)) {
return true;
} else {
if (log.isDebugEnabled()) {
log.debug("User not authorized to perform the action :" + decision);
}
}
} catch (Exception e) {
log.error("Error occured while evaluating the policy", e);
throw new SynapseException("Error occured while evaluating the policy");
}
throw new SynapseException("User not authorized to perform the action");
}
/**
*
* @param url
* @param config
* @param contextPath
* @return
*/
private static String getServerURL(String url, ConfigurationContext config) {
if (url.indexOf("${carbon.https.port}") != -1) {
String httpsPort = CarbonUtils.getTransportPort(config, "https") + "";
url = url.replace("${carbon.https.port}", httpsPort);
}
if (url.indexOf("${carbon.management.port}") != -1) {
String httpsPort = CarbonUtils.getTransportPort(config, "https") + "";
url = url.replace("${carbon.management.port}", httpsPort);
}
if (url.indexOf("${carbon.context}") != -1) {
// We need not to worry about context here - just need the server url for logging
url = url.replace("${carbon.context}", "");
}
return url;
}
private EntitlementCallbackHandler getCallbackHandler(String className) throws AxisFault {
try {
Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
return (EntitlementCallbackHandler) clazz.newInstance();
} catch (Exception e) {
log.error("Error occured while loading " + className, e);
}
return null;
}
public void init(SynapseEnvironment synEnv) {
try {
cfgCtx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(
clientRepository != null ? clientRepository : DEFAULT_CLIENT_REPO,
axis2xml != null ? axis2xml : DEFAULT_AXIS2_XML);
} catch (AxisFault e) {
String msg = "Error initializing callout mediator : " + e.getMessage();
log.error(msg, e);
throw new SynapseException(msg, e);
}
}
}