001/*
002 * (C) Copyright 2017 Nuxeo (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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.web.common;
020
021import javax.servlet.http.HttpServletRequest;
022
023import org.apache.commons.lang.StringUtils;
024import org.nuxeo.ecm.core.api.DocumentLocation;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.IdRef;
027import org.nuxeo.ecm.core.api.PathRef;
028import org.nuxeo.ecm.platform.url.api.DocumentView;
029import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager;
030import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Helper for the banner to open a document in the mobile application.
035 *
036 * @since 9.1
037 */
038public class MobileBannerHelper {
039
040    public static final String PROTOCOL_PROPERTY = "nuxeo.mobile.application.protocol";
041
042    public static final String ANDROID_PACKAGE_PROPERTY = "nuxeo.mobile.application.android.package";
043
044    public static final String ITUNES_ID_PROPERTY = "nuxeo.mobile.application.iTunesId";
045
046    public static final String ANDROID_PROTOCOL_SCHEME = "android-app";
047
048    public static final String ITUNES_URL = "https://itunes.apple.com/app/";
049
050    /**
051     * Returns a full URL opening the Android mobile application.
052     */
053    public static String getURLForAndroidApplication(HttpServletRequest request) {
054        return getURLForAndroidApplication(request, null);
055    }
056
057    /**
058     * Returns a full URL opening the Android mobile application for the given document.
059     */
060    public static String getURLForAndroidApplication(HttpServletRequest request, DocumentModel doc) {
061        return getURLForMobileApplication(getAndroidProtocol(), request, doc);
062    }
063
064    /**
065     * Returns a full URL opening the iOS mobile application.
066     */
067    public static String getURLForIOSApplication(HttpServletRequest request) {
068        return getURLForIOSApplication(request, null);
069    }
070
071    /**
072     * Returns a full URL opening the iOS mobile application for the given document.
073     */
074    public static String getURLForIOSApplication(HttpServletRequest request, DocumentModel doc) {
075        return getURLForMobileApplication(getIOSProtocol(), request, doc);
076    }
077
078    /**
079     * Returns the URL of the iOS mobile application in the App Store.
080     */
081    public static String getAppStoreURL() {
082        return ITUNES_URL + Framework.getProperty(ITUNES_ID_PROPERTY);
083    }
084
085    public static String getAndroidProtocol() {
086        return String.format("%s://%s/%s/", ANDROID_PROTOCOL_SCHEME, Framework.getProperty(ANDROID_PACKAGE_PROPERTY),
087                Framework.getProperty(PROTOCOL_PROPERTY));
088    }
089
090    public static String getIOSProtocol() {
091        return String.format("%s://", Framework.getProperty(PROTOCOL_PROPERTY));
092    }
093
094    public static String getURLForMobileApplication(String protocol, HttpServletRequest request, DocumentModel doc) {
095        String baseURL = VirtualHostHelper.getBaseURL(request);
096        String requestedURL = request.getParameter("requestedUrl");
097        return getURLForMobileApplication(protocol, baseURL, doc, requestedURL);
098    }
099
100    public static String getURLForMobileApplication(String protocol, String baseURL, DocumentModel doc,
101            String requestedURL) {
102        String url = protocol + getServerPart(baseURL);
103        if (doc != null) {
104            return url + getDocumentPart(doc);
105        }
106        if (StringUtils.isNotBlank(requestedURL)) {
107            return url + getDocumentPart(requestedURL);
108        }
109        return url;
110    }
111
112    protected static String getServerPart(String baseURL) {
113        if (!baseURL.endsWith("/")) {
114            baseURL += "/";
115        }
116        return baseURL.replaceAll("://", "/");
117    }
118
119    protected static String getDocumentPart(DocumentModel doc) {
120        return doc.getRepositoryName() + "/id/" + doc.getId();
121    }
122
123    protected static String getDocumentPart(String requestedURL) {
124        String docPart = "";
125        DocumentViewCodecManager documentViewCodecManager = Framework.getService(DocumentViewCodecManager.class);
126        DocumentView docView = documentViewCodecManager.getDocumentViewFromUrl(requestedURL, false, null);
127        if (docView != null) {
128            DocumentLocation docLoc = docView.getDocumentLocation();
129            if (docLoc != null) {
130                String serverName = docLoc.getServerName();
131                if (serverName != null) {
132                    docPart += serverName;
133                    IdRef idRef = docLoc.getIdRef();
134                    PathRef pathRef = docLoc.getPathRef();
135                    if (idRef != null) {
136                        docPart += "/id/" + idRef;
137                    } else if (pathRef != null) {
138                        docPart += "/path" + pathRef;
139                    }
140                }
141            }
142        }
143        return docPart;
144    }
145
146}