001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.servlet.HttpHeaders;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.Http;
022    import com.liferay.portal.kernel.util.HttpUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.xml.Attribute;
026    import com.liferay.portal.kernel.xml.Document;
027    import com.liferay.portal.kernel.xml.DocumentException;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.kernel.xml.Namespace;
030    import com.liferay.portal.kernel.xml.QName;
031    import com.liferay.portal.kernel.xml.SAXReaderUtil;
032    
033    import java.io.IOException;
034    
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class GHelperUtil {
041    
042            public static final String APPS_URL =
043                    "https://apps-apis.google.com/a/feeds";
044    
045            public static Element addAppsProperty(
046                    Element parentElement, String name, String value) {
047    
048                    Element element = parentElement.addElement("apps:property");
049    
050                    element.addAttribute("name", name);
051                    element.addAttribute("value", value);
052    
053                    return element;
054            }
055    
056            public static Element addAtomCategory(Element parentElement, String type) {
057                    Element element = parentElement.addElement("atom:category");
058    
059                    element.addAttribute("scheme", "http://schemas.google.com/g/2005#kind");
060                    element.addAttribute(
061                            "term", "http://schemas.google.com/apps/2006#" + type);
062    
063                    return element;
064            }
065    
066            public static Element addAtomEntry(Document document) {
067                    Element element = document.addElement("atom:entry");
068    
069                    element.add(getAppsNamespace());
070                    element.add(getAtomNamespace());
071    
072                    return element;
073            }
074    
075            public static Namespace getAppsNamespace() {
076                    return SAXReaderUtil.createNamespace(_APPS_PREFIX, _APPS_URI);
077            }
078    
079            public static QName getAppsQName(String localName) {
080                    return SAXReaderUtil.createQName(localName, getAppsNamespace());
081            }
082    
083            public static Namespace getAtomNamespace() {
084                    return SAXReaderUtil.createNamespace(_ATOM_PREFIX, _ATOM_URI);
085            }
086    
087            public static QName getAtomQName(String localName) {
088                    return SAXReaderUtil.createQName(localName, getAtomNamespace());
089            }
090    
091            public static Document getDocument(
092                            GAuthenticator gAuthenticator, String url)
093                    throws GoogleAppsException {
094    
095                    try {
096                            Http.Options options = _getOptions(gAuthenticator);
097    
098                            options.setLocation(url);
099    
100                            String xml = HttpUtil.URLtoString(options);
101    
102                            return SAXReaderUtil.read(new UnsyncStringReader(xml));
103                    }
104                    catch (DocumentException de) {
105                            throw new GoogleAppsException(de);
106                    }
107                    catch (IOException ioe) {
108                            throw new GoogleAppsException(ioe);
109                    }
110            }
111    
112            public static String getErrorMessage(Document document) {
113                    Element rootElement = document.getRootElement();
114    
115                    Element errorElement = rootElement.element("error");
116    
117                    List<Attribute> attributes = errorElement.attributes();
118    
119                    StringBundler sb = new StringBundler(attributes.size() * 4 + 1);
120    
121                    sb.append(StringPool.OPEN_CURLY_BRACE);
122    
123                    for (int i = 0; i < attributes.size(); i++) {
124                            Attribute attribute = attributes.get(i);
125    
126                            sb.append(attribute.getName());
127                            sb.append(StringPool.EQUAL);
128                            sb.append(attribute.getValue());
129    
130                            if ((i + 1) <= attributes.size()) {
131                                    sb.append(StringPool.COMMA_AND_SPACE);
132                            }
133                    }
134    
135                    sb.append(StringPool.CLOSE_CURLY_BRACE);
136    
137                    return sb.toString();
138            }
139    
140            public static boolean hasError(Document document) {
141                    Element rootElement = document.getRootElement();
142    
143                    if (rootElement.element("error") != null) {
144                            return true;
145                    }
146                    else {
147                            return false;
148                    }
149            }
150    
151            public static void submitAdd(
152                            GAuthenticator gAuthenticator, String url, Document document)
153                    throws GoogleAppsException {
154    
155                    try {
156                            Http.Options options = _getOptions(gAuthenticator);
157    
158                            options.setBody(
159                                    document.formattedString(), ContentTypes.APPLICATION_ATOM_XML,
160                                    StringPool.UTF8);
161                            options.setLocation(url);
162                            options.setPost(true);
163    
164                            HttpUtil.URLtoString(options);
165                    }
166                    catch (IOException ioe) {
167                            throw new GoogleAppsException(ioe);
168                    }
169            }
170    
171            public static void submitDelete(GAuthenticator gAuthenticator, String url)
172                    throws GoogleAppsException {
173    
174                    try {
175                            Http.Options options = _getOptions(gAuthenticator);
176    
177                            options.setDelete(true);
178                            options.setLocation(url);
179    
180                            HttpUtil.URLtoString(options);
181                    }
182                    catch (IOException ioe) {
183                            throw new GoogleAppsException(ioe);
184                    }
185            }
186    
187            public static void submitUpdate(
188                            GAuthenticator gAuthenticator, String url, Document document)
189                    throws GoogleAppsException {
190    
191                    try {
192                            Http.Options options = _getOptions(gAuthenticator);
193    
194                            options.setBody(
195                                    document.formattedString(), ContentTypes.APPLICATION_ATOM_XML,
196                                    StringPool.UTF8);
197                            options.setLocation(url);
198                            options.setPut(true);
199    
200                            HttpUtil.URLtoString(options);
201                    }
202                    catch (IOException ioe) {
203                            throw new GoogleAppsException(ioe);
204                    }
205            }
206    
207            private static Http.Options _getOptions(GAuthenticator gAuthenticator) {
208                    Http.Options options = new Http.Options();
209    
210                    options.addHeader(
211                            HttpHeaders.AUTHORIZATION,
212                            "GoogleLogin auth=" + gAuthenticator.getAuthenticationToken());
213    
214                    return options;
215            }
216    
217            private static final String _APPS_PREFIX = "apps";
218    
219            private static final String _APPS_URI =
220                    "http://schemas.google.com/apps/2006";
221    
222            private static final String _ATOM_PREFIX = "atom";
223    
224            private static final String _ATOM_URI = "http://www.w3.org/2005/Atom";
225    
226    }