/** * The contents of this file are subject to the OpenMRS Public License * Version 1.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://license.openmrs.org * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * Copyright (C) OpenMRS, LLC. All Rights Reserved. */ package org.eclipse.datatools.connectivity.oda.openmrs.impl; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Properties; import org.eclipse.datatools.connectivity.oda.IConnection; import org.eclipse.datatools.connectivity.oda.IDataSetMetaData; import org.eclipse.datatools.connectivity.oda.IQuery; import org.eclipse.datatools.connectivity.oda.OdaException; /** * Implementation class of IConnection for an ODA runtime driver. */ public class Connection implements IConnection { private boolean m_isOpen = false; private String path, user, pass; private HttpURLConnection urlconn; private static final String urlend = "moduleServlet/logicws/api/", xmlstart = ""; /* * @see org.eclipse.datatools.connectivity.oda.IConnection#open(java.util.Properties) */ public void open(Properties connProperties) throws OdaException { // Check for null properties if (connProperties == null) throw new OdaException("No Connection Properties Provided."); // Set global path, username, and password path = connProperties.getProperty("PATH"); user = connProperties.getProperty("USERNAME"); pass = connProperties.getProperty("PASSWORD"); // Make sure there is a slash at the end of the path if (!path.endsWith("/")) path += "/"; path += urlend; // Check for user/pass and URL correctness checkProperties(); // Check that the API is working as expected and is available if (!canAccessAPI()) throw new OdaException( "Expected API functions do not respond correctly."); // Mark the connection as open m_isOpen = true; } /* * @see org.eclipse.datatools.connectivity.oda.IConnection#setAppContext(java.lang.Object) */ public void setAppContext(Object context) throws OdaException { // do nothing; assumes no support for pass-through context } /* * @see org.eclipse.datatools.connectivity.oda.IConnection#close() */ public void close() throws OdaException { // Remove pointers to these strings and connections path = null; user = null; pass = null; urlconn = null; // Mark the connection as closed m_isOpen = false; } /* * @see org.eclipse.datatools.connectivity.oda.IConnection#isOpen() */ public boolean isOpen() throws OdaException { return m_isOpen; } /* * @see org.eclipse.datatools.connectivity.oda.IConnection#getMetaData(java.lang.String) */ public IDataSetMetaData getMetaData(String dataSetType) throws OdaException { // assumes that this driver supports only one type of data set, // ignores the specified dataSetType return new DataSetMetaData(this); } /* * @see org.eclipse.datatools.connectivity.oda.IConnection#newQuery(java.lang.String) */ public IQuery newQuery(String dataSetType) throws OdaException { // assumes that this driver supports only one type of data set, // ignores the specified dataSetType return new Query(path, user, pass); } /* * @see org.eclipse.datatools.connectivity.oda.IConnection#getMaxQueries() */ public int getMaxQueries() throws OdaException { return 0; // no limit } /* * @see org.eclipse.datatools.connectivity.oda.IConnection#commit() */ public void commit() throws OdaException { // do nothing; assumes no transaction support needed } /* * @see org.eclipse.datatools.connectivity.oda.IConnection#rollback() */ public void rollback() throws OdaException { // do nothing; assumes no transaction support needed } /** * Sets the uRL conn. * * @param url the new uRL conn * * @throws IOException Signals that an I/O exception has occurred. */ private void setURLConn(URL url) throws IOException { urlconn = (HttpURLConnection) url.openConnection(); urlconn.setRequestProperty("Authorization", "Basic " + Base64.encodeBytes((user + ":" + pass).getBytes())); urlconn.setRequestMethod("POST"); urlconn.connect(); } /** * Should be in format: http(s)://server(:port)(/openmrs)(/) * * @throws OdaException the oda exception */ private void checkProperties() throws OdaException { try { // create URL and Connection setURLConn(new URL(path)); // Test for authentication errors if (urlconn.getResponseCode() == 401) throw new OdaException( "OpenMRS Server cannot be reached: Incorrect User/Pass."); // Test for 404 file not found errors if (urlconn.getResponseCode() == 404) throw new OdaException( "OpenMRS Server cannot be reached: URL is incorrect"); // Clean up urlconn.disconnect(); } catch (MalformedURLException e) { throw new OdaException( "OpenMRS Server cannot be reached: Malformed URL."); } catch (IOException e) { throw new OdaException("OpenMRS Server cannot be reached: " + e); } } /** * Can access api. * * @return true, if successful * * @throws OdaException the oda exception */ private boolean canAccessAPI() throws OdaException { try { InputStream contents; BufferedReader in; // check access to "getFilters" setURLConn(new URL(path + "getFilters")); contents = urlconn.getInputStream(); in = new BufferedReader(new InputStreamReader(contents)); if (!in.readLine().startsWith(xmlstart + "")) { urlconn.disconnect(); return false; } // check access to "getEntities" setURLConn(new URL(path + "getTokenTags")); contents = urlconn.getInputStream(); in = new BufferedReader(new InputStreamReader(contents)); if (!in.readLine().startsWith(xmlstart + "")) { urlconn.disconnect(); return false; } // check access to "getTokens" setURLConn(new URL(path + "getTokens")); contents = urlconn.getInputStream(); in = new BufferedReader(new InputStreamReader(contents)); if (!in.readLine().startsWith(xmlstart + "")) { urlconn.disconnect(); return false; } // check access to "getData" setURLConn(new URL(path + "getData")); contents = urlconn.getInputStream(); in = new BufferedReader(new InputStreamReader(contents)); if (!in.readLine().equals(xmlstart + "")) { urlconn.disconnect(); return false; } } catch (IOException e) { throw new OdaException(e); // return false; } // Clean up urlconn.disconnect(); // No errors, so return true return true; } }