001// Generated by delombok at Thu Mar 19 15:24:58 GMT 2020
002/*
003 *  Licensed to the Apache Software Foundation (ASF) under one
004 *  or more contributor license agreements.  See the NOTICE file
005 *  distributed with this work for additional information
006 *  regarding copyright ownership.  The ASF licenses this file
007 *  to you under the Apache License, Version 2.0 (the
008 *  "License"); you may not use this file except in compliance
009 *  with the License.  You may obtain a copy of the License at
010 *
011 *        http://www.apache.org/licenses/LICENSE-2.0
012 *
013 *  Unless required by applicable law or agreed to in writing,
014 *  software distributed under the License is distributed on an
015 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 *  KIND, either express or implied.  See the License for the
017 *  specific language governing permissions and limitations
018 *  under the License.
019 */
020package org.apache.isis.viewer.restfulobjects.rendering;
021
022import java.text.SimpleDateFormat;
023import java.util.Date;
024import java.util.Locale;
025import java.util.TimeZone;
026import javax.ws.rs.core.MediaType;
027import javax.ws.rs.core.Response;
028import org.apache.isis.applib.services.clock.ClockService;
029import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
030import org.apache.isis.viewer.restfulobjects.applib.RestfulResponse;
031import org.apache.isis.viewer.restfulobjects.applib.util.JsonMapper;
032import org.apache.isis.viewer.restfulobjects.rendering.util.JsonWriterUtil;
033
034public final class Responses {
035    private Responses() {
036    }
037
038    public static Response.ResponseBuilder ofNoContent() {
039        return of(RestfulResponse.HttpStatusCode.NO_CONTENT);
040    }
041
042    public static Response.ResponseBuilder ofNotFound() {
043        return of(RestfulResponse.HttpStatusCode.NOT_FOUND);
044    }
045
046    public static Response.ResponseBuilder ofOk(final ReprRenderer<?, ?> renderer, final Caching caching) {
047        return ofOk(renderer, caching, null);
048    }
049
050    /**
051     * @param rootRepresentationIfAny - if specified, is used for entity; otherwise the renderer is used.  The idea is that the renderer will be set up to render to some sub-node of root representation
052     */
053    public static Response.ResponseBuilder ofOk(final ReprRenderer<?, ?> renderer, final Caching caching, final JsonRepresentation rootRepresentationIfAny) {
054        final JsonRepresentation representation = renderer.render();
055        // if a rootRepresentation is provided, then the assumption is that the rendered
056        // will be rendering to some submap of the rootRepresentation
057        final JsonRepresentation entityRepresentation = rootRepresentationIfAny != null ? rootRepresentationIfAny : representation;
058        final MediaType mediaType = renderer.getMediaType();
059        final Date now = now(renderer);
060        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
061        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
062        final Response.ResponseBuilder response = of(RestfulResponse.HttpStatusCode.OK).header("Date", dateFormat.format(now)).type(mediaType).cacheControl(caching.getCacheControl()).entity(JsonWriterUtil.jsonFor(entityRepresentation, inferPrettyPrinting(renderer)));
063        return response;
064    }
065
066    private static Date now(ReprRenderer<?, ?> renderer) {
067        if (renderer instanceof ReprRendererAbstract) {
068            ((ReprRendererAbstract<?, ?>) renderer).getResourceContext().getServiceRegistry().lookupServiceElseFail(ClockService.class).nowAsJavaUtilDate();
069        }
070        return new Date();
071    }
072
073    protected static Response.ResponseBuilder of(final RestfulResponse.HttpStatusCode httpStatusCode) {
074        return Response.status(httpStatusCode.getJaxrsStatusType()).type(MediaType.APPLICATION_JSON_TYPE);
075    }
076
077    public static Response.ResponseBuilder mediaType(final Response.ResponseBuilder responseBuilder, final MediaType mediaType) {
078        responseBuilder.type(mediaType);
079        return responseBuilder;
080    }
081
082    public static JsonMapper.PrettyPrinting inferPrettyPrinting(ReprRenderer<?, ?> renderer) {
083        if (renderer instanceof ReprRendererAbstract) {
084            final org.apache.isis.core.commons.internal.environment.IsisSystemEnvironment systemEnvironment = ((ReprRendererAbstract<?, ?>) renderer).getResourceContext().getMetaModelContext().getSystemEnvironment();
085            return systemEnvironment.isPrototyping() ? JsonMapper.PrettyPrinting.ENABLE : JsonMapper.PrettyPrinting.DISABLE;
086        }
087        return JsonMapper.PrettyPrinting.DISABLE;
088    }
089}