001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.googleapps;
016    
017    import com.liferay.portal.kernel.googleapps.GoogleAppsException;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.servlet.HttpHeaders;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.Http;
024    import com.liferay.portal.kernel.util.HttpUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.xml.Attribute;
028    import com.liferay.portal.kernel.xml.Document;
029    import com.liferay.portal.kernel.xml.DocumentException;
030    import com.liferay.portal.kernel.xml.Element;
031    import com.liferay.portal.kernel.xml.Namespace;
032    import com.liferay.portal.kernel.xml.QName;
033    import com.liferay.portal.kernel.xml.SAXReaderUtil;
034    
035    import java.io.IOException;
036    
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class GHelperUtil {
043    
044            public static final String APPS_URL =
045                    "https://apps-apis.google.com/a/feeds";
046    
047            public static Element addAppsProperty(
048                    Element parentElement, String name, String value) {
049    
050                    Element element = parentElement.addElement("apps:property");
051    
052                    element.addAttribute("name", name);
053                    element.addAttribute("value", value);
054    
055                    return element;
056            }
057    
058            public static Element addAtomCategory(Element parentElement, String type) {
059                    Element element = parentElement.addElement("atom:category");
060    
061                    element.addAttribute("scheme", "http://schemas.google.com/g/2005#kind");
062                    element.addAttribute(
063                            "term", "http://schemas.google.com/apps/2006#" + type);
064    
065                    return element;
066            }
067    
068            public static Element addAtomEntry(Document document) {
069                    Element element = document.addElement("atom:entry");
070    
071                    element.add(getAppsNamespace());
072                    element.add(getAtomNamespace());
073    
074                    return element;
075            }
076    
077            public static Namespace getAppsNamespace() {
078                    return SAXReaderUtil.createNamespace(_APPS_PREFIX, _APPS_URI);
079            }
080    
081            public static QName getAppsQName(String localName) {
082                    return SAXReaderUtil.createQName(localName, getAppsNamespace());
083            }
084    
085            public static Namespace getAtomNamespace() {
086                    return SAXReaderUtil.createNamespace(_ATOM_PREFIX, _ATOM_URI);
087            }
088    
089            public static QName getAtomQName(String localName) {
090                    return SAXReaderUtil.createQName(localName, getAtomNamespace());
091            }
092    
093            public static Document getDocument(
094                            GAuthenticator gAuthenticator, String url)
095                    throws GoogleAppsException {
096    
097                    try {
098                            if (_log.isInfoEnabled()) {
099                                    _log.info("getDocument request " + url);
100                            }
101    
102                            Http.Options options = _getOptions(gAuthenticator);
103    
104                            options.setLocation(url);
105    
106                            String xml = HttpUtil.URLtoString(options);
107    
108                            if (_log.isInfoEnabled()) {
109                                    _log.info("getDocument response " + xml);
110                            }
111    
112                            return SAXReaderUtil.read(new UnsyncStringReader(xml));
113                    }
114                    catch (DocumentException de) {
115                            throw new GoogleAppsException(de);
116                    }
117                    catch (IOException ioe) {
118                            throw new GoogleAppsException(ioe);
119                    }
120            }
121    
122            public static String getErrorMessage(Document document) {
123                    Element rootElement = document.getRootElement();
124    
125                    Element errorElement = rootElement.element("error");
126    
127                    List<Attribute> attributes = errorElement.attributes();
128    
129                    StringBundler sb = new StringBundler(attributes.size() * 4 + 1);
130    
131                    sb.append(StringPool.OPEN_CURLY_BRACE);
132    
133                    for (int i = 0; i < attributes.size(); i++) {
134                            Attribute attribute = attributes.get(i);
135    
136                            sb.append(attribute.getName());
137                            sb.append(StringPool.EQUAL);
138                            sb.append(attribute.getValue());
139    
140                            if ((i + 1) <= attributes.size()) {
141                                    sb.append(StringPool.COMMA_AND_SPACE);
142                            }
143                    }
144    
145                    sb.append(StringPool.CLOSE_CURLY_BRACE);
146    
147                    return sb.toString();
148            }
149    
150            public static boolean hasError(Document document) {
151                    Element rootElement = document.getRootElement();
152    
153                    if (rootElement.element("error") != null) {
154                            return true;
155                    }
156                    else {
157                            return false;
158                    }
159            }
160    
161            public static void submitAdd(
162                            GAuthenticator gAuthenticator, String url, Document document)
163                    throws GoogleAppsException {
164    
165                    try {
166                            if (_log.isInfoEnabled()) {
167                                    _log.info("submitAdd request " + url);
168                            }
169    
170                            Http.Options options = _getOptions(gAuthenticator);
171    
172                            options.setBody(
173                                    document.formattedString(), ContentTypes.APPLICATION_ATOM_XML,
174                                    StringPool.UTF8);
175                            options.setLocation(url);
176                            options.setPost(true);
177    
178                            String response = HttpUtil.URLtoString(options);
179    
180                            if (_log.isInfoEnabled()) {
181                                    _log.info("submitAdd response " + response);
182                            }
183                    }
184                    catch (IOException ioe) {
185                            throw new GoogleAppsException(ioe);
186                    }
187            }
188    
189            public static void submitDelete(GAuthenticator gAuthenticator, String url)
190                    throws GoogleAppsException {
191    
192                    try {
193                            if (_log.isInfoEnabled()) {
194                                    _log.info("submitDelete request " + url);
195                            }
196    
197                            Http.Options options = _getOptions(gAuthenticator);
198    
199                            options.setDelete(true);
200                            options.setLocation(url);
201    
202                            String response = HttpUtil.URLtoString(options);
203    
204                            if (_log.isInfoEnabled()) {
205                                    _log.info("submitDelete response " + response);
206                            }
207                    }
208                    catch (IOException ioe) {
209                            throw new GoogleAppsException(ioe);
210                    }
211            }
212    
213            public static void submitUpdate(
214                            GAuthenticator gAuthenticator, String url, Document document)
215                    throws GoogleAppsException {
216    
217                    try {
218                            if (_log.isInfoEnabled()) {
219                                    _log.info("submitUpdate request " + url);
220                            }
221    
222                            Http.Options options = _getOptions(gAuthenticator);
223    
224                            options.setBody(
225                                    document.formattedString(), ContentTypes.APPLICATION_ATOM_XML,
226                                    StringPool.UTF8);
227                            options.setLocation(url);
228                            options.setPut(true);
229    
230                            String response = HttpUtil.URLtoString(options);
231    
232                            if (_log.isInfoEnabled()) {
233                                    _log.info("submitUpdate response " + response);
234                            }
235                    }
236                    catch (IOException ioe) {
237                            throw new GoogleAppsException(ioe);
238                    }
239            }
240    
241            private static Http.Options _getOptions(GAuthenticator gAuthenticator) {
242                    Http.Options options = new Http.Options();
243    
244                    options.addHeader(
245                            HttpHeaders.AUTHORIZATION,
246                            "GoogleLogin auth=" + gAuthenticator.getAuthenticationToken());
247    
248                    return options;
249            }
250    
251            private static final String _APPS_PREFIX = "apps";
252    
253            private static final String _APPS_URI =
254                    "http://schemas.google.com/apps/2006";
255    
256            private static final String _ATOM_PREFIX = "atom";
257    
258            private static final String _ATOM_URI = "http://www.w3.org/2005/Atom";
259    
260            private static Log _log = LogFactoryUtil.getLog(GHelperUtil.class);
261    
262    }