001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.widget.netvibes.provider;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.net.MalformedURLException;
028: import java.net.SocketTimeoutException;
029: import java.net.URL;
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.List;
033:
034: import org.jboss.portal.common.io.IOTools;
035: import org.jboss.portal.common.net.URLTools;
036: import org.jboss.portal.widget.WidgetQuery;
037: import org.jboss.portal.widget.exceptions.WidgetException;
038: import org.jboss.portal.widget.netvibes.directory.NVEcosystemFactory;
039: import org.jboss.portal.widget.netvibes.directory.NVQueryResultMetaData;
040: import org.jboss.xb.binding.JBossXBException;
041: import org.jboss.xb.binding.ObjectModelFactory;
042: import org.jboss.xb.binding.Unmarshaller;
043: import org.jboss.xb.binding.UnmarshallerFactory;
044:
045: /**
046: * @author <a href="mailto:emuckenh@redhat.com">Emanuel Muckenhuber</a>
047: * @version $Revision$
048: */
049: public class NetvibesQueryResultBuilder {
050:
051: /** . */
052: private static final org.jboss.logging.Logger log = org.jboss.logging.Logger
053: .getLogger(NetvibesQueryResultBuilder.class);
054:
055: /** . */
056: private final URL url;
057:
058: public NetvibesQueryResultBuilder(WidgetQuery query)
059: throws MalformedURLException {
060: if (query == null) {
061: throw new IllegalArgumentException(
062: "query must not be null.");
063: }
064: this .url = query.buildQueryURL();
065: }
066:
067: public NetvibesQueryResultBuilder(URL url) {
068: if (url == null) {
069: throw new IllegalArgumentException("url must not be null.");
070: }
071: this .url = url;
072: }
073:
074: public NetvibesQueryResult build(int connectionTimeout)
075: throws WidgetException {
076: try {
077: // Read fully the URL content first
078: byte[] bytes;
079: if ("http".equals(url.getProtocol())) {
080: bytes = URLTools.getContent(url, connectionTimeout,
081: connectionTimeout);
082: } else {
083:
084: InputStream in = url.openStream();
085: try {
086: bytes = IOTools.getBytes(in);
087: } finally {
088: IOTools.safeClose(in);
089: }
090: }
091:
092: //
093: if (bytes == null) {
094: throw new WidgetException("Cannot retrieve url: " + url);
095: }
096:
097: String string = new String(bytes);
098:
099: List data = null;
100:
101: Unmarshaller unmarshaller = UnmarshallerFactory
102: .newInstance().newUnmarshaller();
103:
104: // create an instance of ObjectModelFactory
105: ObjectModelFactory factory = new NVEcosystemFactory();
106:
107: // let the object model factory to create an instance of List and populate it with data from XML
108: data = (List) unmarshaller.unmarshal(
109: new ByteArrayInputStream(string.getBytes("UTF-8")),
110: factory, null);
111: List entries = new ArrayList();
112:
113: for (Iterator iterator = data.iterator(); iterator
114: .hasNext();) {
115: NVQueryResultMetaData meta = (NVQueryResultMetaData) iterator
116: .next();
117: NetvibesQueryResultEntry entry = new NetvibesQueryResultEntry(
118: meta);
119: entries.add(entry);
120: }
121: return new NetvibesQueryResult(entries);
122: } catch (SocketTimeoutException e) {
123: throw new WidgetException(
124: "Netvibes directory query failed due to timeout. Try to increase connectionTimeout initial parameter; ",
125: e);
126: } catch (JBossXBException e) {
127: throw new WidgetException(
128: "Failed to parse directory response.", e);
129: } catch (IOException e) {
130: throw new WidgetException(
131: "Failed to perform directory lookup.", e);
132: }
133: }
134: }
|