001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019package org.apache.isis.viewer.restfulobjects.server.resources;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.Produces;
024import javax.ws.rs.core.HttpHeaders;
025import javax.ws.rs.core.MediaType;
026import javax.ws.rs.core.Response;
027
028import org.apache.isis.applib.annotation.Where;
029import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
030import org.apache.isis.viewer.restfulobjects.applib.RepresentationType;
031import org.apache.isis.viewer.restfulobjects.applib.RestfulMediaType;
032import org.apache.isis.viewer.restfulobjects.applib.client.RestfulResponse.HttpStatusCode;
033import org.apache.isis.viewer.restfulobjects.applib.version.VersionResource;
034import org.apache.isis.viewer.restfulobjects.server.RestfulObjectsApplicationException;
035
036/**
037 * Implementation note: it seems to be necessary to annotate the implementation
038 * with {@link Path} rather than the interface (at least under RestEasy 1.0.2
039 * and 1.1-RC2).
040 */
041//@Path("/version")
042public class VersionResourceServerside extends ResourceAbstract implements VersionResource {
043
044    @Override
045    @GET
046    @Produces({ MediaType.APPLICATION_JSON, RestfulMediaType.APPLICATION_JSON_VERSION })
047    public Response version() {
048        init(RepresentationType.VERSION, Where.NOWHERE);
049        fakeRuntimeExceptionIfXFail();
050
051        final VersionReprRenderer renderer = new VersionReprRenderer(getResourceContext(), null, JsonRepresentation.newMap());
052        renderer.includesSelf();
053
054        return responseOfOk(renderer, Caching.ONE_DAY).build();
055    }
056
057    private void fakeRuntimeExceptionIfXFail() {
058        final HttpHeaders httpHeaders = getResourceContext().getHttpHeaders();
059        if (httpHeaders.getRequestHeader("X-Fail") != null) {
060            throw RestfulObjectsApplicationException.create(HttpStatusCode.METHOD_FAILURE);
061        }
062    }
063
064}