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.applib; 020 021import javax.ws.rs.core.MediaType; 022 023import org.apache.isis.viewer.restfulobjects.applib.client.ClientRequestConfigurer; 024import org.apache.isis.viewer.restfulobjects.applib.client.RestfulRequest; 025import org.apache.isis.viewer.restfulobjects.applib.client.RestfulResponse; 026import org.codehaus.jackson.JsonNode; 027import org.codehaus.jackson.node.JsonNodeFactory; 028import org.codehaus.jackson.node.ObjectNode; 029import org.jboss.resteasy.client.ClientExecutor; 030 031public final class LinkRepresentation extends JsonRepresentation { 032 033 public LinkRepresentation() { 034 this(new ObjectNode(JsonNodeFactory.instance)); 035 withMethod(RestfulHttpMethod.GET); 036 } 037 038 public LinkRepresentation(final JsonNode jsonNode) { 039 super(jsonNode); 040 } 041 042 public String getRel() { 043 return asObjectNode().path("rel").getTextValue(); 044 } 045 046 public LinkRepresentation withRel(final String rel) { 047 asObjectNode().put("rel", rel); 048 return this; 049 } 050 051 public LinkRepresentation withRel(Rel rel) { 052 return withRel(rel.getName()); 053 } 054 055 056 public String getHref() { 057 return asObjectNode().path("href").getTextValue(); 058 } 059 060 public LinkRepresentation withHref(final String href) { 061 asObjectNode().put("href", href); 062 return this; 063 } 064 065 public JsonRepresentation getValue() { 066 return getRepresentation("value"); 067 } 068 069 public String getTitle() { 070 return getString("title"); 071 } 072 073 public LinkRepresentation withTitle(final String title) { 074 asObjectNode().put("title", title); 075 return this; 076 } 077 078 public RestfulHttpMethod getHttpMethod() { 079 final String methodStr = asObjectNode().path("method").getTextValue(); 080 return RestfulHttpMethod.valueOf(methodStr); 081 } 082 083 public MediaType getType() { 084 final String typeStr = asObjectNode().path("type").getTextValue(); 085 if (typeStr == null) { 086 return MediaType.APPLICATION_JSON_TYPE; 087 } 088 return MediaType.valueOf(typeStr); 089 } 090 091 public LinkRepresentation withMethod(final RestfulHttpMethod httpMethod) { 092 asObjectNode().put("method", httpMethod.name()); 093 return this; 094 } 095 096 /** 097 * Returns the "arguments" json-property of the link (a map). 098 * 099 * <p> 100 * If there is no "arguments" node, then as a convenience will 101 * return an empty map. 102 * 103 * @return 104 */ 105 public JsonRepresentation getArguments() { 106 final JsonNode arguments = asObjectNode().get("arguments"); 107 if (arguments == null || arguments.isNull()) { 108 return JsonRepresentation.newMap(); 109 } 110 return new JsonRepresentation(arguments); 111 } 112 113 public <T> RestfulResponse<JsonRepresentation> follow(final ClientExecutor executor) throws Exception { 114 return follow(executor, null); 115 } 116 117 public <T extends JsonRepresentation> RestfulResponse<T> follow(final ClientExecutor executor, final JsonRepresentation requestArgs) throws Exception { 118 119 final ClientRequestConfigurer clientRequestConfigurer = ClientRequestConfigurer.create(executor, getHref()); 120 121 clientRequestConfigurer.accept(MediaType.APPLICATION_JSON_TYPE); 122 clientRequestConfigurer.setHttpMethod(getHttpMethod()); 123 124 clientRequestConfigurer.configureArgs(requestArgs); 125 126 final RestfulRequest restfulRequest = new RestfulRequest(clientRequestConfigurer); 127 return restfulRequest.executeT(); 128 } 129 130 @Override 131 public int hashCode() { 132 final int prime = 31; 133 int result = 1; 134 result = prime * result + ((getHref() == null) ? 0 : getHref().hashCode()); 135 result = prime * result + ((getHttpMethod() == null) ? 0 : getHttpMethod().hashCode()); 136 return result; 137 } 138 139 @Override 140 public boolean equals(final Object obj) { 141 if (this == obj) { 142 return true; 143 } 144 if (obj == null) { 145 return false; 146 } 147 if (getClass() != obj.getClass()) { 148 return false; 149 } 150 final LinkRepresentation other = (LinkRepresentation) obj; 151 if (getHref() == null) { 152 if (other.getHref() != null) { 153 return false; 154 } 155 } else if (!getHref().equals(other.getHref())) { 156 return false; 157 } 158 if (getHttpMethod() != other.getHttpMethod()) { 159 return false; 160 } 161 return true; 162 } 163 164 @Override 165 public String toString() { 166 return "Link [rel=" + getRel() + ", href=" + getHref() + ", method=" + getHttpMethod() + ", type=" + getType() + "]"; 167 } 168 169 170}