001/*
002 * (C) Copyright 2015-2020 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 *     Benoit Delbosc
018 */
019package org.nuxeo.elasticsearch.http.readonly;
020
021import javax.validation.constraints.NotNull;
022
023import org.nuxeo.ecm.core.api.NuxeoPrincipal;
024
025/**
026 * Rewrite a Document get request to add security.
027 *
028 * @since 7.3
029 */
030public class DocRequestFilter {
031    private final NuxeoPrincipal principal;
032
033    private final String indices;
034
035    /** @deprecated since 11.4, types have been removed since Elasticsearch 7.x */
036    @Deprecated(since = "11.4", forRemoval = true)
037    private final String types = null;
038
039    private final String documentId;
040
041    private final String rawQuery;
042
043    public DocRequestFilter(NuxeoPrincipal principal, String indices, String documentId, String rawQuery) {
044        this.principal = principal;
045        this.indices = indices;
046        this.documentId = documentId;
047        this.rawQuery = rawQuery;
048    }
049
050    /**
051     * @deprecated since 11.4, types have been removed since Elasticsearch 7.x, use
052     *             {@link #DocRequestFilter(NuxeoPrincipal, String, String, String)} instead
053     */
054    @Deprecated(since = "11.4", forRemoval = true)
055    public DocRequestFilter(NuxeoPrincipal principal, String indices, String types, String documentId,
056            String rawQuery) {
057        this(principal, indices, documentId, rawQuery);
058    }
059
060    protected @NotNull String getUrl() {
061        String url = "/" + indices + "/" + documentId;
062        if (rawQuery != null) {
063            url += "?" + rawQuery;
064        }
065        return url;
066    }
067
068    @Override
069    public String toString() {
070        return "Get Doc url: " + getUrl() + " user: " + principal;
071    }
072
073    public String getCheckAccessUrl() {
074        return "/" + indices + "/" + documentId + "?fields=ecm:acl";
075    }
076}