001/*
002 * (C) Copyright 2015 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 *     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    private final String types;
036
037    private final String documentId;
038
039    private final String rawQuery;
040
041    public DocRequestFilter(NuxeoPrincipal principal, String indices, String types, String documentId, String rawQuery) {
042        this.principal = principal;
043        this.indices = indices;
044        this.types = types;
045        this.documentId = documentId;
046        this.rawQuery = rawQuery;
047    }
048
049    protected @NotNull String getUrl() {
050        String url = "/" + indices + "/" + types + "/" + documentId;
051        if (rawQuery != null) {
052            url += "?" + rawQuery;
053        }
054        return url;
055    }
056
057    @Override
058    public String toString() {
059        return "Get Doc url: " + getUrl() + " user: " + principal;
060    }
061
062    public String getCheckAccessUrl() {
063        return "/" + indices + "/" + types + "/" + documentId + "?fields=ecm:acl";
064    }
065}