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 *     Estelle Giuly <egiuly@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.core.io.download;
021
022import java.util.Arrays;
023
024
025/**
026 * This class exposes information of a blob given its download path.
027 * For instance, it parses the download path "default/3727ef6b-cf8c-4f27-ab2c-79de0171a2c8/files:files/0/file/image.png"
028 * into { "repository": "default",
029 *        "docId": "3727ef6b-cf8c-4f27-ab2c-79de0171a2c8",
030 *        "xpath": "files:files/0/file",
031 *        "filename": "image.png" }
032 *
033 * @since 9.1
034 */
035public class DownloadBlobInfo {
036    protected final String repository;
037
038    protected final String docId;
039
040    protected final String xpath;
041
042    protected final String filename;
043
044    public DownloadBlobInfo(String downloadPath) {
045        String[] parts = downloadPath.split("/");
046        int length = parts.length;
047        if (length < 2) {
048            throw new IllegalArgumentException("The path \"" + downloadPath + "\" is not used to download blobs.");
049        }
050        repository = parts[0];
051        docId = parts[1];
052        if (length == 2) {
053            xpath = null;
054            filename = null;
055        } else if (length == 3) {
056            xpath = parts[2];
057            filename = null;
058        } else {
059            xpath = String.join("/", Arrays.asList(parts).subList(2, length - 1));
060            filename = parts[length - 1];
061        }
062    }
063}