001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Kevin Leturc
018 */
019package org.nuxeo.ecm.liveconnect.onedrive;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.net.URI;
024import java.util.Collections;
025import java.util.List;
026
027import javax.servlet.http.HttpServletRequest;
028
029import org.nuxeo.ecm.core.blob.BlobManager.UsageHint;
030import org.nuxeo.ecm.core.blob.ManagedBlob;
031import org.nuxeo.ecm.core.blob.apps.AppLink;
032import org.nuxeo.ecm.liveconnect.core.AbstractLiveConnectBlobProvider;
033import org.nuxeo.ecm.liveconnect.core.LiveConnectFile;
034import org.nuxeo.ecm.liveconnect.core.LiveConnectFileInfo;
035import org.nuxeo.onedrive.client.OneDriveAPI;
036import org.nuxeo.onedrive.client.OneDriveFile;
037import org.nuxeo.onedrive.client.OneDriveSharingLink.Type;
038import org.nuxeo.onedrive.client.OneDriveThumbnailSize;
039
040import com.google.api.client.auth.oauth2.Credential;
041
042/**
043 * Provider for blobs getting information from OneDrive.
044 *
045 * @since 8.2
046 */
047public class OneDriveBlobProvider extends AbstractLiveConnectBlobProvider<OneDriveOAuth2ServiceProvider> {
048
049    private static final String CACHE_NAME = "onedrive";
050
051    private static final String ONEDRIVE_DOCUMENT_TO_BE_UPDATED_PP = "onedrive_document_to_be_updated";
052
053    @Override
054    protected String getCacheName() {
055        return CACHE_NAME;
056    }
057
058    @Override
059    protected String getPageProviderNameForUpdate() {
060        return ONEDRIVE_DOCUMENT_TO_BE_UPDATED_PP;
061    }
062
063    @Override
064    public URI getURI(ManagedBlob blob, UsageHint usage, HttpServletRequest servletRequest) throws IOException {
065        LiveConnectFileInfo fileInfo = toFileInfo(blob);
066        String url = null;
067        switch (usage) {
068        case STREAM:
069        case DOWNLOAD:
070            url = prepareOneDriveFile(fileInfo).getMetadata().getDownloadUrl();
071            break;
072        case VIEW:
073        case EDIT:
074            url = prepareOneDriveFile(fileInfo).createSharedLink(Type.EDIT).getLink().getWebUrl();
075            break;
076        }
077        return url == null ? null : asURI(url);
078    }
079
080    @Override
081    public InputStream getStream(ManagedBlob blob) throws IOException {
082        return prepareOneDriveFile(toFileInfo(blob)).download();
083    }
084
085    @Override
086    public InputStream getThumbnail(ManagedBlob blob) throws IOException {
087        return prepareOneDriveFile(toFileInfo(blob)).downloadThumbnail(OneDriveThumbnailSize.LARGE);
088    }
089
090    @Override
091    public List<AppLink> getAppLinks(String username, ManagedBlob blob) throws IOException {
092        // application links do not work with document which are not office document
093        if (!blob.getMimeType().contains("officedocument")) {
094            return Collections.emptyList();
095        }
096        // application links do not work with revisions
097        LiveConnectFileInfo fileInfo = toFileInfo(blob);
098        if (fileInfo.getRevisionId().isPresent()) {
099            return Collections.emptyList();
100        }
101        AppLink appLink = new AppLink();
102        String appUrl = prepareOneDriveFile(fileInfo).createSharedLink(Type.EDIT).getLink().getWebUrl();
103        appLink.setLink(appUrl);
104        appLink.setAppName("Microsoft OneDrive");
105        appLink.setIcon("icons/OneDrive.png");
106        return Collections.singletonList(appLink);
107    }
108
109    protected OneDriveAPI getOneDriveAPI(LiveConnectFileInfo fileInfo) throws IOException {
110        return getOneDriveAPI(getCredential(fileInfo));
111    }
112
113    protected OneDriveAPI getOneDriveAPI(Credential credential) {
114        return getOAuth2Provider().getAPIInitializer().apply(credential.getAccessToken());
115    }
116
117    @Override
118    protected LiveConnectFile retrieveFile(LiveConnectFileInfo fileInfo) throws IOException {
119        return new OneDriveLiveConnectFile(fileInfo, retrieveOneDriveFileMetadata(fileInfo));
120    }
121
122    protected OneDriveFile.Metadata retrieveOneDriveFileMetadata(LiveConnectFileInfo fileInfo) throws IOException {
123        return prepareOneDriveFile(fileInfo).getMetadata();
124    }
125
126    protected OneDriveFile prepareOneDriveFile(LiveConnectFileInfo fileInfo) throws IOException {
127        OneDriveAPI api = getOneDriveAPI(fileInfo);
128        return new OneDriveFile(api, fileInfo.getFileId());
129    }
130
131}