001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.isis.viewer.restfulobjects.server.resources; 018 019import org.apache.isis.core.commons.authentication.AuthenticationSession; 020import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation; 021import org.apache.isis.viewer.restfulobjects.applib.Rel; 022import org.apache.isis.viewer.restfulobjects.applib.RepresentationType; 023import org.apache.isis.viewer.restfulobjects.rendering.LinkBuilder; 024import org.apache.isis.viewer.restfulobjects.rendering.LinkFollowSpecs; 025import org.apache.isis.viewer.restfulobjects.rendering.RendererContext; 026import org.apache.isis.viewer.restfulobjects.rendering.ReprRendererAbstract; 027 028public class UserReprRenderer extends ReprRendererAbstract<UserReprRenderer, AuthenticationSession> { 029 030 UserReprRenderer(final RendererContext rendererContext, final LinkFollowSpecs linkFollower, final JsonRepresentation representation) { 031 super(rendererContext, linkFollower, RepresentationType.USER, representation); 032 } 033 034 @Override 035 public UserReprRenderer with(final AuthenticationSession authenticationSession) { 036 representation.mapPut("userName", authenticationSession.getUserName()); 037 final JsonRepresentation roles = JsonRepresentation.newArray(); 038 for (final String role : authenticationSession.getRoles()) { 039 roles.arrayAdd(role); 040 } 041 representation.mapPut("roles", roles); 042 return this; 043 } 044 045 @Override 046 public JsonRepresentation render() { 047 if (includesSelf) { 048 addLinkToSelf(); 049 addLinkToUp(); 050 } 051 getExtensions(); 052 return representation; 053 } 054 055 private void addLinkToSelf() { 056 final JsonRepresentation link = LinkBuilder.newBuilder(getRendererContext(), Rel.SELF.getName(), RepresentationType.USER, "user").build(); 057 058 final LinkFollowSpecs linkFollower = getLinkFollowSpecs().follow("links"); 059 if (linkFollower.matches(link)) { 060 final UserReprRenderer renderer = new UserReprRenderer(getRendererContext(), linkFollower, JsonRepresentation.newMap()); 061 renderer.with(getRendererContext().getAuthenticationSession()); 062 link.mapPut("value", renderer.render()); 063 } 064 065 getLinks().arrayAdd(link); 066 } 067 068 private void addLinkToUp() { 069 final JsonRepresentation link = LinkBuilder.newBuilder(rendererContext, Rel.UP.getName(), RepresentationType.HOME_PAGE, "").build(); 070 071 final LinkFollowSpecs linkFollower = getLinkFollowSpecs().follow("links"); 072 if (linkFollower.matches(link)) { 073 final HomePageReprRenderer renderer = new HomePageReprRenderer(getRendererContext(), linkFollower, JsonRepresentation.newMap()); 074 link.mapPut("value", renderer.render()); 075 } 076 getLinks().arrayAdd(link); 077 } 078 079 080}