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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.restapi.server.jaxrs.adapters;
021
022import javax.ws.rs.GET;
023import javax.ws.rs.Produces;
024import javax.ws.rs.QueryParam;
025import javax.ws.rs.core.MediaType;
026
027import org.apache.commons.lang3.StringUtils;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.webengine.model.WebAdapter;
031import org.nuxeo.ecm.webengine.model.exceptions.IllegalParameterException;
032import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
033
034/**
035 * @since 9.3
036 */
037@WebAdapter(name = EmptyDocumentAdapter.NAME, type = "emptyDocumentAdapter")
038@Produces(MediaType.APPLICATION_JSON)
039public class EmptyDocumentAdapter extends DefaultAdapter {
040
041    public static final String NAME = "emptyWithDefault";
042
043    @GET
044    public DocumentModel getEmptyDocumentModel(@QueryParam("type") String type, @QueryParam("name") String name) {
045        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
046        CoreSession session = ctx.getCoreSession();
047
048        if (StringUtils.isBlank(type)) {
049            throw new IllegalParameterException("Missing type parameter");
050        }
051
052        DocumentModel emptyDoc = session.createDocumentModel(doc != null ? doc.getPathAsString() : null, name, type);
053        emptyDoc.detach(false);
054        return emptyDoc;
055    }
056}