001/*
002 * (C) Copyright 2017-2019 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.restapi.server.jaxrs.adapters;
021
022import static org.nuxeo.ecm.core.io.marshallers.json.document.DocumentPropertyJsonWriter.OMIT_PHANTOM_SECURED_PROPERTY;
023
024import javax.ws.rs.GET;
025import javax.ws.rs.Produces;
026import javax.ws.rs.QueryParam;
027import javax.ws.rs.core.Context;
028import javax.ws.rs.core.MediaType;
029
030import org.apache.commons.lang3.StringUtils;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.io.registry.context.RenderingContext;
034import org.nuxeo.ecm.webengine.model.WebAdapter;
035import org.nuxeo.ecm.webengine.model.exceptions.IllegalParameterException;
036import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
037
038/**
039 * @since 9.3
040 */
041@WebAdapter(name = EmptyDocumentAdapter.NAME, type = "emptyDocumentAdapter")
042@Produces(MediaType.APPLICATION_JSON)
043public class EmptyDocumentAdapter extends DefaultAdapter {
044
045    public static final String NAME = "emptyWithDefault";
046
047    @Context
048    protected RenderingContext renderingCtx;
049
050    @GET
051    public DocumentModel getEmptyDocumentModel(@QueryParam("type") String type, @QueryParam("name") String name) {
052        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
053        CoreSession session = ctx.getCoreSession();
054
055        if (StringUtils.isBlank(type)) {
056            throw new IllegalParameterException("Missing type parameter");
057        }
058        // as the returned document is intended to be POSTed for creation we remove secured properties
059        renderingCtx.addParameterValues(OMIT_PHANTOM_SECURED_PROPERTY, true);
060
061        DocumentModel emptyDoc = session.createDocumentModel(doc != null ? doc.getPathAsString() : null, name, type);
062        emptyDoc.detach(false);
063        return emptyDoc;
064    }
065}