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