/*
* 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.ui.client;
import java.lang.Exception;
import java.rmi.RemoteException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.entitlement.stub.EntitlementPolicyAdminServiceIdentityException;
import org.wso2.carbon.identity.entitlement.stub.EntitlementPolicyAdminServiceStub;
import org.wso2.carbon.identity.entitlement.stub.dto.PaginatedPolicySetDTO;
import org.wso2.carbon.identity.entitlement.stub.dto.PolicyDTO;
public class EntitlementPolicyAdminServiceClient {
private EntitlementPolicyAdminServiceStub stub;
private static final Log log = LogFactory.getLog(EntitlementPolicyAdminServiceClient.class);
/**
* Instantiates EntitlementServiceClient
*
* @param cookie For session management
* @param backendServerURL URL of the back end server where EntitlementPolicyAdminService is
* running.
* @param configCtx ConfigurationContext
* @throws org.apache.axis2.AxisFault
*/
public EntitlementPolicyAdminServiceClient(String cookie, String backendServerURL,
ConfigurationContext configCtx) throws AxisFault {
String serviceURL = backendServerURL + "EntitlementPolicyAdminService";
stub = new EntitlementPolicyAdminServiceStub(configCtx, serviceURL);
ServiceClient client = stub._getServiceClient();
Options option = client.getOptions();
option.setManageSession(true);
option.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, cookie);
}
/**
*
* @param pageNumber
* @return PaginatedPolicySetDTO object containing the number of pages and the set of policies that reside in the
* given page.
* @throws AxisFault
*/
public PaginatedPolicySetDTO getAllPolicies(String policyTypeFilter,String policySearchString,
int pageNumber) throws AxisFault {
try {
return stub.getAllPolicies(policyTypeFilter, policySearchString, pageNumber);
} catch (Exception e) {
String message = "Error while loading all policies from backend service";
handleException(message, e);
}
PaginatedPolicySetDTO paginatedPolicySetDTO = new PaginatedPolicySetDTO();
paginatedPolicySetDTO.setPolicySet(new PolicyDTO[0]);
return paginatedPolicySetDTO;
}
/**
*
* @param policyId
* @return
* @throws AxisFault
*/
public PolicyDTO getPolicy(String policyId) throws AxisFault {
PolicyDTO dto = null;
try {
dto = stub.getPolicy(policyId);
dto.setPolicy(dto.getPolicy().trim().replaceAll("><", ">\n<"));
} catch (Exception e) {
String message = "Error while loading the policy from backend service";
handleException(message, e);
}
return dto;
}
/**
*
* @param policy
* @throws AxisFault
*/
public void removePolicy(PolicyDTO policy) throws AxisFault {
try {
stub.removePolicy(policy);
} catch (Exception e) {
String message = "Error while removing the policy from backend service";
handleException(message, e);
}
}
/**
*
* @param policy
* @throws AxisFault
*/
public void updatePolicy(PolicyDTO policy) throws AxisFault {
try {
policy.setPolicy(policy.getPolicy().trim().replaceAll(">\\s+<", "><"));
stub.updatePolicy(policy);
} catch (Exception e) {
String message = "Error while updating the policy at the backend service";
handleException(message, e);
}
}
/**
*
* @param policy
* @throws AxisFault
*/
public void addPolicy(PolicyDTO policy) throws AxisFault {
try {
policy.setPolicy(policy.getPolicy().trim().replaceAll(">\\s+<", "><"));
stub.addPolicy(policy);
} catch (Exception e) {
String message = e.getMessage();
handleException(message, e);
}
}
/**
*
* @param request
* @throws AxisFault
*/
public void importPolicy(HttpServletRequest request) throws AxisFault {
PolicyDTO dto = null;
try {
if (ServletFileUpload.isMultipartContent(request)) {
List items = parseRequest(new ServletRequestContext(request));
byte[] content = null;
String fileName = null;
for (Object item : items) {
DiskFileItem diskFileItem = (DiskFileItem) item;
String name = diskFileItem.getFieldName();
if (name.equals("policyFromFileSystem")) {
content = diskFileItem.get();
fileName = diskFileItem.getName();
int index = fileName.lastIndexOf("\\");
fileName = fileName.substring(index + 1);
}
}
dto = new PolicyDTO();
dto.setPolicy(new String(content));
dto.setPolicy(dto.getPolicy().trim().replaceAll(">\\s+<", "><"));
stub.addPolicy(dto);
} else {
throw new Exception("Error while importing the policy at the backend service");
}
} catch (Exception e) {
String message = e.getMessage();
handleException(message, e);
}
}
/**
* Import XACML policy from registry
* @param policyRegistryPath registry path
* @throws AxisFault
*/
public void importPolicyFromRegistry(String policyRegistryPath) throws AxisFault {
try {
stub.importPolicyFromRegistry(policyRegistryPath);
} catch (Exception e) {
String message = e.getMessage();
handleException(message, e);
}
}
/**
* Clears the decision cache maintained by the PDP.
* @throws AxisFault
*/
public void clearDecisionCache() throws AxisFault {
try {
stub.clearDecisionCache();
} catch (Exception e) {
String message = e.getMessage();
handleException(message, e);
}
}
/**
* Pre-defined Attributes related with XACML Policy is fetched from registry.
* @param resourceName Resource Name of the Registry
* @return
* @throws org.apache.axis2.AxisFault
*/
public String [] getEntitlementPolicyDataFromRegistry (String resourceName) throws AxisFault {
try {
return stub.getEntitlementPolicyDataFromRegistry(resourceName);
} catch (Exception e) {
String message = e.getMessage();
handleException(message, e);
}
return null;
}
/**
* Returns the list of policy set ids available in PDP
* @return list of policy set ids
* @throws AxisFault
*/
public String[] getAllPolicyIds() throws AxisFault {
try {
return stub.getAllPolicyIds();
} catch (Exception e) {
String message = e.getMessage();
handleException(message, e);
}
return null;
}
/**
* Get globally defined policy combining algorithm
* @return policy combining algorithm as a String
* @throws AxisFault
*/
public String getGlobalPolicyAlgorithm() throws AxisFault {
try {
return stub.getGlobalPolicyAlgorithm();
} catch (Exception e) {
handleException(e.getMessage(), e);
}
return null;
}
/**
* Set policy combining algorithm globally
* @param policyAlgorithm policy combining algorithm as a String
* @throws AxisFault
*/
public void setGlobalPolicyAlgorithm(String policyAlgorithm) throws AxisFault {
try {
stub.setGlobalPolicyAlgorithm(policyAlgorithm);
} catch (Exception e) {
handleException(e.getMessage(), e);
}
}
/**
*
* @param requestContext
* @return
* @throws FileUploadException
*/
private List parseRequest(ServletRequestContext requestContext) throws FileUploadException {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
return upload.parseRequest(requestContext);
}
/**
* Logs and wraps the given exception.
*
* @param msg Error message
* @param e Exception
* @throws AxisFault
*/
private void handleException(String msg, Exception e) throws AxisFault {
log.error(msg, e);
throw new AxisFault(msg, e);
}
}