package net.sourceforge.fenixedu.integration.client; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Properties; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; public class FenixClient { private static final String SERVER_URL; private static final String RETRIEVE_USER_INFORMATION_INTERFACE; private static final String USER_PERMISSION_CHECK_SERVICE_INTERFACE; private static final String SUCCESS_CODE = "SUCCESS"; // private static final String UNEXPECTED_ERROR_CODE = "UNEXPECTED_ERROR"; private static final String ENCODING = "UTF-8"; static { Properties properties = new Properties(); InputStream inputStream = FenixClient.class.getResourceAsStream("/fenixClient.properties"); try { properties.load(inputStream); SERVER_URL = properties.getProperty("serverUrl"); } catch (IOException e) { throw new RuntimeException(e); } finally { try { inputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } RETRIEVE_USER_INFORMATION_INTERFACE = SERVER_URL + "/external/retrieveUserInformation.do"; USER_PERMISSION_CHECK_SERVICE_INTERFACE = SERVER_URL + "/external/userPermissionCheck.do"; } public static UserInformation getUserInformation(String username) throws FenixClientException { List parameters = new ArrayList(); parameters.add(new NameValuePair("method", "getUserEmailAndUniqueUsername")); parameters.add(new NameValuePair("username", username)); String response = executeServiceRequest(RETRIEVE_USER_INFORMATION_INTERFACE, parameters); int indexOfFirstNewLine = response.indexOf("\n"); String responseCode = response.substring(0, indexOfFirstNewLine); String responseMessage = response.substring(indexOfFirstNewLine + 1); if (responseCode.equals(SUCCESS_CODE)) { String[] fieldNameValuePairs = responseMessage.split("&"); String email = null; String uniqueUsername = null; for (String fieldNameFieldValuePair : fieldNameValuePairs) { String[] fieldNameFieldValueArray = fieldNameFieldValuePair.split("="); String fieldName = fieldNameFieldValueArray[0]; String fieldValue; if (fieldNameFieldValueArray.length > 1) { try { fieldValue = URLDecoder.decode(fieldNameFieldValueArray[1], ENCODING); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } else { fieldValue = null; } if (fieldName.equals("email")) { email = fieldValue; } else if (fieldName.equals("uniqueUsername")) { uniqueUsername = fieldValue; } } UserInformation userInformation = new UserInformation(uniqueUsername, email); return userInformation; } else { throw new FenixClientException(response); } } public static boolean canUserAccessFile(String username, String bitstreamIdentification) throws FenixClientException { List parameters = new ArrayList(); parameters.add(new NameValuePair("method", "canUserAccessFile")); parameters.add(new NameValuePair("username", username)); parameters.add(new NameValuePair("dspaceBitstreamIdentification", bitstreamIdentification)); String response = executeServiceRequest(USER_PERMISSION_CHECK_SERVICE_INTERFACE, parameters); int indexOfFirstNewLine = response.indexOf("\n"); String responseCode = response.substring(0, indexOfFirstNewLine); String responseMessage = response.substring(indexOfFirstNewLine + 1); if (!responseCode.equals(SUCCESS_CODE)) { throw new FenixClientException(responseCode); } else { return Boolean.valueOf(responseMessage); } } private static String executeServiceRequest(String serviceInterfaceUrl, List parameters) throws FenixClientException { HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(serviceInterfaceUrl); Iterator parameterIterator = parameters.iterator(); while (parameterIterator.hasNext()) { NameValuePair parameter = parameterIterator.next(); postMethod.addParameter(parameter); } try { httpClient.executeMethod(postMethod); return postMethod.getResponseBodyAsString(); } catch (HttpException e) { throw new FenixClientException(e); } catch (IOException e) { throw new FenixClientException(e); } finally { postMethod.releaseConnection(); } } }