001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 *     Nelson Silva
017 */
018package org.nuxeo.ecm.liveconnect.google.drive;
019
020import java.io.IOException;
021import java.util.Locale;
022
023import javax.faces.application.Application;
024import javax.faces.component.NamingContainer;
025import javax.faces.component.UIComponent;
026import javax.faces.component.UIInput;
027import javax.faces.component.html.HtmlInputText;
028import javax.faces.context.FacesContext;
029import javax.faces.context.ResponseWriter;
030import javax.servlet.http.HttpServletRequest;
031
032import org.apache.commons.lang.StringUtils;
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035import org.nuxeo.common.utils.i18n.I18NUtils;
036import org.nuxeo.ecm.core.api.Blob;
037import org.nuxeo.ecm.core.api.DocumentModelList;
038import org.nuxeo.ecm.core.api.NuxeoException;
039import org.nuxeo.ecm.core.blob.BlobManager;
040import org.nuxeo.ecm.liveconnect.google.drive.GoogleDriveBlobProvider.FileInfo;
041import org.nuxeo.ecm.platform.ui.web.component.file.InputFileChoice;
042import org.nuxeo.ecm.platform.ui.web.component.file.InputFileInfo;
043import org.nuxeo.ecm.platform.ui.web.component.file.JSFBlobUploader;
044import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
045import org.nuxeo.runtime.api.Framework;
046
047import com.google.api.client.auth.oauth2.Credential;
048
049/**
050 * JSF Blob Upload based on Google Drive blobs.
051 *
052 * @since 7.3
053 */
054public class GoogleDriveBlobUploader implements JSFBlobUploader {
055
056    private static final Log log = LogFactory.getLog(GoogleDriveBlobUploader.class);
057
058    public static final String UPLOAD_GOOGLE_DRIVE_FACET_NAME = "uploadGoogleDrive";
059
060    // restrict sign-in to accounts at this domain
061    public static final String GOOGLE_DOMAIN_PROP = "nuxeo.google.domain";
062
063    protected String clientId;
064
065    public GoogleDriveBlobUploader() {
066        try {
067            getGoogleDriveBlobProvider();
068        } catch (NuxeoException e) {
069            // this exception is caught by JSFBlobUploaderDescriptor.getJSFBlobUploader
070            // to mean that the uploader is not available because badly configured
071            throw new IllegalStateException(e);
072        }
073    }
074
075    @Override
076    public String getChoice() {
077        return InputFileChoice.UPLOAD + "GoogleDrive";
078    }
079
080    @Override
081    public void hookSubComponent(UIInput parent) {
082        Application app = FacesContext.getCurrentInstance().getApplication();
083        ComponentUtils.initiateSubComponent(parent, UPLOAD_GOOGLE_DRIVE_FACET_NAME,
084                app.createComponent(HtmlInputText.COMPONENT_TYPE));
085
086    }
087
088    // Needs supporting JavaScript code for nuxeo.utils.pickFromGoogleDrive defined in googleclient.js.
089    @Override
090    public void encodeBeginUpload(UIInput parent, FacesContext context, String onClick) throws IOException {
091        UIComponent facet = parent.getFacet(UPLOAD_GOOGLE_DRIVE_FACET_NAME);
092        if (!(facet instanceof HtmlInputText)) {
093            return;
094        }
095        HtmlInputText inputText = (HtmlInputText) facet;
096
097        // not ours to close
098        @SuppressWarnings("resource")
099        ResponseWriter writer = context.getResponseWriter();
100
101        String inputId = facet.getClientId(context);
102        String prefix = parent.getClientId(context) + NamingContainer.SEPARATOR_CHAR;
103        String pickId = prefix + "GoogleDrivePickMsg";
104        String authId = prefix + "GoogleDriveAuthMsg";
105        String infoId = prefix + "GoogleDriveInfo";
106        String authorizationUrl = hasServiceAccount() ? "" : getOAuthAuthorizationUrl();
107        Locale locale = context.getViewRoot().getLocale();
108        String message;
109        boolean isProviderAvailable = getGoogleDriveBlobProvider().getOAuth2Provider().isProviderAvailable();
110
111        writer.startElement("button", parent);
112        writer.writeAttribute("type", "button", null);
113        writer.writeAttribute("class", "button GoogleDrivePickerButton", null);
114
115        // only add onclick event to button if oauth service provider is available
116        // this prevents users from using the picker if some configuration is missing
117        if (isProviderAvailable) {
118            // TODO pass existing access token
119            String onButtonClick = onClick
120                + ";"
121                + String.format("new nuxeo.utils.GoogleDrivePicker('%s','%s','%s','%s','%s','%s', '%s')",
122                getClientId(), pickId, authId, inputId, infoId, getGoogleDomain(), authorizationUrl);
123            writer.writeAttribute("onclick", onButtonClick, null);
124        }
125
126        writer.startElement("span", parent);
127        writer.writeAttribute("id", pickId, null);
128        message = I18NUtils.getMessageString("messages", "label.inputFile.googleDriveUploadPicker", null, locale);
129        writer.write(message);
130        writer.endElement("span");
131
132        writer.startElement("span", parent);
133        writer.writeAttribute("id", authId, null);
134        writer.writeAttribute("style", "display:none", null); // hidden
135        message = I18NUtils.getMessageString("messages", "label.inputFile.authenticate", null, locale);
136        writer.write(message);
137        writer.endElement("span");
138
139        writer.endElement("button");
140
141        if (isProviderAvailable) {
142            writer.write(ComponentUtils.WHITE_SPACE_CHARACTER);
143            writer.startElement("span", parent);
144            writer.writeAttribute("id", infoId, null);
145            message = I18NUtils.getMessageString("messages", "error.inputFile.noFileSelected", null, locale);
146            writer.write(message);
147            writer.endElement("span");
148        } else {
149            // if oauth service provider not properly setup, add warning message
150            writer.startElement("span", parent);
151            writer.writeAttribute("class", "processMessage completeWarning", null);
152            writer.writeAttribute("style", "margin: 0 0 .5em 0; font-size: 11px; background-position-y: 0.6em", null);
153            message = I18NUtils.getMessageString("messages", "error.googledrive.providerUnavailable", null, locale);
154            writer.write(message);
155            writer.endElement("span");
156        }
157
158        inputText.setLocalValueSet(false);
159        inputText.setStyle("display:none"); // hidden
160        ComponentUtils.encodeComponent(context, inputText);
161    }
162
163    @Override
164    public void validateUpload(UIInput parent, FacesContext context, InputFileInfo submitted) {
165        UIComponent facet = parent.getFacet(UPLOAD_GOOGLE_DRIVE_FACET_NAME);
166        if (!(facet instanceof HtmlInputText)) {
167            return;
168        }
169        HtmlInputText inputText = (HtmlInputText) facet;
170        Object value = inputText.getSubmittedValue();
171        if (value != null && !(value instanceof String)) {
172            ComponentUtils.addErrorMessage(context, parent, "error.inputFile.invalidSpecialBlob");
173            parent.setValid(false);
174            return;
175        }
176        String string = (String) value;
177        if (StringUtils.isBlank(string) || string.indexOf(':') < 0) {
178            String message = context.getPartialViewContext().isAjaxRequest() ? InputFileInfo.INVALID_WITH_AJAX_MESSAGE
179                    : InputFileInfo.INVALID_FILE_MESSAGE;
180            ComponentUtils.addErrorMessage(context, parent, message);
181            parent.setValid(false);
182            return;
183        }
184
185        // micro parse the string (user:fileId)
186        String[] parts = string.split(":");
187        String user = parts[0];
188        String fileId = parts[1];
189
190        // check if we can get an access token
191        String accessToken = getAccessToken(user);
192        if (accessToken == null) {
193            String link = String.format("<a href='#' onclick=\"openPopup('%s'); return false;\">Register a new token</a> and try again.", getOAuthAuthorizationUrl());
194            ComponentUtils.addErrorMessage(context, parent, "error.inputFile.accessToken", new Object[] { user, link });
195            parent.setValid(false);
196            return;
197        }
198
199        Blob blob = createBlob(new FileInfo(user, fileId, null)); // no revisionId
200        submitted.setBlob(blob);
201        submitted.setFilename(blob.getFilename());
202        submitted.setMimeType(blob.getMimeType());
203    }
204
205    /**
206     * Google Drive upload button is added to the file widget if and only if Google Drive OAuth service provider is enabled
207     *
208     * @return true if Google Drive OAuth service provider is enabled or false otherwise.
209     */
210    @Override
211    public boolean isEnabled() {
212        return getGoogleDriveBlobProvider().getOAuth2Provider().isEnabled();
213    }
214
215    /**
216     * Creates a Google Drive managed blob.
217     *
218     * @param fileInfo the Google Drive file info
219     * @return the blob
220     */
221    protected Blob createBlob(FileInfo fileInfo) {
222        try {
223            return getGoogleDriveBlobProvider().getBlob(fileInfo);
224        } catch (IOException e) {
225            throw new RuntimeException(e); // TODO better feedback
226        }
227    }
228
229    protected GoogleDriveBlobProvider getGoogleDriveBlobProvider() {
230        return (GoogleDriveBlobProvider) Framework.getService(BlobManager.class)
231            .getBlobProvider(GoogleDriveBlobProvider.PREFIX);
232    }
233
234    protected String getGoogleDomain() {
235        String domain = Framework.getProperty(GOOGLE_DOMAIN_PROP);
236        return (domain != null) ? domain : "";
237    }
238
239    protected String getClientId() {
240        String clientId = getGoogleDriveBlobProvider().getClientId();
241        return (clientId != null) ? clientId : "";
242    }
243
244    protected String getAccessToken(String user) {
245        try {
246            Credential credential = getGoogleDriveBlobProvider().getCredential(user);
247            if (credential != null) {
248                String accessToken = credential.getAccessToken();
249                if (accessToken != null) {
250                    return accessToken;
251                }
252            }
253        } catch (IOException e) {
254            log.error("Failed to get access token for " + user, e);
255        }
256        return null;
257    }
258
259    private boolean hasServiceAccount() {
260        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
261        String username = request.getUserPrincipal().getName();
262        GoogleOAuth2ServiceProvider provider = (GoogleOAuth2ServiceProvider) getGoogleDriveBlobProvider().getOAuth2Provider();
263        return provider != null && provider.getServiceUser(username) != null;
264    }
265
266    private String getOAuthAuthorizationUrl() {
267        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
268        GoogleOAuth2ServiceProvider provider = (GoogleOAuth2ServiceProvider) getGoogleDriveBlobProvider().getOAuth2Provider();
269        return (provider != null && provider.getClientId() != null) ? provider.getAuthorizationUrl(request) : "";
270    }
271}