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;
020
021 import java.net.URI;
022
023 import javax.ws.rs.core.MediaType;
024 import javax.ws.rs.core.Response;
025
026 import org.apache.commons.httpclient.HttpClient;
027 import org.jboss.resteasy.client.ClientExecutor;
028 import org.jboss.resteasy.client.ClientRequestFactory;
029 import org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor;
030
031 import org.apache.isis.viewer.restfulobjects.applib.domaintypes.DomainTypeResource;
032 import org.apache.isis.viewer.restfulobjects.applib.homepage.HomePageResource;
033 import org.apache.isis.viewer.restfulobjects.applib.links.LinkRepresentation;
034 import org.apache.isis.viewer.restfulobjects.applib.user.UserResource;
035 import org.apache.isis.viewer.restfulobjects.applib.version.VersionResource;
036 import org.apache.isis.viewer.restfulobjects.domainobjects.DomainObjectResource;
037 import org.apache.isis.viewer.restfulobjects.domainobjects.DomainServiceResource;
038
039 public class RestfulClient {
040
041 private final HomePageResource homePageResource;
042 private final UserResource userResource;
043 private final VersionResource versionResource;
044 private final DomainObjectResource domainObjectResource;
045 private final DomainServiceResource domainServiceResource;
046 private final DomainTypeResource domainTypeResource;
047
048 private final ClientExecutor executor;
049 private final ClientRequestFactory clientRequestFactory;
050
051 public RestfulClient(final URI baseUri) {
052 this(baseUri, new ApacheHttpClientExecutor(new HttpClient()));
053 }
054
055 public RestfulClient(final URI baseUri, final ClientExecutor clientExecutor) {
056 this.executor = clientExecutor;
057 this.clientRequestFactory = new ClientRequestFactory(clientExecutor, baseUri);
058
059 this.homePageResource = clientRequestFactory.createProxy(HomePageResource.class);
060 this.userResource = clientRequestFactory.createProxy(UserResource.class);
061 this.domainTypeResource = clientRequestFactory.createProxy(DomainTypeResource.class);
062 this.domainServiceResource = clientRequestFactory.createProxy(DomainServiceResource.class);
063 this.domainObjectResource = clientRequestFactory.createProxy(DomainObjectResource.class);
064 this.versionResource = clientRequestFactory.createProxy(VersionResource.class);
065 }
066
067 // ///////////////////////////////////////////////////////////////
068 // resources
069 // ///////////////////////////////////////////////////////////////
070
071 public HomePageResource getHomePageResource() {
072 return homePageResource;
073 }
074
075 public UserResource getUserResource() {
076 return userResource;
077 }
078
079 public VersionResource getVersionResource() {
080 return versionResource;
081 }
082
083 public DomainTypeResource getDomainTypeResource() {
084 return domainTypeResource;
085 }
086
087 public DomainObjectResource getDomainObjectResource() {
088 return domainObjectResource;
089 }
090
091 public DomainServiceResource getDomainServiceResource() {
092 return domainServiceResource;
093 }
094
095 // ///////////////////////////////////////////////////////////////
096 // resource walking support
097 // ///////////////////////////////////////////////////////////////
098
099 public RepresentationWalker createWalker(final Response response) {
100 return new RepresentationWalker(this, response);
101 }
102
103 public RestfulResponse<JsonRepresentation> follow(final LinkRepresentation link) throws Exception {
104 return followT(link);
105 }
106
107 public <T extends JsonRepresentation> RestfulResponse<T> followT(final LinkRepresentation link) throws Exception {
108 return followT(link, JsonRepresentation.newMap());
109 }
110
111 public RestfulResponse<JsonRepresentation> follow(final LinkRepresentation link, final JsonRepresentation requestArgs) throws Exception {
112 return followT(link, requestArgs);
113 }
114
115 public <T extends JsonRepresentation> RestfulResponse<T> followT(final LinkRepresentation link, final JsonRepresentation requestArgs) throws Exception {
116 return link.<T> follow(executor, requestArgs);
117 }
118
119 public RestfulRequest createRequest(final HttpMethod httpMethod, final String uriTemplate) {
120
121 final boolean includesScheme = uriTemplate.startsWith("http:") || uriTemplate.startsWith("https:");
122 final String base = clientRequestFactory.getBase().toString();
123 final String uri = (includesScheme ? "" : base) + uriTemplate;
124
125 final ClientRequestConfigurer clientRequestConfigurer = ClientRequestConfigurer.create(executor, uri);
126
127 clientRequestConfigurer.accept(MediaType.APPLICATION_JSON_TYPE);
128 clientRequestConfigurer.setHttpMethod(httpMethod);
129
130 return new RestfulRequest(clientRequestConfigurer);
131 }
132
133 /**
134 * exposed for testing purposes only.
135 */
136 public ClientRequestFactory getClientRequestFactory() {
137 return clientRequestFactory;
138 }
139
140 }