CookieManager is a simple utility for handling cookies when working @@ -88,7 +82,7 @@ public XmlaOlap4jCookieManager() { * or IOException will be thrown * @throws java.io.IOException Thrown if conn is not open. */ - public void storeCookies(URLConnection conn) throws IOException { + public void storeCookies(URLConnection conn){ // Determines the domain from where these cookies are being sent String domain = getDomainFromHost(conn.getURL().getHost()); @@ -164,7 +158,7 @@ public void storeCookies(URLConnection conn) throws IOException { * @param conn a java.net.URLConnection - must NOT be open, or IOException will be thrown * @throws java.io.IOException Thrown if conn has already been opened. */ - public void setCookies(URLConnection conn) throws IOException { + public void setCookies(URLConnection conn){ // Determines the domain and path to retrieve the appropriate cookies URL url = conn.getURL(); @@ -200,9 +194,8 @@ && isNotExpired((String) cookie.get(EXPIRES))) { } conn.setRequestProperty(COOKIE, cookieStringBuffer.toString()); } catch (java.lang.IllegalStateException ise) { - IOException ioe = new IOException( + throw new RuntimeException( "Illegal State! Cookies cannot be set on a URLConnection that is already connected. Only call setCookies(java.net.URLConnection) AFTER calling java.net.URLConnection.connect()."); - throw ioe; } } diff --git a/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jHttpProxy.java b/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jHttpProxy.java index 9c5f98c..c9ef89b 100644 --- a/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jHttpProxy.java +++ b/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jHttpProxy.java @@ -34,59 +34,66 @@ public class XmlaOlap4jHttpProxy extends XmlaOlap4jAbstractHttpProxy { @Override public byte[] getResponse(URL url, String request) - throws IOException + throws XmlaOlap4jProxyException { - // Open connection to manipulate the properties - URLConnection urlConnection = url.openConnection(); - urlConnection.setDoOutput(true); + try { + // Open connection to manipulate the properties + URLConnection urlConnection = url.openConnection(); + urlConnection.setDoOutput(true); - // Set headers - urlConnection.setRequestProperty( - "content-type", - "text/xml"); - urlConnection.setRequestProperty( - "User-Agent", - "Olap4j(" - .concat(XmlaOlap4jDriver.VERSION) - .concat(")")); - urlConnection.setRequestProperty( - "Accept", - "text/xml;q=1"); - urlConnection.setRequestProperty( - "Accept-Charset", - getEncodingCharsetName() - .concat(";q=1")); - - // Encode credentials for basic authentication - if (url.getUserInfo() != null) { - String encoding = - Base64.encodeBytes(url.getUserInfo().getBytes(), 0); + // Set headers urlConnection.setRequestProperty( - "Authorization", "Basic " + encoding); - } + "content-type", + "text/xml"); + urlConnection.setRequestProperty( + "User-Agent", + "Olap4j(" + .concat(XmlaOlap4jDriver.VERSION) + .concat(")")); + urlConnection.setRequestProperty( + "Accept", + "text/xml;q=1"); + urlConnection.setRequestProperty( + "Accept-Charset", + getEncodingCharsetName() + .concat(";q=1")); - // Set correct cookies - this.useCookies(urlConnection); + // Encode credentials for basic authentication + if (url.getUserInfo() != null) { + String encoding = + Base64.encodeBytes(url.getUserInfo().getBytes(), 0); + urlConnection.setRequestProperty( + "Authorization", "Basic " + encoding); + } - // Send data (i.e. POST). Use same encoding as specified in the - // header. - final String encoding = getEncodingCharsetName(); - urlConnection.getOutputStream().write(request.getBytes(encoding)); + // Set correct cookies + this.useCookies(urlConnection); - // Get the response, again assuming default encoding. - InputStream is = urlConnection.getInputStream(); - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buf = new byte[1024]; - int count; + // Send data (i.e. POST). Use same encoding as specified in the + // header. + final String encoding = getEncodingCharsetName(); + urlConnection.getOutputStream().write(request.getBytes(encoding)); - while ((count = is.read(buf)) > 0) { - baos.write(buf, 0, count); - } + // Get the response, again assuming default encoding. + InputStream is = urlConnection.getInputStream(); + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buf = new byte[1024]; + int count; - // Save the returned cookies for later use - this.saveCookies(urlConnection); + while ((count = is.read(buf)) > 0) { + baos.write(buf, 0, count); + } - return baos.toByteArray(); + // Save the returned cookies for later use + this.saveCookies(urlConnection); + + return baos.toByteArray(); + // All exceptions should be trapped here. + // The response will only be available here anyways. + } catch (Exception e) { + throw new XmlaOlap4jProxyException( + "This proxy encountered an exception while processing the query.",e); + } } @Override diff --git a/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jProxy.java b/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jProxy.java index f74a3a4..c47a275 100644 --- a/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jProxy.java +++ b/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jProxy.java @@ -23,10 +23,20 @@ public interface XmlaOlap4jProxy { * * @param url Target URL * @param request Request string - * @return Response - * @throws IOException + * @return Response The byte array that contains the whole response + * from the server. + * @throws IOException This exception declaration will be removed soon. + * Don't catch this. Catch XmlaOlap4jProxyException instead. + * @throws XmlaOlap4jProxyException If anything occurs during the + * request execution. */ - byte[] get(URL url, String request) throws IOException; + /* + * FIXME We will need to remove the IOException declaration because + * this type of error is linked to the proxy type. A wrapper + * class was created, but some proxies out there (MondrianInprocProxy...) + * still uses this. + */ + byte[] get(URL url, String request) throws XmlaOlap4jProxyException,IOException; /** * Submits a request for background execution. diff --git a/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jProxyException.java b/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jProxyException.java new file mode 100644 index 0000000..747ed75 --- /dev/null +++ b/src/org/olap4j/driver/xmla/proxy/XmlaOlap4jProxyException.java @@ -0,0 +1,25 @@ +/* +// $Id:$ +// This software is subject to the terms of the Common Public License +// Agreement, available at the following URL: +// http://www.opensource.org/licenses/cpl.html. +// Copyright (C) 2007-2007 Julian Hyde +// All Rights Reserved. +// You must accept the terms of that agreement to use this software. +*/ +package org.olap4j.driver.xmla.proxy; + +/** + * Gets thrown whenever an exception is encountered during the querying + * of an XmlaOlap4jProxy subclass. + * + * @author Luc Boudreau + * @version $Id:$ + */ +public class XmlaOlap4jProxyException extends Exception { + private static final long serialVersionUID = 1729906649527317997L; + public XmlaOlap4jProxyException(String message, Throwable cause) { + super(message, cause); + } +} +//End XmlaOlap4jProxyException.java \ No newline at end of file