├── .gitignore ├── LICENSE ├── README.textile ├── client ├── .project ├── jettison-1.1.jar ├── jruby.jar ├── pom.xml └── src │ ├── main │ └── java │ │ └── br │ │ └── com │ │ └── caelum │ │ └── restfulie │ │ ├── Link.java │ │ ├── RequestEntry.java │ │ ├── Resource.java │ │ ├── Response.java │ │ ├── RestClient.java │ │ ├── Restfulie.java │ │ ├── RestfulieException.java │ │ ├── client │ │ └── DefaultLinkConverter.java │ │ ├── feature │ │ ├── CurlLogging.java │ │ ├── Features.java │ │ ├── FollowRedirects.java │ │ ├── RequestFeature.java │ │ ├── ResponseFeature.java │ │ ├── RetryWhenUnavailable.java │ │ └── ThrowError.java │ │ ├── http │ │ ├── AsynchronousRequest.java │ │ ├── ContentProcessor.java │ │ ├── DefaultHeader.java │ │ ├── DefaultHttpRequest.java │ │ ├── DefaultRelation.java │ │ ├── DefaultRestClient.java │ │ ├── Header.java │ │ ├── Headers.java │ │ ├── HttpClientProvider.java │ │ ├── HttpMethod.java │ │ ├── HttpURLConnectionContentProcessor.java │ │ ├── IdentityContentProcessor.java │ │ ├── Request.java │ │ ├── RequestCallback.java │ │ ├── apache │ │ │ ├── ApacheDispatcher.java │ │ │ ├── ApacheHeaders.java │ │ │ └── ApacheResponse.java │ │ ├── error │ │ │ ├── BadRequestException.java │ │ │ ├── ClientException.java │ │ │ ├── ConflictException.java │ │ │ ├── ForbiddenException.java │ │ │ ├── GoneException.java │ │ │ ├── MethodNotAllowedException.java │ │ │ ├── NotFoundException.java │ │ │ ├── NotImplementedException.java │ │ │ ├── PreconditionFailedException.java │ │ │ ├── ProxyAuthenticationRequiredException.java │ │ │ ├── RedicetionException.java │ │ │ ├── ServerException.java │ │ │ ├── UnauthorizedException.java │ │ │ └── UnknowCodeException.java │ │ └── javanet │ │ │ ├── JavaNetDispatcher.java │ │ │ ├── JavaNetResponse.java │ │ │ └── MapHeaders.java │ │ ├── mediatype │ │ ├── EnhancedList.java │ │ ├── FormEncoded.java │ │ ├── JsonMediaType.java │ │ ├── MediaType.java │ │ ├── MediaTypes.java │ │ ├── XStreamHelper.java │ │ └── XmlMediaType.java │ │ ├── opensearch │ │ ├── SearchDescription.java │ │ ├── Tags.java │ │ ├── Url.java │ │ └── conveter │ │ │ ├── DefaultTagsConveter.java │ │ │ └── DefaultUrlConverter.java │ │ ├── relation │ │ ├── CachedEnhancer.java │ │ ├── DefaultEnhancer.java │ │ └── Enhancer.java │ │ ├── request │ │ ├── RequestChain.java │ │ ├── RequestDispatcher.java │ │ ├── RequestStack.java │ │ └── ResponseChain.java │ │ └── serializer │ │ └── SerializationException.java │ └── test │ └── java │ └── br │ └── com │ └── caelum │ └── restfulie │ ├── LinkTest.java │ ├── XmlMediaTypeTest.java │ ├── feature │ ├── FollowRedirectsTest.java │ ├── RetryWhenUnavailableTest.java │ └── ThrowErrorTest.java │ ├── http │ ├── DefaultRestClientTest.java │ ├── MapHeadersTest.java │ └── apache │ │ ├── ApacheHeadersTest.java │ │ ├── ApacheResponseTest.java │ │ └── ContentTypeHeader.java │ ├── integration │ └── client │ │ ├── ClientTest.java │ │ ├── Item.java │ │ ├── Order.java │ │ ├── Payment.java │ │ └── Product.java │ ├── maze │ └── Maze.java │ ├── mediatype │ └── FormEncodedTest.java │ └── opensearch │ ├── OpenSearchTest.java │ ├── UrlTest.java │ └── converter │ └── TagsConverterTest.java ├── example ├── client │ ├── .classpath │ ├── .project │ ├── examples │ │ ├── order.xml │ │ └── payment.xml │ ├── lib │ │ ├── javassist.jar │ │ ├── restfulie-java-client-1.0.0-beta-2.jar │ │ ├── stax-1.2.0.jar │ │ ├── stax-api-1.0.1.jar │ │ ├── xpp3_min-1.1.4c.jar │ │ └── xstream-1.3.1.jar │ └── src │ │ ├── main │ │ └── java │ │ │ └── br │ │ │ └── com │ │ │ └── caelum │ │ │ └── restbucks │ │ │ └── model │ │ │ ├── Item.java │ │ │ ├── Order.java │ │ │ ├── Ordering.java │ │ │ ├── Payment.java │ │ │ └── Receipt.java │ │ └── test │ │ └── java │ │ └── br │ │ └── com │ │ └── caelum │ │ └── restbucks │ │ ├── Entry.java │ │ ├── MappingConfig.java │ │ └── MappingConfigTest.java ├── rest_from_scratch │ ├── part_1 │ │ ├── client │ │ │ ├── .classpath │ │ │ ├── .gitignore │ │ │ ├── .project │ │ │ ├── README │ │ │ ├── lib │ │ │ │ ├── javassist.jar │ │ │ │ ├── jettison-1.2.jar │ │ │ │ ├── junit-4.5.jar │ │ │ │ ├── stax-1.2.0.jar │ │ │ │ ├── stax-api-1.0.1.jar │ │ │ │ ├── xpp3_min-1.1.4c.jar │ │ │ │ └── xstream-1.3.1.jar │ │ │ └── src │ │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── br │ │ │ │ └── com │ │ │ │ └── caelum │ │ │ │ ├── client │ │ │ │ └── ClientTests.java │ │ │ │ └── example │ │ │ │ └── model │ │ │ │ └── Item.java │ │ └── server │ │ │ ├── .classpath │ │ │ ├── .gitignore │ │ │ ├── .project │ │ │ ├── .settings │ │ │ ├── .jsdtscope │ │ │ ├── org.eclipse.jdt.core.prefs │ │ │ ├── org.eclipse.wst.common.component │ │ │ ├── org.eclipse.wst.common.project.facet.core.xml │ │ │ ├── org.eclipse.wst.jsdt.ui.superType.container │ │ │ └── org.eclipse.wst.jsdt.ui.superType.name │ │ │ ├── README │ │ │ ├── WebContent │ │ │ ├── META-INF │ │ │ │ └── MANIFEST.MF │ │ │ ├── WEB-INF │ │ │ │ ├── jsp │ │ │ │ │ └── .DS_Store │ │ │ │ ├── lib │ │ │ │ │ ├── .jars │ │ │ │ │ ├── aopalliance.jar │ │ │ │ │ ├── aspectjrt.jar │ │ │ │ │ ├── cglib-nodep-2.1_3.jar │ │ │ │ │ ├── commons-fileupload-1.2.1.jar │ │ │ │ │ ├── commons-logging.jar │ │ │ │ │ ├── google-collect-1.0.jar │ │ │ │ │ ├── javassist-3.9.0.GA.jar │ │ │ │ │ ├── jettison-1.2.jar │ │ │ │ │ ├── jstl-api-1.2.jar │ │ │ │ │ ├── jstl-impl-1.2.jar │ │ │ │ │ ├── log4j-1.2.15.jar │ │ │ │ │ ├── mirror-1.5.1.jar │ │ │ │ │ ├── objenesis-1.1.jar │ │ │ │ │ ├── ognl-2.7.3.jar │ │ │ │ │ ├── org.springframework.aop-3.0.0.RELEASE.jar │ │ │ │ │ ├── org.springframework.asm-3.0.0.RELEASE.jar │ │ │ │ │ ├── org.springframework.aspects-3.0.0.RELEASE.jar │ │ │ │ │ ├── org.springframework.beans-3.0.0.RELEASE.jar │ │ │ │ │ ├── org.springframework.context-3.0.0.RELEASE.jar │ │ │ │ │ ├── org.springframework.core-3.0.0.RELEASE.jar │ │ │ │ │ ├── org.springframework.expression-3.0.0.RELEASE.jar │ │ │ │ │ ├── org.springframework.web-3.0.0.RELEASE.jar │ │ │ │ │ ├── paranamer-2.2.jar │ │ │ │ │ ├── slf4j-api-1.5.8.jar │ │ │ │ │ ├── slf4j-log4j12-1.5.8.jar │ │ │ │ │ ├── vraptor-3.1.3.jar │ │ │ │ │ ├── vraptor-3.2.0-SNAPSHOT.jar │ │ │ │ │ └── xstream-1.3.1.jar │ │ │ │ └── web.xml │ │ │ └── index.jsp │ │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ ├── br │ │ │ └── com │ │ │ │ └── caelum │ │ │ │ └── example │ │ │ │ ├── controller │ │ │ │ └── ItemsController.java │ │ │ │ ├── infra │ │ │ │ └── Database.java │ │ │ │ ├── model │ │ │ │ └── Item.java │ │ │ │ └── vraptor │ │ │ │ └── component │ │ │ │ ├── CustomJsonDeserializer.java │ │ │ │ ├── CustomXStreamXMLDeserializer.java │ │ │ │ └── JSONSerializationCustomized.java │ │ │ └── log4j.xml │ └── part_2 │ │ ├── client │ │ ├── .gitignore │ │ ├── README │ │ ├── lib │ │ │ ├── javassist.jar │ │ │ ├── junit-4.5.jar │ │ │ ├── stax-1.2.0.jar │ │ │ ├── stax-api-1.0.1.jar │ │ │ ├── xpp3_min-1.1.4c.jar │ │ │ └── xstream-1.3.1.jar │ │ ├── restfulie-java-client-1.0.0-beta-2.jar │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── br │ │ │ └── com │ │ │ └── caelum │ │ │ ├── client │ │ │ └── ClientTests.java │ │ │ └── example │ │ │ └── model │ │ │ ├── Basket.java │ │ │ ├── Item.java │ │ │ └── Payment.java │ │ └── server │ │ ├── .gitignore │ │ ├── README │ │ ├── WebContent │ │ ├── META-INF │ │ │ └── MANIFEST.MF │ │ ├── WEB-INF │ │ │ ├── jsp │ │ │ │ └── .DS_Store │ │ │ ├── lib │ │ │ │ ├── .jars │ │ │ │ ├── aopalliance.jar │ │ │ │ ├── aspectjrt.jar │ │ │ │ ├── cglib-nodep-2.1_3.jar │ │ │ │ ├── commons-fileupload-1.2.1.jar │ │ │ │ ├── commons-logging.jar │ │ │ │ ├── javassist-3.9.0.GA.jar │ │ │ │ ├── jettison-1.2.jar │ │ │ │ ├── jstl-api-1.2.jar │ │ │ │ ├── jstl-impl-1.2.jar │ │ │ │ ├── log4j-1.2.15.jar │ │ │ │ ├── mirror-1.5.1.jar │ │ │ │ ├── objenesis-1.1.jar │ │ │ │ ├── ognl-2.7.3.jar │ │ │ │ ├── org.springframework.aop-3.0.0.RELEASE.jar │ │ │ │ ├── org.springframework.asm-3.0.0.RELEASE.jar │ │ │ │ ├── org.springframework.aspects-3.0.0.RELEASE.jar │ │ │ │ ├── org.springframework.beans-3.0.0.RELEASE.jar │ │ │ │ ├── org.springframework.context-3.0.0.RELEASE.jar │ │ │ │ ├── org.springframework.core-3.0.0.RELEASE.jar │ │ │ │ ├── org.springframework.expression-3.0.0.RELEASE.jar │ │ │ │ ├── org.springframework.web-3.0.0.RELEASE.jar │ │ │ │ ├── paranamer-2.2.jar │ │ │ │ ├── slf4j-api-1.5.8.jar │ │ │ │ ├── slf4j-log4j12-1.5.8.jar │ │ │ │ └── xstream-1.3.1.jar │ │ │ └── web.xml │ │ ├── index.jsp │ │ └── vraptor-3.2.0-SNAPSHOT.jar │ │ └── src │ │ └── main │ │ └── java │ │ ├── br │ │ └── com │ │ │ └── caelum │ │ │ └── example │ │ │ ├── controller │ │ │ ├── Basket.java │ │ │ ├── Baskets.java │ │ │ ├── BasketsController.java │ │ │ ├── ItemsController.java │ │ │ └── PaymentsController.java │ │ │ ├── infra │ │ │ ├── Database.java │ │ │ └── Payments.java │ │ │ ├── model │ │ │ ├── Item.java │ │ │ └── Payment.java │ │ │ └── vraptor │ │ │ └── component │ │ │ ├── CustomJsonDeserializer.java │ │ │ ├── CustomXStreamXMLDeserializer.java │ │ │ └── JSONSerializationCustomized.java │ │ └── log4j.xml └── server │ ├── .classpath │ ├── .project │ ├── .settings │ ├── .jsdtscope │ ├── org.eclipse.jdt.core.prefs │ ├── org.eclipse.wst.common.component │ ├── org.eclipse.wst.common.project.facet.core.xml │ ├── org.eclipse.wst.jsdt.ui.superType.container │ └── org.eclipse.wst.jsdt.ui.superType.name │ ├── WebContent │ ├── META-INF │ │ └── MANIFEST.MF │ ├── WEB-INF │ │ ├── jsp │ │ │ ├── index │ │ │ │ └── index.jsp │ │ │ └── ordering │ │ │ │ └── index.jsp │ │ ├── lib │ │ │ ├── .jars │ │ │ ├── aspectjrt.jar │ │ │ ├── cglib-nodep-2.1_3.jar │ │ │ ├── commons-fileupload-1.2.1.jar │ │ │ ├── commons-io-1.3.2.jar │ │ │ ├── commons-logging.jar │ │ │ ├── commons-vfs-1.0.jar │ │ │ ├── google-collect-1.0-rc2.jar │ │ │ ├── javassist-3.8.0.GA.jar │ │ │ ├── jstl.jar │ │ │ ├── log4j-1.2.15.jar │ │ │ ├── mirror-1.5.1.jar │ │ │ ├── objenesis-1.1.jar │ │ │ ├── ognl-2.7.3.jar │ │ │ ├── paranamer-1.5.jar │ │ │ ├── slf4j-api-1.5.6.jar │ │ │ ├── slf4j-log4j12-1.5.6.jar │ │ │ ├── spring-2.5.5.jar │ │ │ ├── standard-1.1.2.jar │ │ │ ├── vraptor-3.1.2-SNAPSHOT.jar │ │ │ ├── xpp3_min-1.1.4c.jar │ │ │ └── xstream-1.3.1.jar │ │ └── web.xml │ └── index.html │ ├── build-lib │ ├── hamcrest-all-1.2RC3.jar │ ├── junit-4.5.jar │ └── mockito-all-1.8.1.jar │ ├── src │ └── br │ │ └── com │ │ └── caelum │ │ └── vraptor │ │ └── restbucks │ │ ├── Item.java │ │ ├── Order.java │ │ ├── OrderControllerInterceptor.java │ │ ├── OrderDatabase.java │ │ ├── OrderMachineController.java │ │ ├── OrderStateControl.java │ │ ├── Payment.java │ │ ├── Receipt.java │ │ ├── XmlDeserializer.java │ │ ├── XmlSerializer.java │ │ ├── restfulie │ │ ├── CustomRoutes.java │ │ └── StateMachineController.java │ │ └── web │ │ ├── ItemController.java │ │ ├── OrderingController.java │ │ └── PaymentController.java │ └── test │ └── br │ └── com │ └── caelum │ └── vraptor │ └── restbucks │ ├── OrderTest.java │ └── XmlDeserializerTest.java ├── jruby.jar └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | javadoc 3 | *.class 4 | .project 5 | .classpath 6 | .settings/ 7 | bin/ 8 | lib/ 9 | *~ 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ -------------------------------------------------------------------------------- /client/.project: -------------------------------------------------------------------------------- 1 | 2 | restfulie 3 | Sonatype helps open source projects to set up maven repositories on http://oss.sonatype.org. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse. 4 | 5 | 6 | 7 | org.eclipse.jdt.core.javabuilder 8 | 9 | 10 | 11 | org.eclipse.jdt.core.javanature 12 | 13 | -------------------------------------------------------------------------------- /client/jettison-1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/client/jettison-1.1.jar -------------------------------------------------------------------------------- /client/jruby.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/client/jruby.jar -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/Link.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie; 2 | 3 | import br.com.caelum.restfulie.http.Request; 4 | 5 | /** 6 | * A relation to any resource or resource state. 7 | * 8 | * @author guilherme silveira 9 | */ 10 | public interface Link { 11 | 12 | String getHref(); 13 | 14 | String getRel(); 15 | 16 | String getType(); 17 | 18 | Request follow(); 19 | } 20 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/RequestEntry.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie; 2 | 3 | import java.net.URI; 4 | 5 | import br.com.caelum.restfulie.http.Request; 6 | 7 | /** 8 | * Defines methods to access a rest system entry point. 9 | * 10 | * @author guilherme silveira 11 | */ 12 | public interface RequestEntry { 13 | 14 | /** 15 | * Entry point to direct access an uri. 16 | */ 17 | Request at(URI uri); 18 | 19 | /** 20 | * Entry point to direct access an uri. 21 | */ 22 | Request at(String uri) throws RestfulieException ; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/Resource.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie; 19 | 20 | import java.util.List; 21 | 22 | /** 23 | * A single resource.
24 | * Deserialized objects will implement this interface and support transition 25 | * methods. 26 | * 27 | * @author guilherme silveira 28 | * @author lucas souza 29 | */ 30 | public interface Resource { 31 | 32 | /** 33 | * Returns a relation, if found, or null if it was not found. 34 | */ 35 | Link getLink(String rel); 36 | 37 | /** 38 | * Returns whether a link relation exists. 39 | */ 40 | boolean hasLink(String rel); 41 | 42 | /** 43 | * Returns a list of possible relations given this resource's state. 44 | * 45 | * @return the collection of relation 46 | */ 47 | List getLinks(); 48 | 49 | /** 50 | * Returns a list of links for given relation. 51 | */ 52 | List getLinks(String rel); 53 | 54 | } 55 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/Response.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie; 19 | 20 | import java.net.URI; 21 | import java.util.List; 22 | 23 | import br.com.caelum.restfulie.http.Headers; 24 | import br.com.caelum.restfulie.http.Request; 25 | 26 | /** 27 | * A transition execution response.
28 | * Through this interface, one can either access the original response 29 | * (depending on the http layer provider chosen) or, if any, the resulting 30 | * resource. 31 | * 32 | * @author guilherme silveira 33 | */ 34 | public interface Response { 35 | 36 | public int getCode(); 37 | 38 | public String getContent(); 39 | 40 | public List getHeader(String key); 41 | 42 | /** 43 | * Returns the resource if any resource can be parsed from this response's content. 44 | */ 45 | public T getResource(); 46 | 47 | public Headers getHeaders(); 48 | 49 | URI getLocation(); 50 | 51 | public String getType(); 52 | 53 | public Request getRequest(); 54 | 55 | String getStatusLine(); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/RestClient.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie; 19 | 20 | import java.net.URI; 21 | import java.util.concurrent.ExecutorService; 22 | 23 | import org.jvnet.inflector.Pluralizer; 24 | 25 | import br.com.caelum.restfulie.mediatype.MediaTypes; 26 | import br.com.caelum.restfulie.request.RequestDispatcher; 27 | 28 | /** 29 | * Allows resources configuration and access. 30 | * 31 | * @author guilherme silveira 32 | */ 33 | public interface RestClient extends RequestEntry { 34 | 35 | /** 36 | * Returns the http client provider in use. 37 | */ 38 | RequestDispatcher getProvider(); 39 | 40 | /** 41 | * Returns the last accessed URI 42 | */ 43 | URI lastURI(); 44 | 45 | /** 46 | * Returns the media type registry. 47 | */ 48 | MediaTypes getMediaTypes(); 49 | 50 | /** 51 | * Allows someone to use a different request dispatcher. 52 | */ 53 | RestClient use(RequestDispatcher executor); 54 | 55 | /** 56 | * Returns the inflection rules 57 | */ 58 | Pluralizer inflectionRules(); 59 | 60 | /** 61 | * Set custom inflection rules 62 | */ 63 | RestClient withInflector(Pluralizer inflector); 64 | 65 | /** 66 | * Returns the required object for asynchronous requests of this client. 67 | */ 68 | ExecutorService getThreads(); 69 | 70 | } -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/Restfulie.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie; 19 | 20 | import java.net.URI; 21 | import java.net.URISyntaxException; 22 | import br.com.caelum.restfulie.http.DefaultHttpRequest; 23 | import br.com.caelum.restfulie.http.DefaultRestClient; 24 | import br.com.caelum.restfulie.http.Request; 25 | import br.com.caelum.restfulie.relation.CachedEnhancer; 26 | import br.com.caelum.restfulie.relation.DefaultEnhancer; 27 | import br.com.caelum.restfulie.relation.Enhancer; 28 | 29 | /** 30 | * Restfulie's client API entry point.
31 | 32 | * @author guilherme silveira 33 | */ 34 | public class Restfulie { 35 | 36 | /** 37 | * Given an retrieved resource, gives access to restfulie's transition api. 38 | */ 39 | public static Resource resource(T object) { 40 | return (Resource) object; 41 | } 42 | 43 | /** 44 | * Entry point to configure serialization data prior to accessing the resources. 45 | */ 46 | public static RestClient custom() { 47 | return new DefaultRestClient(new CachedEnhancer(new DefaultEnhancer())); 48 | } 49 | 50 | /** 51 | * Entry point to configure the configuration to serialize serialization data prior to accessing the resources of the resources. AAAAAAAAAAAAW YEAH. 52 | */ 53 | public static RestClient custom(Enhancer enhancer) { 54 | return new DefaultRestClient(enhancer); 55 | } 56 | 57 | /** 58 | * Entry point to direct access an uri. 59 | */ 60 | public static Request at(URI uri) { 61 | RestClient client = custom(); 62 | return new DefaultHttpRequest(uri, client).accept("application/xml"); 63 | } 64 | 65 | /** 66 | * Entry point to direct access an uri. 67 | * @throws URISyntaxException 68 | */ 69 | public static Request at(String uri) { 70 | try { 71 | return at(new URI(uri)); 72 | } catch (URISyntaxException e) { 73 | throw new RestfulieException("Invalid URI Syntax for " + uri, e); 74 | } 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/RestfulieException.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie; 19 | 20 | 21 | /** 22 | * Anything went wrong with executing a request. 23 | * @author guilherme silveira 24 | * 25 | */ 26 | public class RestfulieException extends RuntimeException { 27 | 28 | private static final long serialVersionUID = 3766001815829700069L; 29 | 30 | public RestfulieException(String string) { 31 | super(string); 32 | } 33 | 34 | public RestfulieException(String msg, Throwable e) { 35 | super(msg, e); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/client/DefaultLinkConverter.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie.client; 19 | 20 | import br.com.caelum.restfulie.RestClient; 21 | import br.com.caelum.restfulie.http.DefaultRelation; 22 | 23 | import com.thoughtworks.xstream.converters.Converter; 24 | import com.thoughtworks.xstream.converters.MarshallingContext; 25 | import com.thoughtworks.xstream.converters.UnmarshallingContext; 26 | import com.thoughtworks.xstream.io.HierarchicalStreamReader; 27 | import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 28 | 29 | /** 30 | * Default transition converter.
31 | * Injects the current deserializer to all transitions. 32 | * 33 | * @author guilherme silveira 34 | * 35 | */ 36 | public class DefaultLinkConverter implements Converter { 37 | 38 | private final RestClient client; 39 | 40 | public DefaultLinkConverter(RestClient client) { 41 | this.client = client; 42 | } 43 | 44 | public void marshal(Object source, HierarchicalStreamWriter writer, 45 | MarshallingContext context) { 46 | // marshalling does not do anything 47 | } 48 | 49 | public Object unmarshal(HierarchicalStreamReader reader, 50 | UnmarshallingContext context) { 51 | String rel = reader.getAttribute("rel"); 52 | String href = reader.getAttribute("href"); 53 | String type = reader.getAttribute("type"); 54 | return new DefaultRelation(rel, href, type, client); 55 | } 56 | 57 | public boolean canConvert(Class type) { 58 | return type.equals(DefaultRelation.class); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/feature/CurlLogging.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.feature; 2 | 3 | import java.net.URI; 4 | 5 | import br.com.caelum.restfulie.Response; 6 | import br.com.caelum.restfulie.http.Request; 7 | import br.com.caelum.restfulie.request.RequestChain; 8 | 9 | public class CurlLogging implements RequestFeature { 10 | 11 | public Response process(RequestChain chain, Request request, String verb, 12 | URI uri, Object payload) { 13 | if(verb.equals("POST")) { 14 | System.out.println(String.format("curl -v %s -H 'Content-type: %s' -d '%s'", uri, request.getHeaders().get("Content-type"), payload)); 15 | } else { 16 | System.out.println(String.format("curl -v %s", uri)); 17 | } 18 | return chain.next(request, verb, uri, payload); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/feature/Features.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.feature; 2 | 3 | /** 4 | * Lookup methods for features 5 | * @author jose donizetti 6 | */ 7 | public class Features { 8 | 9 | public static ResponseFeature throwError() { 10 | return new ThrowError(); 11 | } 12 | 13 | public static RequestFeature retryWhenUnavaiable() { 14 | return new RetryWhenUnavailable(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/feature/FollowRedirects.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.feature; 2 | 3 | import java.net.URI; 4 | 5 | import br.com.caelum.restfulie.Response; 6 | import br.com.caelum.restfulie.RestClient; 7 | import br.com.caelum.restfulie.http.Request; 8 | import br.com.caelum.restfulie.request.ResponseChain; 9 | 10 | /** 11 | * A feature that automatically follows 300~399 and 201 codes by using the 12 | * Location header. 13 | * 14 | * @author guilherme silveira 15 | */ 16 | public class FollowRedirects implements ResponseFeature { 17 | 18 | private final RestClient client; 19 | 20 | public FollowRedirects(RestClient client) { 21 | this.client = client; 22 | } 23 | 24 | public Response process(ResponseChain chain, Response response) { 25 | if (shouldRedirect(response)) { 26 | String uri = response.getHeader("Location").get(0); 27 | Request request = response.getRequest(); 28 | if (uri.charAt(0) == '/') { 29 | URI target = request.getURI().resolve(uri); 30 | return client.at(target).addHeaders(request.getHeaders()).get(); 31 | } 32 | return client.at(uri).addHeaders(request.getHeaders()).get(); 33 | } 34 | return response; 35 | } 36 | 37 | /** 38 | * Extension point that redirects 201 and 3XX. Overwrite to redirect only 39 | * the ones you want. 40 | */ 41 | protected boolean shouldRedirect(Response response) { 42 | return response.getCode() / 100 == 3 || response.getCode() == 201; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/feature/RequestFeature.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.feature; 2 | 3 | import java.net.URI; 4 | 5 | import br.com.caelum.restfulie.Response; 6 | import br.com.caelum.restfulie.http.Request; 7 | import br.com.caelum.restfulie.request.RequestChain; 8 | 9 | /** 10 | * A request feature is a interceptor that will be invoked prior to the 11 | * execution of the request. Compose http behavior by using your own features in the feature stack. 12 | * 13 | * @author guilherme silveira 14 | */ 15 | public interface RequestFeature { 16 | 17 | Response process(RequestChain chain, Request request, String verb, URI uri, 18 | Object payload); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/feature/ResponseFeature.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.feature; 2 | 3 | import br.com.caelum.restfulie.Response; 4 | import br.com.caelum.restfulie.request.ResponseChain; 5 | 6 | public interface ResponseFeature { 7 | 8 | Response process(ResponseChain responseChain, Response response); 9 | 10 | } 11 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/feature/RetryWhenUnavailable.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.feature; 2 | 3 | import java.net.URI; 4 | 5 | import br.com.caelum.restfulie.Response; 6 | import br.com.caelum.restfulie.http.Request; 7 | import br.com.caelum.restfulie.request.RequestChain; 8 | 9 | /** 10 | * Retry when unavailable feature. Default code 503. 11 | * 12 | * @author jose donizetti 13 | */ 14 | public class RetryWhenUnavailable implements RequestFeature { 15 | 16 | private Response response; 17 | 18 | public Response process(RequestChain chain, Request request, String verb, URI uri, Object payload) { 19 | 20 | response = chain.next(request, verb, uri, payload); 21 | if(shouldRetry()) { 22 | response = chain.next(request, verb, uri, payload); 23 | } 24 | 25 | return response; 26 | } 27 | 28 | protected boolean shouldRetry() { 29 | return response.getCode() == 503; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/AsynchronousRequest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http; 2 | 3 | import java.util.concurrent.Callable; 4 | import br.com.caelum.restfulie.Response; 5 | 6 | /** 7 | * An asynchronous HTTP request. 8 | * 9 | * @author samuel portela 10 | */ 11 | public class AsynchronousRequest implements Callable { 12 | 13 | private final Request request; 14 | private final HttpMethod httpMethod; 15 | private final Object payload; 16 | private final RequestCallback requestCallback; 17 | 18 | public AsynchronousRequest(Request request, HttpMethod httpMethod, RequestCallback requestCallback) { 19 | this(request, httpMethod, null, requestCallback); 20 | } 21 | 22 | public AsynchronousRequest(Request request, HttpMethod httpMethod, Object payload, RequestCallback requestCallback) { 23 | this.request = request; 24 | this.httpMethod = httpMethod; 25 | this.payload = payload; 26 | this.requestCallback = requestCallback; 27 | } 28 | 29 | public Request getRequest() { 30 | return request; 31 | } 32 | 33 | public HttpMethod getHttpMethod() { 34 | return httpMethod; 35 | } 36 | 37 | public Object getPayload() { 38 | return payload; 39 | } 40 | 41 | public Response call() { 42 | Response response = null; 43 | try { 44 | response = getHttpMethod().execute(request, payload); 45 | requestCallback.callback(response); 46 | } 47 | catch (Exception e) { 48 | requestCallback.onException(request, httpMethod, e); 49 | } 50 | return response; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/ContentProcessor.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie.http; 19 | 20 | import java.io.IOException; 21 | 22 | /** 23 | * Responsible for parsing http request's results. 24 | * 25 | * @author guilherme silveira 26 | */ 27 | public interface ContentProcessor { 28 | 29 | public String read() throws IOException; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/DefaultHeader.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http; 2 | 3 | public class DefaultHeader implements Header { 4 | 5 | private final String name; 6 | private final String value; 7 | 8 | public DefaultHeader(String name, String value) { 9 | this.name = name; 10 | this.value = value; 11 | } 12 | 13 | 14 | public String getName() { 15 | return this.name; 16 | } 17 | 18 | public String getValue() { 19 | return this.value; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/DefaultRelation.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie.http; 19 | 20 | import java.net.URI; 21 | 22 | import br.com.caelum.restfulie.Link; 23 | import br.com.caelum.restfulie.RestClient; 24 | 25 | /** 26 | * Default implementation of a transition. 27 | * 28 | * @author guilherme silveira 29 | * @author lucas souza 30 | */ 31 | public class DefaultRelation implements Link { 32 | 33 | private String rel; 34 | private String href; 35 | private String type; 36 | private final RestClient client; 37 | 38 | public DefaultRelation(String rel, String href, String type, RestClient client) { 39 | this.rel = rel; 40 | this.href = href; 41 | this.client = client; 42 | this.type = type; 43 | } 44 | 45 | public String getHref() { 46 | return href; 47 | } 48 | 49 | public String getRel() { 50 | return rel; 51 | } 52 | 53 | public String getType() { 54 | return type; 55 | } 56 | 57 | public Request follow() { 58 | if (href.startsWith("/")) { 59 | URI uri = client.lastURI(); 60 | return client.at(extractHost(uri) + href).as(type); 61 | } 62 | return client.at(href).as(type); 63 | } 64 | 65 | private String extractHost(URI uri) { 66 | return uri.toString().replaceFirst(uri.getPath() + "$", ""); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return rel + " - " + href; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/Header.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http; 2 | 3 | public interface Header { 4 | 5 | String getName(); 6 | String getValue(); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/Headers.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http; 2 | 3 | import java.util.List; 4 | 5 | import br.com.caelum.restfulie.Link; 6 | 7 | public interface Headers extends Iterable { 8 | 9 | public abstract String getMain(String string); 10 | 11 | /** 12 | * Returns a list with all values for this header 13 | */ 14 | List get(String key); 15 | 16 | /** 17 | * Returns the first appearance of this header 18 | */ 19 | String getFirst(String key); 20 | 21 | /** 22 | * Returns all links on of this header 23 | */ 24 | public abstract List getLinks(); 25 | 26 | /** 27 | * Returns a link given its rel 28 | */ 29 | public abstract Link getLink(String rel); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/HttpClientProvider.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http; 2 | 3 | import java.net.URI; 4 | 5 | import br.com.caelum.restfulie.Response; 6 | 7 | /** 8 | * An http client request provider.
9 | * Implementations provide access to requests over http. 10 | * 11 | * @author guilherme silveira 12 | */ 13 | public interface HttpClientProvider { 14 | 15 | Response process(Request request, String verb, URI uri, Object payload); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/HttpMethod.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie.http; 19 | 20 | import br.com.caelum.restfulie.Response; 21 | 22 | public enum HttpMethod { 23 | 24 | PATCH, OPTIONS, TRACE, 25 | 26 | GET() { 27 | public Response execute(Request request, Object payload) throws Exception { 28 | return request.get(); 29 | } 30 | }, 31 | 32 | POST() { 33 | public Response execute(Request request, Object payload) throws Exception { 34 | return request.post(payload); 35 | } 36 | }, 37 | 38 | DELETE() { 39 | public Response execute(Request request, Object payload) throws Exception { 40 | return request.delete(); 41 | } 42 | }, 43 | 44 | PUT() { 45 | public Response execute(Request request, Object payload) throws Exception { 46 | return request.put(payload); 47 | } 48 | }; 49 | 50 | public Response execute(Request request, Object payload) throws Exception { 51 | return this.execute(request, payload); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/HttpURLConnectionContentProcessor.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie.http; 19 | 20 | import java.io.BufferedReader; 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | import java.io.InputStreamReader; 24 | import java.net.HttpURLConnection; 25 | 26 | /** 27 | * Default implementation based on an httpurlconnection. Will basically process 28 | * all input data and return it through the read method. 29 | * 30 | * @author guilherme silveira 31 | */ 32 | public class HttpURLConnectionContentProcessor implements ContentProcessor { 33 | 34 | private final HttpURLConnection connection; 35 | 36 | private String cachedContent = null; 37 | 38 | public HttpURLConnectionContentProcessor(HttpURLConnection connection) { 39 | this.connection = connection; 40 | } 41 | 42 | public String read() throws IOException { 43 | if(cachedContent!=null) { 44 | return cachedContent; 45 | } 46 | InputStream stream = (InputStream) connection.getContent(); 47 | BufferedReader reader = new BufferedReader( 48 | new InputStreamReader(stream)); 49 | StringBuilder content = new StringBuilder(); 50 | while (true) { 51 | String partial = reader.readLine(); 52 | if (partial == null) { 53 | break; 54 | } 55 | if (content.length() != 0) { 56 | content.append("\n"); 57 | } 58 | content.append(partial); 59 | } 60 | cachedContent = content.toString(); 61 | return cachedContent; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/IdentityContentProcessor.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie.http; 19 | 20 | import java.io.IOException; 21 | 22 | /** 23 | * Simple processor which does nothing. 24 | * 25 | * @author guilherme silveira 26 | */ 27 | public class IdentityContentProcessor implements ContentProcessor { 28 | 29 | public String read() throws IOException { 30 | return null; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/Request.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http; 2 | 3 | import java.net.URI; 4 | import java.util.Map; 5 | import java.util.concurrent.Future; 6 | 7 | import br.com.caelum.restfulie.Response; 8 | import br.com.caelum.restfulie.feature.RequestFeature; 9 | import br.com.caelum.restfulie.feature.ResponseFeature; 10 | 11 | /** 12 | * An http request. Defaults verb to GET. 13 | * 14 | * @author guilherme silveira 15 | */ 16 | public interface Request { 17 | 18 | /** 19 | * Access the resource, returning the request result. 20 | * @return 21 | */ 22 | Response access(); 23 | 24 | /** 25 | * Adds a request header. 26 | */ 27 | Request with(String key, String value); 28 | 29 | /** 30 | * Sets the verb to use (default GET). 31 | */ 32 | Request using(String verb); 33 | 34 | 35 | Request as(String contentType); 36 | 37 | /** 38 | * Accepts one specific content type. 39 | */ 40 | Request accept(String type); 41 | 42 | Response get(); 43 | 44 | Future getAsync(RequestCallback requestCallback); 45 | 46 | Response post(T object); 47 | 48 | Future postAsync(T payload, RequestCallback requestCallback); 49 | 50 | Response put(T object); 51 | 52 | Future putAsync(T payload, RequestCallback requestCallback); 53 | 54 | Response patch(T object); 55 | 56 | Response delete(); 57 | 58 | Future deleteAsync(RequestCallback requestCallback); 59 | 60 | Response options(); 61 | 62 | Response head(); 63 | 64 | Map getHeaders(); 65 | Request addHeaders(Map headers); 66 | 67 | /** 68 | * Shortcut for accepting and sending some media type. It has the same effect 69 | * as invoking as(type).accepts(type) 70 | */ 71 | Request handling(String type); 72 | 73 | URI getURI(); 74 | 75 | /** 76 | * Add feature to RequestStack 77 | */ 78 | Request with(RequestFeature feature); 79 | /** 80 | * Add feature to the ResponseStack 81 | */ 82 | Request with(ResponseFeature feature); 83 | 84 | } 85 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/RequestCallback.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import br.com.caelum.restfulie.Response; 6 | 7 | /** 8 | * The RequestCallback is used by AsynchronousRequest to provide asynchronous HTTP requests using 9 | * Threads. As the only thing above a Thread is the JVM, it would not be possible to catch the 10 | * exception without using the future.get() method. Because of this, currently the only way to catch 11 | * exceptions thrown during the asynchronous request execution is overwriting the onException 12 | * method. Maybe in the future, if a better solution is found, this code can be improved (keeping 13 | * the compatibility). 14 | * 15 | * @author samuel portela 16 | */ 17 | public abstract class RequestCallback { 18 | 19 | private static final Logger logger = LoggerFactory.getLogger(RequestCallback.class); 20 | 21 | /** 22 | * Method to be called when the response comes. 23 | */ 24 | public abstract void callback(Response response); 25 | 26 | /** 27 | * Method to be called when something goes wrong with the request. 28 | */ 29 | public void onException(Request request, HttpMethod httpMethod, Exception e) { 30 | e.printStackTrace(); 31 | logger.error("An asynchronous request could not be made to " + request.getURI() + " using the HTTP " + httpMethod + " method", e); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/apache/ApacheHeaders.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.apache; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | import org.apache.http.Header; 8 | import org.apache.http.HttpResponse; 9 | 10 | import br.com.caelum.restfulie.Link; 11 | import br.com.caelum.restfulie.RestClient; 12 | import br.com.caelum.restfulie.http.DefaultHeader; 13 | import br.com.caelum.restfulie.http.DefaultRelation; 14 | import br.com.caelum.restfulie.http.Headers; 15 | 16 | public class ApacheHeaders implements Headers { 17 | private final HttpResponse response; 18 | private List links; 19 | private final RestClient client; 20 | 21 | public ApacheHeaders(HttpResponse response, RestClient client) { 22 | this.response = response; 23 | this.client = client; 24 | this.links = new ArrayList(); 25 | } 26 | 27 | public String getMain(String key) { 28 | return getFirst(key).split(";")[0]; 29 | } 30 | 31 | public List get(String key) { 32 | Header[] values = response.getHeaders(key); 33 | List list = new ArrayList(); 34 | for(Header h : values) { 35 | list.add(h.getValue()); 36 | } 37 | return list; 38 | } 39 | 40 | public String getFirst(String key) { 41 | Header[] headers = response.getHeaders(key); 42 | return headers != null && headers.length > 0 ? get(key).get(0) : ""; 43 | } 44 | 45 | //TODO doni rever testes 46 | public List getLinks() { 47 | if(links.isEmpty()) { 48 | for ( String tempLinks : get("link")) { 49 | for(String link : tempLinks.split(",")){ 50 | String[] split = link.split(";"); 51 | String href = split[0].trim().substring(1,split[0].trim().length()-1); 52 | String rel = split[1].trim().substring(5,split[1].trim().length()-1); 53 | this.links.add(new DefaultRelation(rel,href,"",client)); 54 | } 55 | } 56 | } 57 | return links; 58 | } 59 | 60 | public Link getLink(String rel) { 61 | getLinks(); 62 | for(Link link : links) { 63 | if(link.getRel().equals(rel)) { 64 | return link; 65 | } 66 | } 67 | return null; 68 | } 69 | 70 | public Iterator iterator() { 71 | List headers = new ArrayList(); 72 | for(Header apacheHeader : response.getAllHeaders()) { 73 | br.com.caelum.restfulie.http.Header h = new DefaultHeader(apacheHeader.getName(),apacheHeader.getValue()); 74 | headers.add(h); 75 | } 76 | return headers.iterator(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/apache/ApacheResponse.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.apache; 2 | 3 | import java.io.IOException; 4 | import java.net.URI; 5 | import java.net.URISyntaxException; 6 | import java.util.List; 7 | 8 | import org.apache.http.HttpEntity; 9 | import org.apache.http.HttpResponse; 10 | import org.apache.http.util.EntityUtils; 11 | 12 | import br.com.caelum.restfulie.Response; 13 | import br.com.caelum.restfulie.RestClient; 14 | import br.com.caelum.restfulie.RestfulieException; 15 | import br.com.caelum.restfulie.http.Headers; 16 | import br.com.caelum.restfulie.http.Request; 17 | 18 | public class ApacheResponse implements Response { 19 | 20 | private final HttpResponse response; 21 | private final RestClient client; 22 | private HttpEntity entity; 23 | private final Request details; 24 | 25 | public ApacheResponse(HttpResponse response, RestClient client, Request details) { 26 | this.response = response; 27 | this.client = client; 28 | this.details = details; 29 | this.entity = response.getEntity(); 30 | } 31 | 32 | public int getCode() { 33 | return response.getStatusLine().getStatusCode(); 34 | } 35 | 36 | public String getContent() { 37 | if (entity == null) { 38 | return ""; 39 | } 40 | try { 41 | long len = entity.getContentLength(); 42 | if (len < 10 * 1024 * 1024) { 43 | return EntityUtils.toString(entity); 44 | } else { 45 | return ""; 46 | } 47 | } catch (IOException ex) { 48 | throw new RestfulieException("Unable to parse response content", ex); 49 | } 50 | } 51 | 52 | public List getHeader(String key) { 53 | return getHeaders().get(key); 54 | } 55 | 56 | public T getResource() { 57 | String contentType = getType(); 58 | String content = getContent(); 59 | return (T) client.getMediaTypes().forContentType(contentType) 60 | .unmarshal(content, client); 61 | } 62 | 63 | public String getType() { 64 | return getHeaders().getMain("Content-Type"); 65 | } 66 | 67 | public Headers getHeaders() { 68 | return new ApacheHeaders(response,client); 69 | } 70 | 71 | public void discard() throws IOException { 72 | response.getEntity().consumeContent(); 73 | } 74 | 75 | public URI getLocation() { 76 | try { 77 | String location = getHeaders().getFirst("Location"); 78 | if(location == null || location.equals("")) 79 | return getRequest().getURI(); 80 | else 81 | return new URI(location); 82 | } catch (URISyntaxException e) { 83 | throw new RestfulieException("Invalid URI received as a response", e); 84 | } 85 | } 86 | 87 | public Request getRequest() { 88 | return details; 89 | } 90 | 91 | public String getStatusLine() { 92 | return response.getStatusLine().getReasonPhrase(); 93 | } 94 | 95 | } -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/BadRequestException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 400 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class BadRequestException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 2258661686367560426L; 14 | 15 | public BadRequestException(String message) { 16 | super(message); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/ClientException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 402, 406, 408, 411, 413..499 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class ClientException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 1388113693249430023L; 14 | 15 | public ClientException(String message) { 16 | super(message); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/ConflictException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 409 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class ConflictException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 2536017915960455465L; 14 | 15 | public ConflictException(String message) { 16 | super(message); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/ForbiddenException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 403 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class ForbiddenException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 8835699291841679562L; 14 | 15 | public ForbiddenException(String message) { 16 | super(message); 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/GoneException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 410 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class GoneException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 4057096870697483501L; 14 | 15 | public GoneException(String message) { 16 | super(message); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/MethodNotAllowedException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 405 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class MethodNotAllowedException extends RestfulieException{ 12 | 13 | private static final long serialVersionUID = 3491771205478082847L; 14 | 15 | public MethodNotAllowedException(String message) { 16 | super(message); 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/NotFoundException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 404 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class NotFoundException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 4646962023618885027L; 14 | 15 | public NotFoundException(String message) { 16 | super(message); 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/NotImplementedException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 501 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class NotImplementedException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 6469960679609931636L; 14 | 15 | public NotImplementedException(String message) { 16 | super(message); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/PreconditionFailedException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 412 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class PreconditionFailedException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 6515303038251543597L; 14 | 15 | public PreconditionFailedException(String message) { 16 | super(message); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/ProxyAuthenticationRequiredException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 407 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class ProxyAuthenticationRequiredException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = -5960166008854619628L; 14 | 15 | public ProxyAuthenticationRequiredException(String message) { 16 | super(message); 17 | } 18 | 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/RedicetionException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 300.399 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class RedicetionException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 3063468545997174918L; 14 | 15 | public RedicetionException(String message) { 16 | super(message); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/ServerException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 500, 502..599 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class ServerException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 6099610123350347391L; 14 | 15 | public ServerException(String message) { 16 | super(message); 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/UnauthorizedException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * 401 response code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class UnauthorizedException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = 2963522403558660414L; 14 | 15 | public UnauthorizedException(String message) { 16 | super(message); 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/error/UnknowCodeException.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.error; 2 | 3 | import br.com.caelum.restfulie.RestfulieException; 4 | 5 | /** 6 | * Unkown responde code 7 | * 8 | * @author jose donizetti 9 | * 10 | */ 11 | public class UnknowCodeException extends RestfulieException { 12 | 13 | private static final long serialVersionUID = -8286986069712242964L; 14 | 15 | public UnknowCodeException(String message) { 16 | super(message); 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/http/javanet/MapHeaders.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.javanet; 2 | 3 | import java.util.Iterator; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import br.com.caelum.restfulie.Link; 8 | import br.com.caelum.restfulie.http.Header; 9 | import br.com.caelum.restfulie.http.Headers; 10 | 11 | public class MapHeaders implements Headers{ 12 | 13 | private final Map> fields; 14 | 15 | public MapHeaders(Map> fields) { 16 | this.fields = fields; 17 | } 18 | 19 | public List get(String key) { 20 | return fields.get(key); 21 | } 22 | 23 | public String getMain(String key) { 24 | if(!fields.containsKey(key)) { 25 | throw new IllegalArgumentException("Unable to parse as field does not exist."); 26 | } 27 | return get(key).get(0).split(";")[0]; 28 | } 29 | 30 | public String getFirst(String key) { 31 | return get(key).get(0); 32 | } 33 | 34 | public List getLinks() { 35 | return null; 36 | } 37 | 38 | public Link getLink(String rel) { 39 | // TODO Auto-generated method stub 40 | return null; 41 | } 42 | 43 | public Iterator
iterator() { 44 | // TODO Auto-generated method stub 45 | return null; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/mediatype/EnhancedList.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.mediatype; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.Iterator; 6 | import java.util.List; 7 | import java.util.ListIterator; 8 | 9 | public class EnhancedList implements List { 10 | 11 | private List elements = new ArrayList(); 12 | 13 | public void add(int index, T element) { 14 | elements.add(index, element); 15 | } 16 | 17 | public boolean add(T e) { 18 | return elements.add(e); 19 | } 20 | 21 | public boolean addAll(Collection c) { 22 | return elements.addAll(c); 23 | } 24 | 25 | public boolean addAll(int index, Collection c) { 26 | return elements.addAll(index, c); 27 | } 28 | 29 | public void clear() { 30 | elements.clear(); 31 | } 32 | 33 | public boolean contains(Object o) { 34 | return elements.contains(o); 35 | } 36 | 37 | public boolean containsAll(Collection c) { 38 | return elements.containsAll(c); 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | return elements.equals(o); 44 | } 45 | 46 | public T get(int index) { 47 | return elements.get(index); 48 | } 49 | 50 | @Override 51 | public int hashCode() { 52 | return elements.hashCode(); 53 | } 54 | 55 | public int indexOf(Object o) { 56 | return elements.indexOf(o); 57 | } 58 | 59 | public boolean isEmpty() { 60 | return elements.isEmpty(); 61 | } 62 | 63 | public Iterator iterator() { 64 | return elements.iterator(); 65 | } 66 | 67 | public int lastIndexOf(Object o) { 68 | return elements.lastIndexOf(o); 69 | } 70 | 71 | public ListIterator listIterator() { 72 | return elements.listIterator(); 73 | } 74 | 75 | public ListIterator listIterator(int index) { 76 | return elements.listIterator(index); 77 | } 78 | 79 | public T remove(int index) { 80 | return elements.remove(index); 81 | } 82 | 83 | public boolean remove(Object o) { 84 | return elements.remove(o); 85 | } 86 | 87 | public boolean removeAll(Collection c) { 88 | return elements.removeAll(c); 89 | } 90 | 91 | public boolean retainAll(Collection c) { 92 | return elements.retainAll(c); 93 | } 94 | 95 | public T set(int index, T element) { 96 | return elements.set(index, element); 97 | } 98 | 99 | public int size() { 100 | return elements.size(); 101 | } 102 | 103 | public List subList(int fromIndex, int toIndex) { 104 | return elements.subList(fromIndex, toIndex); 105 | } 106 | 107 | public Object[] toArray() { 108 | return elements.toArray(); 109 | } 110 | 111 | public T[] toArray(T[] a) { 112 | return elements.toArray(a); 113 | } 114 | 115 | @Override 116 | public String toString() { 117 | return elements.toString(); 118 | } 119 | 120 | private Object readResolve() { 121 | if (elements == null) { 122 | elements = new ArrayList(); 123 | } 124 | return this; 125 | 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/mediatype/FormEncoded.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.mediatype; 2 | 3 | import java.io.IOException; 4 | import java.io.Writer; 5 | import java.net.URLEncoder; 6 | import java.util.Map; 7 | import java.util.Map.Entry; 8 | 9 | import br.com.caelum.restfulie.RestClient; 10 | 11 | /** 12 | * A media type that supports x-www-form-urlencoded. 13 | * 14 | * @author guilherme silveira 15 | */ 16 | public class FormEncoded implements MediaType { 17 | 18 | public boolean answersTo(String type) { 19 | return "application/x-www-form-urlencoded".equals(type); 20 | } 21 | 22 | @SuppressWarnings("unchecked") 23 | public void marshal(T payload, Writer writer, RestClient client) throws IOException { 24 | if(payload.getClass().equals(String.class)) { 25 | writer.append(String.class.cast(payload)); 26 | return; 27 | } 28 | Map params = (Map) payload; 29 | int at = 0; 30 | for (Entry param : params.entrySet()) { 31 | writer.append(URLEncoder.encode(param.getKey())); 32 | writer.append("="); 33 | writer.append(URLEncoder.encode(param.getValue())); 34 | if (++at != params.size()) { 35 | writer.append("&"); 36 | } 37 | } 38 | } 39 | 40 | public T unmarshal(String content, RestClient client) { 41 | return null; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/mediatype/JsonMediaType.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.mediatype; 2 | 3 | import java.io.IOException; 4 | import java.io.Writer; 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | import java.util.List; 8 | 9 | import br.com.caelum.restfulie.RestClient; 10 | import br.com.caelum.restfulie.client.DefaultLinkConverter; 11 | import br.com.caelum.restfulie.relation.CachedEnhancer; 12 | import br.com.caelum.restfulie.relation.DefaultEnhancer; 13 | import br.com.caelum.restfulie.relation.Enhancer; 14 | 15 | import com.thoughtworks.xstream.XStream; 16 | import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; 17 | 18 | /** 19 | * A xstream + jettison based media type implementation. 20 | * 21 | * @author guilherme silveira 22 | */ 23 | @SuppressWarnings("unchecked") 24 | public class JsonMediaType implements MediaType { 25 | 26 | public static final String TYPE = "application/json"; 27 | 28 | private final List types = Arrays.asList("application/json", "text/json", "json"); 29 | 30 | private final XStreamHelper helper; 31 | 32 | private final XStream xstream; 33 | 34 | public JsonMediaType(Enhancer enhancer) { 35 | helper = new XStreamHelper(new JettisonMappedXmlDriver(), enhancer); 36 | this.xstream = helper.getXStream(getTypesToEnhance(), getCollectionNames()); 37 | configure(xstream); 38 | } 39 | public JsonMediaType() { 40 | this(new CachedEnhancer(new DefaultEnhancer())); 41 | } 42 | 43 | 44 | /** 45 | * Allows xstream further configuration. 46 | */ 47 | protected void configure(XStream xstream) { 48 | } 49 | 50 | public boolean answersTo(String type) { 51 | return types.contains(type); 52 | } 53 | 54 | public void marshal(T payload, Writer writer, RestClient client) throws IOException { 55 | if(payload.getClass().equals(String.class)) { 56 | writer.append(String.class.cast(payload)); 57 | return; 58 | } 59 | xstream.toXML(payload, writer); 60 | writer.flush(); 61 | } 62 | 63 | public T unmarshal(String content, RestClient client) { 64 | xstream.registerConverter(new DefaultLinkConverter(client)); 65 | return (T) xstream.fromXML(content); 66 | } 67 | 68 | protected List getTypesToEnhance() { 69 | return Collections.emptyList(); 70 | } 71 | protected List getCollectionNames() { 72 | return Collections.emptyList(); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/mediatype/MediaType.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.mediatype; 2 | 3 | import java.io.IOException; 4 | import java.io.Writer; 5 | 6 | import br.com.caelum.restfulie.RestClient; 7 | 8 | /** 9 | * A media type handler, capable of marshalling into representations related to 10 | * this media type and unmarshalling objects from those representations. 11 | * 12 | * @author guilherme silveira 13 | */ 14 | public interface MediaType { 15 | 16 | boolean answersTo(String type); 17 | 18 | void marshal(T payload, Writer writer, RestClient client) throws IOException; 19 | 20 | /** 21 | * Unmarshalling should always be to something to be analyzed.
22 | * Remember *not* to expect too much from unmarshalling, your server might 23 | * have provided you something you did not expect. This is REST's idea. 24 | */ 25 | T unmarshal(String content, RestClient client); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/mediatype/MediaTypes.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.mediatype; 2 | 3 | import java.util.LinkedList; 4 | 5 | import br.com.caelum.restfulie.RestfulieException; 6 | 7 | /** 8 | * A media type registry.
9 | * Invoke register to add a new media types. Whenever two media type handlers can 10 | * handle the same media type, the latest added will be used to resolve the 11 | * conflict. 12 | * 13 | * @author guilherme silveira 14 | */ 15 | public class MediaTypes { 16 | 17 | public static final String XML = "application/xml"; 18 | 19 | private final LinkedList types = new LinkedList(); 20 | 21 | public MediaType forContentType(String searching) { 22 | for (MediaType type : types) { 23 | if (type.answersTo(searching)) { 24 | return type; 25 | } 26 | } 27 | throw new RestfulieException("Unsupported media type '" + searching + "'"); 28 | } 29 | 30 | public void register(MediaType mediaType) { 31 | this.types.addFirst(mediaType); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/opensearch/SearchDescription.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.opensearch; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.thoughtworks.xstream.annotations.XStreamAlias; 7 | import com.thoughtworks.xstream.annotations.XStreamImplicit; 8 | 9 | /** 10 | * Openserach document representation 11 | * @author jose donizetti 12 | * 13 | */ 14 | @XStreamAlias("OpenSearchDescription") 15 | public class SearchDescription { 16 | 17 | @XStreamAlias("ShortName") 18 | private String shortName; 19 | 20 | @XStreamAlias("Description") 21 | private String description; 22 | 23 | @XStreamAlias("Tags") 24 | private Tags tags; 25 | 26 | @XStreamAlias("Contact") 27 | private String contact; 28 | 29 | @XStreamImplicit(itemFieldName="Url") 30 | private List urls; 31 | 32 | public SearchDescription() { 33 | tags = new Tags(); 34 | urls = new ArrayList(); 35 | } 36 | 37 | public List getUrls() { 38 | return urls; 39 | } 40 | 41 | public void setUrls(List urls) { 42 | this.urls = urls; 43 | } 44 | 45 | public String getShortName() { 46 | return shortName; 47 | } 48 | 49 | public void setShortName(String shortName) { 50 | this.shortName = shortName; 51 | } 52 | 53 | public String getDescription() { 54 | return description; 55 | } 56 | 57 | public void setDescription(String description) { 58 | this.description = description; 59 | 60 | } 61 | 62 | public String getContact() { 63 | return contact; 64 | } 65 | 66 | public void setContact(String contact) { 67 | this.contact = contact; 68 | } 69 | 70 | public Tags getTags() { 71 | return tags; 72 | } 73 | 74 | public void setTags(Tags tags) { 75 | this.tags = tags; 76 | } 77 | 78 | public Url use(String string) { 79 | for(Url url : urls) { 80 | if(url.getType().equals(string)) { 81 | return url; 82 | } 83 | } 84 | throw new RuntimeException("no such url"); 85 | } 86 | 87 | 88 | } 89 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/opensearch/Tags.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.opensearch; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * OpenSearch tags element 8 | * @author jose donizetti 9 | */ 10 | public class Tags { 11 | private List tags = new ArrayList(); 12 | 13 | public int size() { 14 | return tags.size(); 15 | } 16 | 17 | public boolean add(String o) { 18 | return tags.add(o); 19 | } 20 | 21 | public String get(int index) { 22 | return tags.get(index); 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/opensearch/Url.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.opensearch; 2 | 3 | import br.com.caelum.restfulie.RestClient; 4 | import br.com.caelum.restfulie.http.Request; 5 | 6 | import com.thoughtworks.xstream.annotations.XStreamAlias; 7 | import com.thoughtworks.xstream.annotations.XStreamAsAttribute; 8 | 9 | /** 10 | * OpenSearch URL representation 11 | * @author jose donizetti 12 | * 13 | */ 14 | @XStreamAlias("Url") 15 | public class Url { 16 | 17 | @XStreamAlias("type") 18 | @XStreamAsAttribute 19 | private String type; 20 | 21 | @XStreamAsAttribute 22 | @XStreamAlias("template") 23 | private String template; 24 | 25 | private int page; 26 | private String term = ""; 27 | 28 | private final RestClient client; 29 | 30 | public Url(String type, String template, RestClient client) { 31 | this.type = type; 32 | this.template = template; 33 | this.client = client; 34 | } 35 | 36 | public String getType() { 37 | return type; 38 | } 39 | 40 | public void setType(String type) { 41 | this.type = type; 42 | } 43 | 44 | public String getTemplate() { 45 | return template; 46 | } 47 | 48 | public void setTemplate(String template) { 49 | this.template = template; 50 | } 51 | 52 | public String toUri() { 53 | String url = template.replace("{searchTerms}", term); 54 | url = url.replace("{startPage?}", page+""); 55 | return url; 56 | } 57 | 58 | 59 | public static String queryFor(String query) { 60 | return query; 61 | } 62 | 63 | public static Integer page(Integer page) { 64 | return page; 65 | } 66 | 67 | public Url with(String queryFor) { 68 | this.term = queryFor; 69 | return this; 70 | } 71 | 72 | public Request and(Integer page) { 73 | this.page = page; 74 | return client.at(toUri()); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/opensearch/conveter/DefaultTagsConveter.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.opensearch.conveter; 2 | 3 | import br.com.caelum.restfulie.opensearch.Tags; 4 | 5 | import com.thoughtworks.xstream.converters.Converter; 6 | import com.thoughtworks.xstream.converters.MarshallingContext; 7 | import com.thoughtworks.xstream.converters.UnmarshallingContext; 8 | import com.thoughtworks.xstream.io.HierarchicalStreamReader; 9 | import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 10 | 11 | public class DefaultTagsConveter implements Converter { 12 | 13 | @SuppressWarnings("rawtypes") 14 | public boolean canConvert(Class clazz) { 15 | return clazz.equals(Tags.class); 16 | } 17 | 18 | public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) { 19 | } 20 | 21 | public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 22 | 23 | Tags tags = new Tags(); 24 | String values = reader.getValue(); 25 | 26 | if(values.equals("")) { 27 | return tags; 28 | } 29 | 30 | for (String value : values.trim().split("\\s+")) { 31 | tags.add(value); 32 | } 33 | return tags; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/opensearch/conveter/DefaultUrlConverter.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.opensearch.conveter; 2 | 3 | import br.com.caelum.restfulie.RestClient; 4 | import br.com.caelum.restfulie.opensearch.Url; 5 | 6 | import com.thoughtworks.xstream.converters.Converter; 7 | import com.thoughtworks.xstream.converters.MarshallingContext; 8 | import com.thoughtworks.xstream.converters.UnmarshallingContext; 9 | import com.thoughtworks.xstream.io.HierarchicalStreamReader; 10 | import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 11 | 12 | public class DefaultUrlConverter implements Converter { 13 | 14 | private final RestClient client; 15 | 16 | public DefaultUrlConverter(RestClient client) { 17 | this.client = client; 18 | } 19 | 20 | public boolean canConvert(Class clazz) { 21 | return clazz.equals(Url.class); 22 | } 23 | 24 | public void marshal(Object arg0, HierarchicalStreamWriter arg1, MarshallingContext arg2) { 25 | 26 | } 27 | 28 | public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 29 | String type = reader.getAttribute("type"); 30 | String template = reader.getAttribute("template"); 31 | 32 | return new Url(type,template,client); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/relation/CachedEnhancer.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.relation; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author Caires & Thiago Miranda 8 | * This cached implementation will solve 99% of the concurrency problems, 9 | * but it still may have some cases of concurrency. 10 | */ 11 | 12 | @SuppressWarnings("rawtypes") 13 | public class CachedEnhancer implements Enhancer { 14 | 15 | private final Enhancer enhancer; 16 | private Map cache = new HashMap(); 17 | 18 | public CachedEnhancer(Enhancer enhancer) { 19 | this.enhancer = enhancer; 20 | } 21 | 22 | public Class enhanceResource(Class originalType) { 23 | if(cache.containsKey(originalType)) { 24 | return cache.get(originalType); 25 | } 26 | Class enhanced = enhancer.enhanceResource(originalType); 27 | cache.put(originalType, enhanced); 28 | return enhanced; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/relation/DefaultEnhancer.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.relation; 2 | 3 | import java.util.Random; 4 | import javassist.CannotCompileException; 5 | import javassist.ClassPool; 6 | import javassist.CtClass; 7 | import javassist.CtField; 8 | import javassist.CtNewMethod; 9 | import javassist.LoaderClassPath; 10 | import javassist.NotFoundException; 11 | import br.com.caelum.restfulie.Resource; 12 | 13 | @SuppressWarnings("rawtypes") 14 | public class DefaultEnhancer implements Enhancer { 15 | 16 | public Class enhanceResource(Class originalType) { 17 | ClassPool pool = ClassPool.getDefault(); 18 | if (pool.find(DefaultEnhancer.class.getName()) == null) { 19 | pool.appendClassPath(new LoaderClassPath(getClass().getClassLoader())); 20 | } 21 | try { 22 | // TODO extract this enhancement to an interface and test it appart 23 | CtClass newType = pool.makeClass(generateNewUniqueClassName(originalType)); 24 | newType.setSuperclass(pool.get(originalType.getName())); 25 | newType.addInterface(pool.get(Resource.class.getName())); 26 | enhanceLinks(newType); 27 | return newType.toClass(); 28 | } catch (NotFoundException e) { 29 | throw new IllegalStateException("Unable to extend type " + originalType.getName(), e); 30 | } catch (CannotCompileException e) { 31 | throw new IllegalStateException("Unable to extend type " + originalType.getName(), e); 32 | } 33 | } 34 | 35 | private String generateNewUniqueClassName(Class originalType) { 36 | return "br.com.caelum.restfulie." + originalType.getSimpleName() + "_" + System.currentTimeMillis() + new Random().nextLong(); 37 | } 38 | 39 | private void enhanceLinks(CtClass newType) throws CannotCompileException { 40 | CtField field = CtField.make("public java.util.List link = new java.util.ArrayList();", newType); 41 | newType.addField(field); 42 | newType.addMethod(CtNewMethod.make("public java.util.List getLinks() { return link; }", newType)); 43 | newType.addMethod(CtNewMethod.make("public java.util.List getLinks(String rel) { java.util.List links = new java.util.ArrayList(); for(int i=0;i Class enhanceResource(Class originalType); 6 | 7 | } -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/request/RequestChain.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.request; 2 | 3 | import java.net.URI; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | import br.com.caelum.restfulie.Response; 8 | import br.com.caelum.restfulie.feature.RequestFeature; 9 | import br.com.caelum.restfulie.http.Request; 10 | 11 | public class RequestChain { 12 | 13 | private final Iterator current; 14 | public RequestChain(List requests) { 15 | this.current = requests.iterator(); 16 | } 17 | 18 | public Response next(Request request, String verb, URI uri, Object payload) { 19 | if(current.hasNext()) { 20 | return current.next().process(this, request, verb, uri, payload); 21 | } 22 | return null; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/request/RequestDispatcher.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.request; 2 | 3 | import java.net.URI; 4 | 5 | import br.com.caelum.restfulie.Response; 6 | import br.com.caelum.restfulie.http.Request; 7 | 8 | /** 9 | * A dispatcher that will actually execute the request. 10 | * 11 | * @author Guilherme Silveira 12 | */ 13 | public interface RequestDispatcher { 14 | 15 | Response process(Request request, String verb, URI uri, Object payload); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/request/RequestStack.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.request; 2 | 3 | import java.net.URI; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import br.com.caelum.restfulie.Response; 8 | import br.com.caelum.restfulie.RestClient; 9 | import br.com.caelum.restfulie.feature.RequestFeature; 10 | import br.com.caelum.restfulie.feature.ResponseFeature; 11 | import br.com.caelum.restfulie.http.Request; 12 | 13 | public class RequestStack implements RequestFeature { 14 | 15 | private final List responses = new ArrayList(); 16 | private final List requests = new ArrayList(); 17 | private final RestClient client; 18 | 19 | public RequestStack(RestClient client) { 20 | this.client = client; 21 | } 22 | 23 | public void with(ResponseFeature feature) { 24 | this.responses.add(feature); 25 | } 26 | 27 | public Response process(Request request, String verb, URI uri, Object payload) { 28 | requests.add(this); 29 | return new RequestChain(requests).next(request, verb, uri, payload); 30 | } 31 | 32 | public void with(RequestFeature feature) { 33 | this.requests.add(feature); 34 | } 35 | 36 | public Response process(RequestChain chain, Request request, String verb, 37 | URI uri, Object payload) { 38 | Response response = client.getProvider().process(request, verb, uri, payload); 39 | return new ResponseChain(responses, client).next(response); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/request/ResponseChain.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.request; 2 | 3 | import java.util.Iterator; 4 | import java.util.List; 5 | 6 | import br.com.caelum.restfulie.Response; 7 | import br.com.caelum.restfulie.RestClient; 8 | import br.com.caelum.restfulie.feature.ResponseFeature; 9 | 10 | public class ResponseChain { 11 | 12 | private final Iterator current; 13 | private final RestClient client; 14 | 15 | public ResponseChain(List features, RestClient client) { 16 | this.client = client; 17 | this.current = features.iterator(); 18 | } 19 | 20 | public Response next(Response response) { 21 | if(current.hasNext()) { 22 | return current.next().process(this, response); 23 | } 24 | return response; 25 | } 26 | 27 | public RestClient getClient() { 28 | return client; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /client/src/main/java/br/com/caelum/restfulie/serializer/SerializationException.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource - guilherme.silveira@caelum.com.br 3 | * All rights reserved. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package br.com.caelum.restfulie.serializer; 19 | 20 | /** 21 | * Representation of problems during serialization 22 | * @author guilherme silveira 23 | * @since 3.0.2 24 | */ 25 | @SuppressWarnings("serial") 26 | public class SerializationException extends RuntimeException { 27 | 28 | public SerializationException(String string, Throwable e) { 29 | super(string,e); 30 | } 31 | 32 | public SerializationException(String msg) { 33 | super(msg); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/LinkTest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie; 2 | 3 | import static org.hamcrest.MatcherAssert.assertThat; 4 | import static org.hamcrest.Matchers.equalTo; 5 | import static org.hamcrest.Matchers.is; 6 | 7 | import org.junit.Test; 8 | 9 | import br.com.caelum.restfulie.http.DefaultRestClient; 10 | import br.com.caelum.restfulie.http.Request; 11 | 12 | public class LinkTest { 13 | 14 | @Test 15 | public void shouldUseTheMediaTypeProviedByTheUser() { 16 | DefaultRestClient restfulie = new DefaultRestClient(); 17 | Request request = restfulie.at("uri").as("application/xml"); 18 | 19 | assertThat(request.getHeaders().get("Content-type"),is(equalTo("application/xml"))); 20 | } 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/feature/FollowRedirectsTest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.feature; 2 | 3 | import static org.hamcrest.CoreMatchers.equalTo; 4 | import static org.hamcrest.CoreMatchers.is; 5 | import static org.junit.Assert.assertThat; 6 | import static org.mockito.Matchers.anyMap; 7 | import static org.mockito.Mockito.when; 8 | 9 | import java.net.URI; 10 | import java.net.URISyntaxException; 11 | import java.util.Arrays; 12 | 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | import org.mockito.Mock; 16 | import org.mockito.runners.MockitoJUnitRunner; 17 | 18 | import br.com.caelum.restfulie.Response; 19 | import br.com.caelum.restfulie.RestClient; 20 | import br.com.caelum.restfulie.http.Request; 21 | 22 | @RunWith(MockitoJUnitRunner.class) 23 | public class FollowRedirectsTest { 24 | 25 | @Mock 26 | private Response redirectResponse, response; 27 | @Mock 28 | private RestClient client; 29 | @Mock 30 | private Request request, oldRequest; 31 | 32 | @Test 33 | public void shouldFollowAbsoluteLocation() { 34 | String uri = "http://mylocation.com"; 35 | when(response.getCode()).thenReturn(302); 36 | when(response.getHeader("Location")).thenReturn(Arrays.asList(uri)); 37 | when(client.at(uri)).thenReturn(request); 38 | when(request.get()).thenReturn(redirectResponse); 39 | when(request.addHeaders(anyMap())).thenReturn(request); 40 | when(response.getRequest()).thenReturn(oldRequest); 41 | 42 | assertThat(new FollowRedirects(client).process(null, response), is(equalTo(redirectResponse))); 43 | 44 | } 45 | 46 | @Test 47 | public void shouldNotFollowOtherCodes() { 48 | when(response.getCode()).thenReturn(202); 49 | assertThat(new FollowRedirects(client).process(null, response), is(equalTo(response))); 50 | } 51 | 52 | @Test 53 | public void shouldFollowRelativeLocations() throws URISyntaxException { 54 | 55 | String uri = "/client"; 56 | when(response.getCode()).thenReturn(302); 57 | when(response.getHeader("Location")).thenReturn(Arrays.asList(uri)); 58 | when(response.getRequest()).thenReturn(oldRequest); 59 | when(oldRequest.getURI()).thenReturn(new URI("http://caelum.com.br/everything_else")); 60 | when(client.at(new URI("http://caelum.com.br/client"))).thenReturn(request); 61 | when(request.get()).thenReturn(redirectResponse); 62 | when(request.addHeaders(anyMap())).thenReturn(request); 63 | when(response.getRequest()).thenReturn(oldRequest); 64 | 65 | assertThat(new FollowRedirects(client).process(null, response), is(equalTo(redirectResponse))); 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/feature/RetryWhenUnavailableTest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.feature; 2 | 3 | 4 | import static br.com.caelum.restfulie.feature.Features.retryWhenUnavaiable; 5 | import static org.mockito.Mockito.times; 6 | import static org.mockito.Mockito.verify; 7 | import static org.mockito.Mockito.when; 8 | 9 | import java.net.URI; 10 | import java.net.URISyntaxException; 11 | 12 | import org.junit.Ignore; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | import org.mockito.Mock; 16 | import org.mockito.runners.MockitoJUnitRunner; 17 | 18 | import br.com.caelum.restfulie.Response; 19 | import br.com.caelum.restfulie.Restfulie; 20 | import br.com.caelum.restfulie.http.Request; 21 | import br.com.caelum.restfulie.request.RequestChain; 22 | 23 | @RunWith(MockitoJUnitRunner.class) 24 | public class RetryWhenUnavailableTest { 25 | 26 | @Mock 27 | private Request request; 28 | 29 | @Mock 30 | private RequestChain chain; 31 | 32 | @Mock 33 | private Response response; 34 | 35 | @Test 36 | public void shouldRetryWhenCode503() throws URISyntaxException { 37 | String verb = "get"; 38 | URI uri = new URI("http://someplace.com"); 39 | 40 | when(chain.next(request, verb, uri, null)).thenReturn(response); 41 | when(response.getCode()).thenReturn(503); 42 | 43 | new RetryWhenUnavailable().process(chain, request, verb, uri, null); 44 | 45 | verify(chain, times(2)).next(request, verb, uri, null); 46 | } 47 | 48 | @Test 49 | public void shouldNotRetryWhenUnkonwCode() throws URISyntaxException { 50 | String verb = "get"; 51 | URI uri = new URI("http://someplace.com"); 52 | 53 | when(chain.next(request, verb, uri, null)).thenReturn(response); 54 | when(response.getCode()).thenReturn(200); 55 | 56 | new RetryWhenUnavailable().process(chain, request, verb, uri, null); 57 | 58 | verify(chain, times(1)).next(request, verb, uri, null); 59 | } 60 | 61 | @Ignore 62 | public void dslText() { 63 | Restfulie.at("here").with(retryWhenUnavaiable()).get(); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/http/DefaultRestClientTest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | import org.jvnet.inflector.Noun; 7 | import org.jvnet.inflector.Pluralizer; 8 | 9 | import br.com.caelum.restfulie.RestClient; 10 | 11 | public class DefaultRestClientTest 12 | { 13 | 14 | private RestClient client; 15 | 16 | @Test 17 | public void shouldInflectUsingDefaultInflector() 18 | { 19 | client = new DefaultRestClient(); 20 | assertEquals( Noun.pluralOf("loaf", client.inflectionRules()), "loaves" ); 21 | } 22 | 23 | @Test 24 | public void shouldInflectUsingCustomInflector() 25 | { 26 | Pluralizer inflector = new Pluralizer() 27 | { 28 | public String pluralize(String word, int number) 29 | { 30 | return "custom works!"; 31 | } 32 | 33 | public String pluralize(String word) 34 | { 35 | return "custom works!"; 36 | } 37 | }; 38 | 39 | client = new DefaultRestClient().withInflector(inflector); 40 | assertEquals( Noun.pluralOf("loaf", client.inflectionRules()), "custom works!"); 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/http/MapHeadersTest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http; 2 | 3 | import static org.hamcrest.MatcherAssert.assertThat; 4 | import static org.hamcrest.Matchers.equalTo; 5 | import static org.hamcrest.Matchers.is; 6 | 7 | import java.util.Arrays; 8 | import java.util.HashMap; 9 | import java.util.List; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | 14 | import br.com.caelum.restfulie.http.javanet.MapHeaders; 15 | 16 | public class MapHeadersTest { 17 | 18 | private Headers headers; 19 | private HashMap> fields; 20 | 21 | @Before 22 | public void setUp() { 23 | this.fields = new HashMap>(); 24 | headers = new MapHeaders(fields); 25 | } 26 | 27 | @Test 28 | public void shouldReturnRaw() { 29 | fields.put("Content-Type", Arrays.asList("application/xml;charset=ISO-8859-1")); 30 | assertThat(headers.get("Content-Type").get(0), is(equalTo("application/xml;charset=ISO-8859-1"))); 31 | } 32 | 33 | @Test 34 | public void shouldReturnMain() { 35 | fields.put("Content-Type", Arrays.asList("application/xml")); 36 | assertThat(headers.getMain("Content-Type"), is(equalTo("application/xml"))); 37 | } 38 | 39 | @Test 40 | public void shouldReturnMainIgnoringCharset() { 41 | fields.put("Content-Type", Arrays.asList("application/xml;charset=ISO-8859-1")); 42 | assertThat(headers.getMain("Content-Type"), is(equalTo("application/xml"))); 43 | } 44 | 45 | @Test(expected=IllegalArgumentException.class) 46 | public void shouldNotExecutedWhenNoHeaders() { 47 | assertThat(headers.getMain("Content-Type"), is(equalTo("application/xml"))); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/http/apache/ApacheResponseTest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.apache; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.net.URI; 6 | import java.net.URISyntaxException; 7 | 8 | import org.apache.http.Header; 9 | import org.apache.http.HeaderElement; 10 | import org.apache.http.HttpResponse; 11 | import org.apache.http.ParseException; 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | import org.mockito.Mock; 16 | import org.mockito.Mockito; 17 | import org.mockito.runners.MockitoJUnitRunner; 18 | 19 | import static org.mockito.Mockito.*; 20 | 21 | import br.com.caelum.restfulie.Response; 22 | import br.com.caelum.restfulie.http.Request; 23 | 24 | @RunWith(MockitoJUnitRunner.class) 25 | public class ApacheResponseTest 26 | { 27 | 28 | @Mock 29 | private Response response; 30 | 31 | @Mock 32 | private Request request; 33 | 34 | @Mock 35 | private HttpResponse mockHttpResponse; 36 | 37 | @Before 38 | public void setUp() 39 | { 40 | mockHttpResponse = Mockito.mock(HttpResponse.class); 41 | response = new ApacheResponse(mockHttpResponse ,null, request); 42 | } 43 | 44 | @Test 45 | public void shouldGetResponseType() 46 | { 47 | when(mockHttpResponse.getHeaders("Content-Type")).thenReturn(new Header[]{new ContentTypeHeader("text/html")}); 48 | assertEquals("text/html", response.getType()); 49 | } 50 | 51 | @Test 52 | public void shouldReturnToOriginalResponseWhenNoneLocationIsDefined() throws URISyntaxException 53 | { 54 | URI origin = new URI("http://default.com"); 55 | when(request.getURI()).thenReturn(origin); 56 | assertEquals( origin, response.getLocation() ); 57 | } 58 | 59 | @Test 60 | public void shouldGetResponseLocation() throws URISyntaxException 61 | { 62 | when(mockHttpResponse.getHeaders("Location")).thenReturn(new Header[]{new Header() { 63 | 64 | public String getValue() { return "http://example.com"; } 65 | 66 | public String getName() { return "Location"; } 67 | 68 | public HeaderElement[] getElements() throws ParseException { return null; } 69 | } 70 | }); 71 | assertEquals(new URI("http://example.com"), response.getLocation()); 72 | } 73 | } -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/http/apache/ContentTypeHeader.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.http.apache; 2 | 3 | import org.apache.http.Header; 4 | import org.apache.http.HeaderElement; 5 | import org.apache.http.ParseException; 6 | 7 | class ContentTypeHeader implements Header { 8 | 9 | private final String value; 10 | 11 | public ContentTypeHeader(String value) { 12 | this.value = value; 13 | } 14 | 15 | public String getValue() { return value; } 16 | 17 | public String getName() { return "Content-Type"; } 18 | 19 | public HeaderElement[] getElements() throws ParseException { return null; } 20 | } -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/integration/client/Item.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.integration.client; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | 5 | @XStreamAlias("item") 6 | public class Item { 7 | private Integer id; 8 | private String name; 9 | private Double price; 10 | private Integer quantity; 11 | 12 | public Integer getId() { 13 | return id; 14 | } 15 | 16 | public void setId(Integer id) { 17 | this.id = id; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public Double getPrice() { 29 | return price; 30 | } 31 | 32 | public void setPrice(Double price) { 33 | this.price = price; 34 | } 35 | 36 | public Integer getQuantity() { 37 | return quantity; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/integration/client/Order.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.integration.client; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.thoughtworks.xstream.annotations.XStreamAlias; 7 | 8 | @XStreamAlias("order") 9 | public class Order { 10 | private String address; 11 | private Double price; 12 | private String state; 13 | private Integer product; 14 | private Integer quantity; 15 | 16 | private List payments = new ArrayList(); 17 | private List items = new ArrayList(); 18 | 19 | public String getAddress() { 20 | return address; 21 | } 22 | 23 | public void setAddress(String address) { 24 | this.address = address; 25 | } 26 | 27 | public Double getPrice() { 28 | return price; 29 | } 30 | 31 | public void setPrice(Double price) { 32 | this.price = price; 33 | } 34 | 35 | public String getState() { 36 | return state; 37 | } 38 | 39 | public void setState(String state) { 40 | this.state = state; 41 | } 42 | 43 | public List getPayments() { 44 | return payments; 45 | } 46 | 47 | public void setPayments(List payments) { 48 | this.payments = payments; 49 | } 50 | 51 | public List getItems() { 52 | return items; 53 | } 54 | 55 | public void setItems(List items) { 56 | this.items = items; 57 | } 58 | 59 | public Integer getProduct() { 60 | return product; 61 | } 62 | 63 | public void setProduct(Integer product) { 64 | this.product = product; 65 | } 66 | 67 | public Integer getQuantity() { 68 | return quantity; 69 | } 70 | 71 | public void setQuantity(Integer quantity) { 72 | this.quantity = quantity; 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/integration/client/Payment.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.integration.client; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | 5 | @XStreamAlias("payment") 6 | public class Payment { 7 | 8 | @XStreamAlias("card_holder") 9 | private final String holder; 10 | @XStreamAlias("card_number") 11 | private final int number; 12 | private final Double value; 13 | private String state; 14 | 15 | public Payment(String holder, int number, Double value) { 16 | this.holder = holder; 17 | this.number = number; 18 | this.value = value; 19 | } 20 | 21 | public String getHolder() { 22 | return holder; 23 | } 24 | 25 | public int getNumber() { 26 | return number; 27 | } 28 | 29 | public Double getValue() { 30 | return value; 31 | } 32 | 33 | public String getState() { 34 | return state; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/integration/client/Product.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.integration.client; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | 5 | @XStreamAlias("product") 6 | public class Product { 7 | private Integer id; 8 | private String name; 9 | private Double price; 10 | 11 | public Integer getId() { 12 | return id; 13 | } 14 | 15 | public void setId(Integer id) { 16 | this.id = id; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public Double getPrice() { 28 | return price; 29 | } 30 | 31 | public void setPrice(Double price) { 32 | this.price = price; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/maze/Maze.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.maze; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | import java.util.Stack; 6 | 7 | import br.com.caelum.restfulie.Link; 8 | import br.com.caelum.restfulie.Restfulie; 9 | import br.com.caelum.restfulie.http.Headers; 10 | 11 | /** 12 | * 13 | * @author jose donizetti 14 | */ 15 | public class Maze { 16 | 17 | public static void main(String[] args) { 18 | //stealing guilherme silveira algorithm 19 | Headers headers = Restfulie.at("http://amundsen.com/examples/mazes/2d/five-by-five/").accept("application/xml").get().getHeaders(); 20 | 21 | Set visited = new HashSet(); 22 | Stack path = new Stack(); 23 | Link link = null; 24 | int steps = 0; 25 | 26 | while(!(headers.getLink("exit") != null)) { 27 | 28 | link = find(visited,"start north south east west",headers); 29 | 30 | if(link == null) { 31 | path.pop(); 32 | link = path.pop(); 33 | } 34 | 35 | path.add(link); 36 | visited.add(link.getHref()); 37 | System.out.println(link); 38 | headers = link.follow().get().getHeaders(); 39 | 40 | steps++; 41 | } 42 | 43 | System.out.println("steps = " + steps); 44 | 45 | } 46 | 47 | private static Link find(Set visited, String string, Headers headers) { 48 | String[] directions = string.split("\\s++"); 49 | 50 | for(String direction : directions) { 51 | Link link = headers.getLink(direction); 52 | if((link != null) && (!visited.contains(link.getHref()))){ 53 | return link; 54 | } 55 | } 56 | 57 | return null; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/mediatype/FormEncodedTest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.mediatype; 2 | 3 | import static org.hamcrest.CoreMatchers.is; 4 | import static org.junit.Assert.*; 5 | 6 | import java.io.IOException; 7 | import java.io.StringWriter; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | import org.hamcrest.CoreMatchers; 12 | import org.hamcrest.Matchers; 13 | import org.junit.Test; 14 | 15 | import br.com.caelum.restfulie.http.DefaultRestClient; 16 | 17 | public class FormEncodedTest { 18 | 19 | @Test 20 | public void shouldConcatenateParams() throws IOException { 21 | FormEncoded encoded = new FormEncoded(); 22 | StringWriter writer = new StringWriter(); 23 | Map params = new HashMap(); 24 | params.put("name", "Guilherme"); 25 | params.put("age", "29"); 26 | encoded.marshal(params, writer, new DefaultRestClient()); 27 | 28 | assertThat(writer.toString(), Matchers.anyOf(is(CoreMatchers.equalTo("name=Guilherme&age=29")), 29 | is(CoreMatchers.equalTo("age=29&name=Guilherme")))); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /client/src/test/java/br/com/caelum/restfulie/opensearch/converter/TagsConverterTest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restfulie.opensearch.converter; 2 | 3 | import static org.hamcrest.MatcherAssert.assertThat; 4 | import static org.hamcrest.Matchers.equalTo; 5 | import static org.hamcrest.Matchers.is; 6 | 7 | import java.io.StringReader; 8 | 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | 12 | import br.com.caelum.restfulie.opensearch.SearchDescription; 13 | import br.com.caelum.restfulie.opensearch.conveter.DefaultTagsConveter; 14 | 15 | import com.thoughtworks.xstream.XStream; 16 | 17 | public class TagsConverterTest { 18 | 19 | private XStream stream = new XStream(); 20 | 21 | @Before 22 | public void setUp() { 23 | stream.registerConverter(new DefaultTagsConveter()); 24 | stream.processAnnotations(SearchDescription.class); 25 | } 26 | 27 | @Test 28 | public void shouldConvertAnEmptyElement() { 29 | //Given 30 | String xml = "" + 31 | "" + 32 | ""; 33 | 34 | //When 35 | SearchDescription desc = (SearchDescription) stream.fromXML(new StringReader(xml)); 36 | 37 | //Then 38 | assertThat(desc.getTags().size(), is(equalTo(0))); 39 | } 40 | 41 | @Test 42 | public void shouldConvertAnElementWithTwoTagsSeparetedByOneSpace() { 43 | //Given 44 | String xml = "" + 45 | "restfulie rest" + 46 | ""; 47 | 48 | //When 49 | SearchDescription desc = (SearchDescription) stream.fromXML(new StringReader(xml)); 50 | 51 | //Then 52 | assertThat(desc.getTags().size(), is(equalTo(2))); 53 | assertThat(desc.getTags().get(0), is(equalTo("restfulie"))); 54 | assertThat(desc.getTags().get(1), is(equalTo("rest"))); 55 | } 56 | 57 | @Test 58 | public void shouldConvertAnElementWithThreeTagsSeparetedByMoreThanOneSpace() { 59 | //Given 60 | String xml = "" + 61 | "restfulie rest http" + 62 | ""; 63 | 64 | //When 65 | SearchDescription desc = (SearchDescription) stream.fromXML(new StringReader(xml)); 66 | 67 | //Then 68 | assertThat(desc.getTags().size(), is(equalTo(3))); 69 | assertThat(desc.getTags().get(0), is(equalTo("restfulie"))); 70 | assertThat(desc.getTags().get(1), is(equalTo("rest"))); 71 | assertThat(desc.getTags().get(2), is(equalTo("http"))); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /example/client/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /example/client/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | restbucks client 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /example/client/examples/order.xml: -------------------------------------------------------------------------------- 1 | 2 | takeAway 3 | 4 | 5 | latte 6 | 2 7 | skim 8 | medium 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /example/client/examples/payment.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 3 | Guilherme Silveira 4 | 123456789012 5 | 12 6 | 12 7 | 8 | -------------------------------------------------------------------------------- /example/client/lib/javassist.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/client/lib/javassist.jar -------------------------------------------------------------------------------- /example/client/lib/restfulie-java-client-1.0.0-beta-2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/client/lib/restfulie-java-client-1.0.0-beta-2.jar -------------------------------------------------------------------------------- /example/client/lib/stax-1.2.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/client/lib/stax-1.2.0.jar -------------------------------------------------------------------------------- /example/client/lib/stax-api-1.0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/client/lib/stax-api-1.0.1.jar -------------------------------------------------------------------------------- /example/client/lib/xpp3_min-1.1.4c.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/client/lib/xpp3_min-1.1.4c.jar -------------------------------------------------------------------------------- /example/client/lib/xstream-1.3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/client/lib/xstream-1.3.1.jar -------------------------------------------------------------------------------- /example/client/src/main/java/br/com/caelum/restbucks/model/Item.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restbucks.model; 2 | 3 | import java.math.BigDecimal; 4 | 5 | import com.thoughtworks.xstream.annotations.XStreamAlias; 6 | import com.thoughtworks.xstream.annotations.XStreamImplicitCollection; 7 | 8 | @XStreamAlias("item") 9 | public class Item { 10 | enum Coffee { 11 | latte(2.0), cappuccino(2.0), espresso(1.5); 12 | private final BigDecimal price; 13 | 14 | Coffee(double price) { 15 | this.price = new BigDecimal(price); 16 | } 17 | } 18 | 19 | enum Milk { 20 | skim, semi, whole 21 | }; 22 | 23 | enum Size { 24 | small, medium, large 25 | }; 26 | 27 | private String id; 28 | private Coffee drink; 29 | private Milk milk; 30 | private Size size; 31 | @XStreamAlias("created-at") 32 | private String createdAt; 33 | 34 | @XStreamAlias("updated-at") 35 | private String updatedAt; 36 | 37 | 38 | public Item(Coffee name, Milk milk, Size size) { 39 | this.drink = name; 40 | this.milk = milk; 41 | this.size = size; 42 | } 43 | 44 | public Coffee getDrink() { 45 | return drink; 46 | } 47 | 48 | public void setDrink(Coffee name) { 49 | this.drink = name; 50 | } 51 | 52 | public Milk getMilk() { 53 | return milk; 54 | } 55 | 56 | public void setMilk(Milk milk) { 57 | this.milk = milk; 58 | } 59 | 60 | public Size getSize() { 61 | return size; 62 | } 63 | 64 | public void setSize(Size size) { 65 | this.size = size; 66 | } 67 | 68 | public BigDecimal getPrice() { 69 | return drink.price; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /example/client/src/main/java/br/com/caelum/restbucks/model/Order.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restbucks.model; 2 | 3 | import static br.com.caelum.restfulie.Restfulie.resource; 4 | 5 | import java.math.BigDecimal; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import com.thoughtworks.xstream.annotations.XStreamAlias; 10 | import com.thoughtworks.xstream.annotations.XStreamOmitField; 11 | 12 | @XStreamAlias("order") 13 | public class Order { 14 | 15 | private String id; 16 | private Location location; 17 | 18 | private List items; 19 | 20 | private String status; 21 | private Payment payment; 22 | 23 | @XStreamAlias("created-at") 24 | private String createdAt; 25 | 26 | @XStreamAlias("updated-at") 27 | private String updatedAt; 28 | 29 | @XStreamOmitField 30 | private double cost; 31 | 32 | public enum Location { 33 | takeAway, drinkIn 34 | } 35 | 36 | public Order(String status, List items, Location location) { 37 | this.status = status; 38 | this.items = items; 39 | this.location = location; 40 | } 41 | 42 | public Order() { 43 | this.items = new ArrayList(); 44 | } 45 | 46 | public void setLocation(Location location) { 47 | this.location = location; 48 | } 49 | 50 | public String getId() { 51 | return id; 52 | } 53 | 54 | public void setId(String id) { 55 | this.id = id; 56 | } 57 | 58 | public String getStatus() { 59 | return status; 60 | } 61 | 62 | public void setStatus(String status) { 63 | this.status = status; 64 | } 65 | 66 | public Payment getPayment() { 67 | return payment; 68 | } 69 | 70 | public void add(Item item) { 71 | this.items.add(item); 72 | } 73 | 74 | public String getSelfUri() { 75 | return resource(this).getRelation("self").getHref(); 76 | } 77 | 78 | public Payment pay(Payment payment) { 79 | return resource(this).getRelation("payment").accessAndRetrieve(payment); 80 | } 81 | 82 | public void cancel() { 83 | resource(this).getRelation("cancel").access(); 84 | } 85 | 86 | public double getCost() { 87 | return cost; 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /example/client/src/main/java/br/com/caelum/restbucks/model/Ordering.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restbucks.model; 2 | 3 | import br.com.caelum.restbucks.model.Item.Coffee; 4 | import br.com.caelum.restbucks.model.Item.Milk; 5 | import br.com.caelum.restbucks.model.Item.Size; 6 | import br.com.caelum.restbucks.model.Order.Location; 7 | 8 | public class Ordering { 9 | 10 | private final Order order = new Order(); 11 | 12 | public static Ordering order() { 13 | return new Ordering(); 14 | } 15 | 16 | public Ordering withRandomItems() { 17 | int quantity = random(2, 5); 18 | for (int i = 0; i < quantity; i++) { 19 | Item item = new Item(random(Coffee.class), random(Milk.class), random(Size.class)); 20 | order.add(item); 21 | } 22 | return this; 23 | } 24 | 25 | private T random(Class type) { 26 | return type.getEnumConstants()[random(0,type.getEnumConstants().length)]; 27 | } 28 | 29 | private int random(int from, int to) { 30 | return (int) (Math.random() * (to-from) +from); 31 | } 32 | 33 | public Order build() { 34 | order.setLocation(random(Location.class)); 35 | order.setStatus("unpaid"); 36 | return this.order; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /example/client/src/main/java/br/com/caelum/restbucks/model/Payment.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restbucks.model; 2 | 3 | import java.math.BigDecimal; 4 | 5 | import com.thoughtworks.xstream.annotations.XStreamAlias; 6 | 7 | @XStreamAlias("payment") 8 | public class Payment { 9 | 10 | private String id; 11 | 12 | @XStreamAlias("created-at") 13 | private String createdAt; 14 | 15 | @XStreamAlias("updated-at") 16 | private String updatedAt; 17 | 18 | @XStreamAlias("card-number") 19 | private String cardNumber; 20 | @XStreamAlias("cardholder-name") 21 | private String cardholderName; 22 | @XStreamAlias("expiry-month") 23 | private int expiryMonth; 24 | @XStreamAlias("expiry-year") 25 | private int expiryYear; 26 | private double amount; 27 | 28 | public Payment(String cardNumber, String cardholderName, int expiryMonth, 29 | int expiryYear, double amount) { 30 | super(); 31 | this.cardNumber = cardNumber; 32 | this.cardholderName = cardholderName; 33 | this.expiryMonth = expiryMonth; 34 | this.expiryYear = expiryYear; 35 | this.amount = amount; 36 | } 37 | 38 | public String getCreatedAt() { 39 | return this.createdAt; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /example/client/src/main/java/br/com/caelum/restbucks/model/Receipt.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restbucks.model; 2 | 3 | import static br.com.caelum.restfulie.Restfulie.resource; 4 | 5 | import java.util.Calendar; 6 | 7 | import br.com.caelum.restfulie.http.HttpMethod; 8 | 9 | import com.thoughtworks.xstream.annotations.XStreamAlias; 10 | 11 | @XStreamAlias("receipt") 12 | public class Receipt { 13 | 14 | private Calendar paymentTime; 15 | 16 | @XStreamAlias("created-at") 17 | private String createdAt; 18 | 19 | @XStreamAlias("updated-at") 20 | private String updatedAt; 21 | 22 | public Calendar getPaymentTime() { 23 | return paymentTime; 24 | } 25 | 26 | public Order getOrder() { 27 | return resource(this).getRelation("order").method(HttpMethod.GET).accessAndRetrieve(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /example/client/src/test/java/br/com/caelum/restbucks/MappingConfig.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.restbucks; 2 | 3 | import br.com.caelum.restbucks.model.Item; 4 | import br.com.caelum.restbucks.model.Order; 5 | import br.com.caelum.restbucks.model.Payment; 6 | import br.com.caelum.restbucks.model.Receipt; 7 | import br.com.caelum.restfulie.Resources; 8 | import br.com.caelum.restfulie.Restfulie; 9 | 10 | public class MappingConfig { 11 | 12 | private Resources server = Restfulie.resources(); 13 | 14 | public MappingConfig() { 15 | server.configure(Order.class).include("items"); 16 | server.configure(Payment.class); 17 | server.configure(Receipt.class); 18 | server.configure(Item.class); 19 | } 20 | 21 | public Resources getServer() { 22 | return server; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/.gitignore: -------------------------------------------------------------------------------- 1 | bin -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | rest_from_scratch_part_1_client 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/client/README -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/lib/javassist.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/client/lib/javassist.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/lib/jettison-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/client/lib/jettison-1.2.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/lib/junit-4.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/client/lib/junit-4.5.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/lib/stax-1.2.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/client/lib/stax-1.2.0.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/lib/stax-api-1.0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/client/lib/stax-api-1.0.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/lib/xpp3_min-1.1.4c.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/client/lib/xpp3_min-1.1.4c.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/lib/xstream-1.3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/client/lib/xstream-1.3.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/src/main/java/br/com/caelum/client/ClientTests.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.client; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | import static org.junit.Assert.assertNotSame; 6 | 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import br.com.caelum.example.model.Item; 11 | import br.com.caelum.restfulie.Response; 12 | import br.com.caelum.restfulie.RestClient; 13 | import br.com.caelum.restfulie.Restfulie; 14 | import br.com.caelum.restfulie.mediatype.XmlMediaType; 15 | 16 | public class ClientTests { 17 | 18 | private RestClient restfulie; 19 | 20 | @Before 21 | public void setUp() throws Exception { 22 | restfulie = Restfulie.custom(); 23 | restfulie.getMediaTypes().register(new XmlMediaType().withTypes(Item.class)); 24 | } 25 | 26 | @Test 27 | public void shouldBeAbleToGetAnItem() throws Exception { 28 | 29 | Response response = restfulie.at("http://localhost:8080/restfulie/items/2").accept("application/xml").get(); 30 | Item item = response.getResource(); 31 | 32 | assertNotNull(item); 33 | 34 | assertNotNull(item.getName()); 35 | 36 | System.out.println(item.getName()); 37 | 38 | } 39 | 40 | @Test 41 | public void shouldBeAbleToPostAnItem() throws Exception { 42 | Item item = new Item("pipa", 299.0); 43 | Response response = restfulie.at("http://localhost:8080/restfulie/items") 44 | .accept("application/xml") 45 | .as("application/xml") 46 | .post(item); 47 | 48 | Item savedItem = response.getResource(); 49 | assertNotSame(item, savedItem); 50 | 51 | assertEquals("pipa", savedItem.getName()); 52 | assertEquals(Double.valueOf(299.0), savedItem.getPrice()); 53 | assertNotNull(savedItem.getId()); 54 | System.out.println(savedItem.getId()); 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/client/src/main/java/br/com/caelum/example/model/Item.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.model; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | 5 | @XStreamAlias("item") 6 | public class Item { 7 | private Integer id; 8 | private String name; 9 | private Double price; 10 | 11 | public Item(String name, Double price) { 12 | this.name = name; 13 | this.price = price; 14 | } 15 | 16 | public Integer getId() { 17 | return id; 18 | } 19 | 20 | public void setId(Integer id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String nome) { 29 | this.name = nome; 30 | } 31 | 32 | public Double getPrice() { 33 | return price; 34 | } 35 | 36 | public void setPrice(Double preco) { 37 | this.price = preco; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/.gitignore: -------------------------------------------------------------------------------- 1 | build -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | rest_from_scratch_part_1_server 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.wst.jsdt.core.javascriptValidator 10 | 11 | 12 | 13 | 14 | org.eclipse.jdt.core.javabuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.wst.common.project.facet.core.builder 20 | 21 | 22 | 23 | 24 | org.eclipse.wst.validation.validationbuilder 25 | 26 | 27 | 28 | 29 | 30 | org.eclipse.jem.workbench.JavaEMFNature 31 | org.eclipse.wst.common.modulecore.ModuleCoreNature 32 | org.eclipse.wst.common.project.facet.core.nature 33 | org.eclipse.jdt.core.javanature 34 | org.eclipse.wst.jsdt.core.jsNature 35 | 36 | 37 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/.settings/.jsdtscope: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Thu May 20 11:39:11 BRT 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 4 | org.eclipse.jdt.core.compiler.compliance=1.6 5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 6 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 7 | org.eclipse.jdt.core.compiler.source=1.6 8 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/.settings/org.eclipse.wst.common.component: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/.settings/org.eclipse.wst.common.project.facet.core.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/.settings/org.eclipse.wst.jsdt.ui.superType.container: -------------------------------------------------------------------------------- 1 | org.eclipse.wst.jsdt.launching.baseBrowserLibrary -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/.settings/org.eclipse.wst.jsdt.ui.superType.name: -------------------------------------------------------------------------------- 1 | Window -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/README -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/jsp/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/jsp/.DS_Store -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/.jars: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/.jars -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/aopalliance.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/aopalliance.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/aspectjrt.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/aspectjrt.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/cglib-nodep-2.1_3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/cglib-nodep-2.1_3.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/commons-fileupload-1.2.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/commons-fileupload-1.2.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/commons-logging.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/commons-logging.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/google-collect-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/google-collect-1.0.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/javassist-3.9.0.GA.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/javassist-3.9.0.GA.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/jettison-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/jettison-1.2.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/jstl-api-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/jstl-api-1.2.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/jstl-impl-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/jstl-impl-1.2.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/log4j-1.2.15.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/log4j-1.2.15.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/mirror-1.5.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/mirror-1.5.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/objenesis-1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/objenesis-1.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/ognl-2.7.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/ognl-2.7.3.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.aop-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.aop-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.asm-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.asm-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.aspects-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.aspects-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.beans-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.beans-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.context-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.context-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.core-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.core-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.expression-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.expression-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.web-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/org.springframework.web-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/paranamer-2.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/paranamer-2.2.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/slf4j-api-1.5.8.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/slf4j-api-1.5.8.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/slf4j-log4j12-1.5.8.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/slf4j-log4j12-1.5.8.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/vraptor-3.1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/vraptor-3.1.3.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/vraptor-3.2.0-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/vraptor-3.2.0-SNAPSHOT.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/xstream-1.3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_1/server/WebContent/WEB-INF/lib/xstream-1.3.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | vraptor 5 | br.com.caelum.vraptor.VRaptor 6 | 7 | 8 | vraptor 9 | /* 10 | FORWARD 11 | REQUEST 12 | 13 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/WebContent/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 2 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 | 4 | 5 | 6 | 7 | 8 | VRaptor Blank Project 9 | 10 | 11 | It works!! ${variable} 12 | 13 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/src/main/java/br/com/caelum/example/controller/ItemsController.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.controller; 2 | 3 | import static br.com.caelum.vraptor.view.Results.representation; 4 | import static br.com.caelum.vraptor.view.Results.status; 5 | 6 | import java.util.List; 7 | 8 | import br.com.caelum.example.infra.Database; 9 | import br.com.caelum.example.model.Item; 10 | import br.com.caelum.vraptor.Consumes; 11 | import br.com.caelum.vraptor.Get; 12 | import br.com.caelum.vraptor.Path; 13 | import br.com.caelum.vraptor.Post; 14 | import br.com.caelum.vraptor.Resource; 15 | import br.com.caelum.vraptor.Result; 16 | 17 | @Resource 18 | public class ItemsController { 19 | 20 | private final Database database; 21 | private final Result result; 22 | 23 | public ItemsController(Database database, Result result) { 24 | this.database = database; 25 | this.result = result; 26 | } 27 | 28 | @Get 29 | @Path("/items") 30 | public void list() { 31 | List list = database.lista(); 32 | result.use(representation()).from(list, "items").serialize(); 33 | } 34 | 35 | @Get 36 | @Path("/items/{id}") 37 | public void show(int id) { 38 | Item item = database.get(id); 39 | result.use(representation()).from(item).serialize(); 40 | } 41 | 42 | @Post 43 | @Consumes 44 | @Path("/items") 45 | public void create(Item item) { 46 | database.adiciona(item); 47 | result.use(status()).created("/items/" + item.getId()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/src/main/java/br/com/caelum/example/infra/Database.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.infra; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import br.com.caelum.example.model.Item; 7 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 8 | import br.com.caelum.vraptor.ioc.Component; 9 | 10 | @Component 11 | @ApplicationScoped 12 | public class Database { 13 | private int contador = 0; 14 | private final List items; 15 | 16 | public Database() { 17 | this.items = new ArrayList(); 18 | this.adiciona(new Item("Chave", 20.0)); 19 | this.adiciona(new Item("Lousa", 35.0)); 20 | } 21 | 22 | public void adiciona(Item item) { 23 | item.setId(++contador); 24 | this.items.add(item); 25 | } 26 | 27 | public Item get(int id) { 28 | if(id > this.items.size()) { 29 | return null; 30 | } 31 | return this.items.get(id - 1); 32 | } 33 | 34 | public List lista() { 35 | return items; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/src/main/java/br/com/caelum/example/model/Item.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.model; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | 5 | @XStreamAlias("item") 6 | public class Item { 7 | private Integer id; 8 | private String name; 9 | private Double price; 10 | 11 | public Item(String nome, Double preco) { 12 | this.name = nome; 13 | this.price = preco; 14 | } 15 | 16 | public Integer getId() { 17 | return id; 18 | } 19 | 20 | public void setId(Integer id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String nome) { 29 | this.name = nome; 30 | } 31 | 32 | public Double getPrice() { 33 | return price; 34 | } 35 | 36 | public void setPrice(Double preco) { 37 | this.price = preco; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/src/main/java/br/com/caelum/example/vraptor/component/CustomJsonDeserializer.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.vraptor.component; 2 | 3 | import br.com.caelum.example.model.Item; 4 | import br.com.caelum.vraptor.deserialization.JsonDeserializer; 5 | import br.com.caelum.vraptor.http.ParameterNameProvider; 6 | import br.com.caelum.vraptor.interceptor.TypeNameExtractor; 7 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 8 | import br.com.caelum.vraptor.ioc.Component; 9 | 10 | import com.thoughtworks.xstream.XStream; 11 | 12 | @ApplicationScoped 13 | @Component 14 | public class CustomJsonDeserializer extends JsonDeserializer { 15 | 16 | public CustomJsonDeserializer(ParameterNameProvider provider, 17 | TypeNameExtractor extractor) { 18 | super(provider, extractor); 19 | } 20 | 21 | /** 22 | * Extension point to configure your xstream instance. 23 | * @return the configured xstream instance 24 | */ 25 | protected XStream getXStream() { 26 | XStream xstream = super.getXStream(); 27 | xstream.processAnnotations(Item.class); 28 | return xstream; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/src/main/java/br/com/caelum/example/vraptor/component/CustomXStreamXMLDeserializer.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.vraptor.component; 2 | 3 | import java.io.InputStream; 4 | import java.lang.reflect.Method; 5 | 6 | import br.com.caelum.vraptor.deserialization.XMLDeserializer; 7 | import br.com.caelum.vraptor.http.ParameterNameProvider; 8 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 9 | import br.com.caelum.vraptor.ioc.Component; 10 | import br.com.caelum.vraptor.resource.ResourceMethod; 11 | 12 | import com.thoughtworks.xstream.XStream; 13 | import com.thoughtworks.xstream.io.xml.DomDriver; 14 | @ApplicationScoped 15 | @Component 16 | public class CustomXStreamXMLDeserializer implements XMLDeserializer { 17 | 18 | private final ParameterNameProvider provider; 19 | 20 | public CustomXStreamXMLDeserializer(ParameterNameProvider provider) { 21 | this.provider = provider; 22 | } 23 | 24 | public Object[] deserialize(InputStream inputStream, ResourceMethod method) { 25 | Method javaMethod = method.getMethod(); 26 | Class[] types = javaMethod.getParameterTypes(); 27 | if (types.length == 0) { 28 | throw new IllegalArgumentException("Methods that consumes xml must receive just one argument: the xml root element"); 29 | } 30 | XStream xStream = getConfiguredXStream(javaMethod, types); 31 | 32 | Object[] params = new Object[types.length]; 33 | 34 | chooseParam(types, params, xStream.fromXML(inputStream)); 35 | 36 | return params; 37 | } 38 | 39 | /** 40 | * Returns an xstream instance already configured. 41 | */ 42 | public XStream getConfiguredXStream(Method javaMethod, Class[] types) { 43 | XStream xStream = getXStream(); 44 | aliasParams(javaMethod, types, xStream); 45 | return xStream; 46 | } 47 | 48 | private void chooseParam(Class[] types, Object[] params, Object deserialized) { 49 | for (int i = 0; i < types.length; i++) { 50 | if (types[i].isInstance(deserialized)) { 51 | params[i] = deserialized; 52 | } 53 | } 54 | } 55 | 56 | private void aliasParams(Method method, Class[] types, XStream deserializer) { 57 | String[] names = provider.parameterNamesFor(method); 58 | for (int i = 0; i < names.length; i++) { 59 | deserializer.alias(names[i], types[i]); 60 | } 61 | } 62 | 63 | /** 64 | * Extension point to configure your xstream instance. 65 | * @return the configured xstream instance 66 | */ 67 | protected XStream getXStream() { 68 | return new XStream(new DomDriver()); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/src/main/java/br/com/caelum/example/vraptor/component/JSONSerializationCustomized.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.vraptor.component; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import javax.servlet.http.HttpServletResponse; 7 | 8 | import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor; 9 | import br.com.caelum.vraptor.interceptor.TypeNameExtractor; 10 | import br.com.caelum.vraptor.ioc.Component; 11 | import br.com.caelum.vraptor.serialization.ProxyInitializer; 12 | import br.com.caelum.vraptor.serialization.xstream.XStreamJSONSerialization; 13 | 14 | import com.thoughtworks.xstream.XStream; 15 | import com.thoughtworks.xstream.converters.MarshallingContext; 16 | import com.thoughtworks.xstream.converters.collections.CollectionConverter; 17 | import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 18 | import com.thoughtworks.xstream.mapper.Mapper; 19 | 20 | @Component 21 | public class JSONSerializationCustomized extends XStreamJSONSerialization { 22 | 23 | public JSONSerializationCustomized(HttpServletResponse response, 24 | TypeNameExtractor extractor, ProxyInitializer initializer) { 25 | super(response, extractor, initializer); 26 | } 27 | 28 | @Override 29 | protected XStream getXStream() { 30 | XStream xstream = super.getXStream(); 31 | MyCollectionConverter converter = new MyCollectionConverter(xstream 32 | .getMapper()); 33 | xstream.registerConverter(converter); 34 | return xstream; 35 | } 36 | 37 | public static void main(String[] args) { 38 | JSONSerializationCustomized ser = new JSONSerializationCustomized(null, new DefaultTypeNameExtractor(), null); 39 | XStream xstream = ser.getXStream(); 40 | List l = new ArrayList(); 41 | l.add(new Item("a", "b")); 42 | l.add(new Item("d", "c")); 43 | System.out.println(xstream.toXML(l)); 44 | List l2 = (List) xstream.fromXML(xstream.toXML(l)); 45 | System.out.println(l2); 46 | } 47 | 48 | } 49 | 50 | class Item { 51 | public String name; 52 | public String age; 53 | public Item(String name, String age) { 54 | super(); 55 | this.name = name; 56 | this.age = age; 57 | } 58 | 59 | } 60 | 61 | class MyCollectionConverter extends CollectionConverter { 62 | public MyCollectionConverter(Mapper mapper) { 63 | super(mapper); 64 | } 65 | 66 | protected void writeItem(Object item, MarshallingContext context, 67 | HierarchicalStreamWriter writer) { 68 | // PUBLISHED API METHOD! If changing signature, ensure backwards 69 | // compatability. 70 | if (item == null) { 71 | super.writeItem(item, context, writer); 72 | } else { 73 | String name = mapper().serializedClass(item.getClass()); 74 | writer.startNode(name); 75 | super.writeItem(item, context, writer); 76 | writer.endNode(); 77 | } 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /example/rest_from_scratch/part_1/server/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/.gitignore: -------------------------------------------------------------------------------- 1 | bin -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/client/README -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/lib/javassist.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/client/lib/javassist.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/lib/junit-4.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/client/lib/junit-4.5.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/lib/stax-1.2.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/client/lib/stax-1.2.0.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/lib/stax-api-1.0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/client/lib/stax-api-1.0.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/lib/xpp3_min-1.1.4c.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/client/lib/xpp3_min-1.1.4c.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/lib/xstream-1.3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/client/lib/xstream-1.3.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/restfulie-java-client-1.0.0-beta-2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/client/restfulie-java-client-1.0.0-beta-2.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/src/main/java/br/com/caelum/example/model/Basket.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.model; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.thoughtworks.xstream.annotations.XStreamAlias; 7 | 8 | @XStreamAlias("basket") 9 | public class Basket { 10 | 11 | private Long id; 12 | 13 | private List items; 14 | 15 | private Double cost; 16 | 17 | public Basket(List items) { 18 | this.items = new ArrayList(items); 19 | } 20 | 21 | public List getItems() { 22 | return items; 23 | } 24 | 25 | public void setItems(List items) { 26 | this.items = items; 27 | } 28 | 29 | public Double getCost() { 30 | return cost; 31 | } 32 | 33 | public void setCost(Double cost) { 34 | this.cost = cost; 35 | } 36 | 37 | public void setId(Long id) { 38 | this.id = id; 39 | } 40 | 41 | public Long getId() { 42 | return id; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/src/main/java/br/com/caelum/example/model/Item.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.model; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | 5 | @XStreamAlias("item") 6 | public class Item { 7 | private Integer id; 8 | private String name; 9 | private Double price; 10 | 11 | public Item(String name, Double price) { 12 | this.name = name; 13 | this.price = price; 14 | } 15 | 16 | public Integer getId() { 17 | return id; 18 | } 19 | 20 | public void setId(Integer id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String nome) { 29 | this.name = nome; 30 | } 31 | 32 | public Double getPrice() { 33 | return price; 34 | } 35 | 36 | public void setPrice(Double preco) { 37 | this.price = preco; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/client/src/main/java/br/com/caelum/example/model/Payment.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.model; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | 5 | @XStreamAlias("payment") 6 | public class Payment { 7 | 8 | private Status status = Status.INVALID; 9 | private final Double amount; 10 | private Long id; 11 | 12 | public Payment(Double amount) { 13 | this.amount = amount; 14 | } 15 | 16 | public Double getAmount() { 17 | return amount; 18 | } 19 | 20 | public Status getStatus() { 21 | return status; 22 | } 23 | 24 | public static enum Status { 25 | ACCEPTED, INVALID 26 | } 27 | 28 | public void setId(Long id) { 29 | this.id = id; 30 | } 31 | 32 | public Long getId() { 33 | return id; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/.gitignore: -------------------------------------------------------------------------------- 1 | build -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/README -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | 3 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/jsp/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/jsp/.DS_Store -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/.jars: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/.jars -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/aopalliance.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/aopalliance.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/aspectjrt.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/aspectjrt.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/cglib-nodep-2.1_3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/cglib-nodep-2.1_3.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/commons-fileupload-1.2.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/commons-fileupload-1.2.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/commons-logging.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/commons-logging.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/javassist-3.9.0.GA.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/javassist-3.9.0.GA.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/jettison-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/jettison-1.2.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/jstl-api-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/jstl-api-1.2.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/jstl-impl-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/jstl-impl-1.2.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/log4j-1.2.15.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/log4j-1.2.15.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/mirror-1.5.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/mirror-1.5.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/objenesis-1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/objenesis-1.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/ognl-2.7.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/ognl-2.7.3.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.aop-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.aop-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.asm-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.asm-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.aspects-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.aspects-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.beans-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.beans-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.context-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.context-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.core-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.core-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.expression-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.expression-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.web-3.0.0.RELEASE.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/org.springframework.web-3.0.0.RELEASE.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/paranamer-2.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/paranamer-2.2.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/slf4j-api-1.5.8.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/slf4j-api-1.5.8.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/slf4j-log4j12-1.5.8.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/slf4j-log4j12-1.5.8.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/xstream-1.3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/WEB-INF/lib/xstream-1.3.1.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | br.com.caelum.vraptor.packages 9 | br.com.caelum.vraptor.restfulie 10 | 11 | 12 | 13 | vraptor 14 | br.com.caelum.vraptor.VRaptor 15 | 16 | 17 | 18 | vraptor 19 | /* 20 | FORWARD 21 | REQUEST 22 | 23 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 2 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 | 4 | 5 | 6 | 7 | 8 | VRaptor Blank Project 9 | 10 | 11 | It works!! ${variable} 12 | 13 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/WebContent/vraptor-3.2.0-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/rest_from_scratch/part_2/server/WebContent/vraptor-3.2.0-SNAPSHOT.jar -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/controller/Basket.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.controller; 2 | 3 | import java.util.List; 4 | 5 | import br.com.caelum.example.model.Item; 6 | import br.com.caelum.example.model.Payment; 7 | import br.com.caelum.example.model.Payment.Status; 8 | import br.com.caelum.vraptor.restfulie.hypermedia.HypermediaResource; 9 | import br.com.caelum.vraptor.restfulie.relation.RelationBuilder; 10 | 11 | public class Basket implements HypermediaResource { 12 | 13 | private long id; 14 | private List items; 15 | private Payment payment; 16 | 17 | public long getId() { 18 | return id; 19 | } 20 | 21 | public List getItems() { 22 | return items; 23 | } 24 | 25 | public Payment getPayment() { 26 | return payment; 27 | } 28 | 29 | @Override 30 | public void configureRelations(RelationBuilder builder) { 31 | builder.relation("self").uses(BasketsController.class).show(id); 32 | builder.relation("payment").uses(PaymentsController.class).create(id, null); 33 | } 34 | 35 | public void pay(Payment payment) { 36 | this.payment = payment; 37 | payment.setStatus(Status.ACCEPTED); 38 | } 39 | 40 | public void setId(Long id) { 41 | this.id = id; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/controller/Baskets.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.controller; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 7 | import br.com.caelum.vraptor.ioc.Component; 8 | 9 | @Component 10 | @ApplicationScoped 11 | public class Baskets { 12 | 13 | private Map baskets = new HashMap(); 14 | 15 | private long current = 1; 16 | 17 | 18 | public Basket get(Long id) { 19 | return baskets.get(id); 20 | } 21 | 22 | public void save(Basket basket) { 23 | long id = current++; 24 | basket.setId(id); 25 | baskets.put(id, basket); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/controller/BasketsController.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.controller; 2 | 3 | import static br.com.caelum.vraptor.view.Results.representation; 4 | import static br.com.caelum.vraptor.view.Results.status; 5 | import br.com.caelum.vraptor.Consumes; 6 | import br.com.caelum.vraptor.Get; 7 | import br.com.caelum.vraptor.Path; 8 | import br.com.caelum.vraptor.Post; 9 | import br.com.caelum.vraptor.Resource; 10 | import br.com.caelum.vraptor.Result; 11 | 12 | @Resource 13 | public class BasketsController { 14 | 15 | private final Result result; 16 | private final Baskets baskets; 17 | 18 | public BasketsController(Result result, Baskets baskets) { 19 | this.result = result; 20 | this.baskets = baskets; 21 | } 22 | 23 | @Get @Path("/basket/{id}") 24 | public void show(Long id) { 25 | result.use(representation()).from(baskets.get(id)).recursive().serialize(); 26 | } 27 | 28 | @Post @Path("/basket") 29 | @Consumes("application/xml") 30 | public void create(Basket basket) { 31 | baskets.save(basket); 32 | result.use(status()).created("/basket/" + basket.getId()); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/controller/ItemsController.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.controller; 2 | 3 | import static br.com.caelum.vraptor.view.Results.representation; 4 | import static br.com.caelum.vraptor.view.Results.status; 5 | import br.com.caelum.example.infra.Database; 6 | import br.com.caelum.example.model.Item; 7 | import br.com.caelum.vraptor.Consumes; 8 | import br.com.caelum.vraptor.Get; 9 | import br.com.caelum.vraptor.Path; 10 | import br.com.caelum.vraptor.Post; 11 | import br.com.caelum.vraptor.Resource; 12 | import br.com.caelum.vraptor.Result; 13 | import br.com.caelum.vraptor.restfulie.Restfulie; 14 | import br.com.caelum.vraptor.restfulie.hypermedia.ConfigurableHypermediaResource; 15 | 16 | @Resource 17 | public class ItemsController { 18 | 19 | private final Database database; 20 | private final Result result; 21 | private final Restfulie restfulie; 22 | 23 | public ItemsController(Database database, Result result, Restfulie restfulie) { 24 | this.database = database; 25 | this.result = result; 26 | this.restfulie = restfulie; 27 | } 28 | 29 | @Get 30 | @Path("/items") 31 | public void list() { 32 | ConfigurableHypermediaResource resource = restfulie.enhance(database.lista()); 33 | resource.relation("basket").uses(BasketsController.class).create(null); 34 | 35 | result.use(representation()).from(resource, "items").serialize(); 36 | } 37 | 38 | @Get 39 | @Path("/items/{id}") 40 | public void show(int id) { 41 | Item item = database.get(id); 42 | result.use(representation()).from(item).serialize(); 43 | } 44 | 45 | @Post 46 | @Consumes 47 | @Path("/items") 48 | public void create(Item item) { 49 | database.adiciona(item); 50 | result.use(status()).created("/items/" + item.getId()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/controller/PaymentsController.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.controller; 2 | 3 | import static br.com.caelum.vraptor.view.Results.representation; 4 | import static br.com.caelum.vraptor.view.Results.status; 5 | import br.com.caelum.example.infra.Payments; 6 | import br.com.caelum.example.model.Payment; 7 | import br.com.caelum.vraptor.Consumes; 8 | import br.com.caelum.vraptor.Get; 9 | import br.com.caelum.vraptor.Path; 10 | import br.com.caelum.vraptor.Post; 11 | import br.com.caelum.vraptor.Resource; 12 | import br.com.caelum.vraptor.Result; 13 | 14 | @Resource 15 | public class PaymentsController { 16 | 17 | private final Result result; 18 | private final Baskets baskets; 19 | private final Payments payments; 20 | 21 | public PaymentsController(Result result, Baskets baskets, Payments payments) { 22 | this.result = result; 23 | this.baskets = baskets; 24 | this.payments = payments; 25 | } 26 | 27 | @Post @Path("/basket/{id}/payment") 28 | @Consumes("application/xml") 29 | public void create(Long id, Payment payment) { 30 | Basket basket = baskets.get(id); 31 | basket.pay(payment); 32 | payments.save(payment); 33 | 34 | result.use(status()).created("/payment/" + payment.getId()); 35 | } 36 | 37 | @Get @Path("/payment/{id}") 38 | public void show(Long id) { 39 | result.use(representation()).from(payments.get(id)).serialize(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/infra/Database.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.infra; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import br.com.caelum.example.model.Item; 7 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 8 | import br.com.caelum.vraptor.ioc.Component; 9 | 10 | @Component 11 | @ApplicationScoped 12 | public class Database { 13 | private int contador = 0; 14 | private final List items; 15 | 16 | public Database() { 17 | this.items = new ArrayList(); 18 | this.adiciona(new Item("Chave", 20.0)); 19 | this.adiciona(new Item("Lousa", 35.0)); 20 | } 21 | 22 | public void adiciona(Item item) { 23 | item.setId(++contador); 24 | this.items.add(item); 25 | } 26 | 27 | public Item get(int id) { 28 | if(id > this.items.size()) { 29 | return null; 30 | } 31 | return this.items.get(id - 1); 32 | } 33 | 34 | public List lista() { 35 | return items; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/infra/Payments.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.infra; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import br.com.caelum.example.model.Payment; 7 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 8 | import br.com.caelum.vraptor.ioc.Component; 9 | 10 | @Component 11 | @ApplicationScoped 12 | public class Payments { 13 | 14 | private Map payments = new HashMap(); 15 | 16 | private long next; 17 | 18 | public Payment get(Long id) { 19 | return payments.get(id); 20 | } 21 | 22 | public void save(Payment payment) { 23 | long id = next++; 24 | payment.setId(id); 25 | payments.put(id, payment); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/model/Item.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.model; 2 | 3 | import br.com.caelum.example.controller.ItemsController; 4 | import br.com.caelum.vraptor.restfulie.hypermedia.HypermediaResource; 5 | import br.com.caelum.vraptor.restfulie.relation.RelationBuilder; 6 | 7 | import com.thoughtworks.xstream.annotations.XStreamAlias; 8 | 9 | @XStreamAlias("item") 10 | public class Item implements HypermediaResource { 11 | private Integer id; 12 | private String name; 13 | private Double price; 14 | 15 | public Item(String nome, Double preco) { 16 | this.name = nome; 17 | this.price = preco; 18 | } 19 | 20 | public Integer getId() { 21 | return id; 22 | } 23 | 24 | public void setId(Integer id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String nome) { 33 | this.name = nome; 34 | } 35 | 36 | public Double getPrice() { 37 | return price; 38 | } 39 | 40 | public void setPrice(Double preco) { 41 | this.price = preco; 42 | } 43 | 44 | @Override 45 | public void configureRelations(RelationBuilder builder) { 46 | builder.relation("self").uses(ItemsController.class).show(id); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/model/Payment.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.model; 2 | 3 | import br.com.caelum.example.controller.PaymentsController; 4 | import br.com.caelum.vraptor.restfulie.hypermedia.HypermediaResource; 5 | import br.com.caelum.vraptor.restfulie.relation.RelationBuilder; 6 | 7 | public class Payment implements HypermediaResource { 8 | 9 | private Long id; 10 | private Status status = Status.INVALID; 11 | private final Double amount; 12 | 13 | public Payment(Double amount) { 14 | this.amount = amount; 15 | } 16 | 17 | public Double getAmount() { 18 | return amount; 19 | } 20 | 21 | public Status getStatus() { 22 | return status; 23 | } 24 | 25 | public void setStatus(Status status) { 26 | this.status = status; 27 | } 28 | 29 | public static enum Status { 30 | ACCEPTED, INVALID 31 | } 32 | 33 | @Override 34 | public void configureRelations(RelationBuilder builder) { 35 | builder.relation("self").uses(PaymentsController.class).show(id); 36 | } 37 | 38 | public void setId(Long id) { 39 | this.id = id; 40 | } 41 | 42 | public Long getId() { 43 | return id; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/vraptor/component/CustomJsonDeserializer.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.vraptor.component; 2 | 3 | import br.com.caelum.example.model.Item; 4 | import br.com.caelum.vraptor.deserialization.JsonDeserializer; 5 | import br.com.caelum.vraptor.http.ParameterNameProvider; 6 | import br.com.caelum.vraptor.interceptor.TypeNameExtractor; 7 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 8 | import br.com.caelum.vraptor.ioc.Component; 9 | 10 | import com.thoughtworks.xstream.XStream; 11 | 12 | @ApplicationScoped 13 | @Component 14 | public class CustomJsonDeserializer extends JsonDeserializer { 15 | 16 | public CustomJsonDeserializer(ParameterNameProvider provider, 17 | TypeNameExtractor extractor) { 18 | super(provider, extractor); 19 | } 20 | 21 | /** 22 | * Extension point to configure your xstream instance. 23 | * @return the configured xstream instance 24 | */ 25 | protected XStream getXStream() { 26 | XStream xstream = super.getXStream(); 27 | xstream.processAnnotations(Item.class); 28 | return xstream; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/vraptor/component/CustomXStreamXMLDeserializer.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.vraptor.component; 2 | 3 | import br.com.caelum.vraptor.deserialization.XStreamXMLDeserializer; 4 | import br.com.caelum.vraptor.http.ParameterNameProvider; 5 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 6 | import br.com.caelum.vraptor.ioc.Component; 7 | 8 | import com.thoughtworks.xstream.XStream; 9 | import com.thoughtworks.xstream.io.xml.DomDriver; 10 | @ApplicationScoped 11 | @Component 12 | public class CustomXStreamXMLDeserializer extends XStreamXMLDeserializer { 13 | 14 | public CustomXStreamXMLDeserializer(ParameterNameProvider provider) { 15 | super(provider); 16 | } 17 | 18 | /** 19 | * Extension point to configure your xstream instance. 20 | * @return the configured xstream instance 21 | */ 22 | @Override 23 | protected XStream getXStream() { 24 | XStream xStream = new XStream(new DomDriver()); 25 | xStream.alias("item", br.com.caelum.example.model.Item.class); 26 | return xStream; 27 | } 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/br/com/caelum/example/vraptor/component/JSONSerializationCustomized.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.example.vraptor.component; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import javax.servlet.http.HttpServletResponse; 7 | 8 | import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor; 9 | import br.com.caelum.vraptor.interceptor.TypeNameExtractor; 10 | import br.com.caelum.vraptor.ioc.Component; 11 | import br.com.caelum.vraptor.serialization.ProxyInitializer; 12 | import br.com.caelum.vraptor.serialization.xstream.XStreamJSONSerialization; 13 | 14 | import com.thoughtworks.xstream.XStream; 15 | import com.thoughtworks.xstream.converters.MarshallingContext; 16 | import com.thoughtworks.xstream.converters.collections.CollectionConverter; 17 | import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 18 | import com.thoughtworks.xstream.mapper.Mapper; 19 | 20 | @Component 21 | public class JSONSerializationCustomized extends XStreamJSONSerialization { 22 | 23 | public JSONSerializationCustomized(HttpServletResponse response, 24 | TypeNameExtractor extractor, ProxyInitializer initializer) { 25 | super(response, extractor, initializer); 26 | } 27 | 28 | @Override 29 | protected XStream getXStream() { 30 | XStream xstream = super.getXStream(); 31 | MyCollectionConverter converter = new MyCollectionConverter(xstream 32 | .getMapper()); 33 | xstream.registerConverter(converter); 34 | return xstream; 35 | } 36 | 37 | public static void main(String[] args) { 38 | JSONSerializationCustomized ser = new JSONSerializationCustomized(null, new DefaultTypeNameExtractor(), null); 39 | XStream xstream = ser.getXStream(); 40 | List l = new ArrayList(); 41 | l.add(new Item("a", "b")); 42 | l.add(new Item("d", "c")); 43 | System.out.println(xstream.toXML(l)); 44 | List l2 = (List) xstream.fromXML(xstream.toXML(l)); 45 | System.out.println(l2); 46 | } 47 | 48 | } 49 | 50 | class Item { 51 | public String name; 52 | public String age; 53 | public Item(String name, String age) { 54 | super(); 55 | this.name = name; 56 | this.age = age; 57 | } 58 | 59 | } 60 | 61 | class MyCollectionConverter extends CollectionConverter { 62 | public MyCollectionConverter(Mapper mapper) { 63 | super(mapper); 64 | } 65 | 66 | protected void writeItem(Object item, MarshallingContext context, 67 | HierarchicalStreamWriter writer) { 68 | // PUBLISHED API METHOD! If changing signature, ensure backwards 69 | // compatability. 70 | if (item == null) { 71 | super.writeItem(item, context, writer); 72 | } else { 73 | String name = mapper().serializedClass(item.getClass()); 74 | writer.startNode(name); 75 | super.writeItem(item, context, writer); 76 | writer.endNode(); 77 | } 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /example/rest_from_scratch/part_2/server/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /example/server/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /example/server/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | restbucks server 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.wst.jsdt.core.javascriptValidator 10 | 11 | 12 | 13 | 14 | org.eclipse.jdt.core.javabuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.wst.common.project.facet.core.builder 20 | 21 | 22 | 23 | 24 | org.eclipse.wst.validation.validationbuilder 25 | 26 | 27 | 28 | 29 | 30 | org.eclipse.jem.workbench.JavaEMFNature 31 | org.eclipse.wst.common.modulecore.ModuleCoreNature 32 | org.eclipse.wst.common.project.facet.core.nature 33 | org.eclipse.jdt.core.javanature 34 | org.eclipse.wst.jsdt.core.jsNature 35 | 36 | 37 | -------------------------------------------------------------------------------- /example/server/.settings/.jsdtscope: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /example/server/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Wed Feb 10 19:49:33 BRST 2010 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 6 | org.eclipse.jdt.core.compiler.compliance=1.5 7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.source=1.5 13 | -------------------------------------------------------------------------------- /example/server/.settings/org.eclipse.wst.common.component: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /example/server/.settings/org.eclipse.wst.common.project.facet.core.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/server/.settings/org.eclipse.wst.jsdt.ui.superType.container: -------------------------------------------------------------------------------- 1 | org.eclipse.wst.jsdt.launching.baseBrowserLibrary -------------------------------------------------------------------------------- /example/server/.settings/org.eclipse.wst.jsdt.ui.superType.name: -------------------------------------------------------------------------------- 1 | Window -------------------------------------------------------------------------------- /example/server/WebContent/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/jsp/index/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | VRaptor Blank Project 6 | 7 | 8 | It works! 9 | 10 | -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/jsp/ordering/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 | 3 | 4 | 5 |
6 | Order #${order.id } - ${order.location }
7 | Status: ${order.status }
8 |
    9 | 10 |
  • ${item.quantity} x ${item.drink } (${item.size }, ${item.milk })
  • 11 |
    12 |
13 |
14 |
15 | 16 | -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/.jars: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/.jars -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/aspectjrt.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/aspectjrt.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/cglib-nodep-2.1_3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/cglib-nodep-2.1_3.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/commons-fileupload-1.2.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/commons-fileupload-1.2.1.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/commons-io-1.3.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/commons-io-1.3.2.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/commons-logging.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/commons-logging.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/commons-vfs-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/commons-vfs-1.0.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/google-collect-1.0-rc2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/google-collect-1.0-rc2.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/javassist-3.8.0.GA.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/javassist-3.8.0.GA.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/jstl.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/jstl.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/log4j-1.2.15.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/log4j-1.2.15.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/mirror-1.5.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/mirror-1.5.1.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/objenesis-1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/objenesis-1.1.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/ognl-2.7.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/ognl-2.7.3.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/paranamer-1.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/paranamer-1.5.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/slf4j-api-1.5.6.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/slf4j-api-1.5.6.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/slf4j-log4j12-1.5.6.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/slf4j-log4j12-1.5.6.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/spring-2.5.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/spring-2.5.5.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/standard-1.1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/standard-1.1.2.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/vraptor-3.1.2-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/vraptor-3.1.2-SNAPSHOT.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/xpp3_min-1.1.4c.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/xpp3_min-1.1.4c.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/lib/xstream-1.3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/WebContent/WEB-INF/lib/xstream-1.3.1.jar -------------------------------------------------------------------------------- /example/server/WebContent/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | vraptor-blank-project 7 | 8 | 9 | br.com.caelum.vraptor.packages 10 | br.com.caelum.vraptor.restfulie,br.com.caelum.vraptor.restbucks 11 | 12 | 13 | 14 | vraptor 15 | br.com.caelum.vraptor.VRaptor 16 | 17 | 18 | 19 | vraptor 20 | /* 21 | FORWARD 22 | REQUEST 23 | 24 | 25 | 26 | index.html 27 | 28 | -------------------------------------------------------------------------------- /example/server/WebContent/index.html: -------------------------------------------------------------------------------- 1 | 2 | Restbucks by caelum
3 | Implemented using vraptor and restfulie java release. 4 | 5 | -------------------------------------------------------------------------------- /example/server/build-lib/hamcrest-all-1.2RC3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/build-lib/hamcrest-all-1.2RC3.jar -------------------------------------------------------------------------------- /example/server/build-lib/junit-4.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/build-lib/junit-4.5.jar -------------------------------------------------------------------------------- /example/server/build-lib/mockito-all-1.8.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/example/server/build-lib/mockito-all-1.8.1.jar -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/Item.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import java.util.List; 4 | 5 | import br.com.caelum.vraptor.restbucks.web.ItemController; 6 | import br.com.caelum.vraptor.restfulie.Restfulie; 7 | import br.com.caelum.vraptor.restfulie.hypermedia.HypermediaResource; 8 | import br.com.caelum.vraptor.restfulie.relation.Relation; 9 | 10 | import com.thoughtworks.xstream.annotations.XStreamAlias; 11 | 12 | @XStreamAlias("item") 13 | public class Item implements HypermediaResource{ 14 | enum Coffee {LATTE, CAPPUCINO, ESPRESSO}; 15 | enum Milk {SKIM, SEMI, WHOLE}; 16 | enum Size {SMALL, MEDIUM, LARGE}; 17 | 18 | private Coffee drink; 19 | private int quantity; 20 | private Milk milk; 21 | private Size size; 22 | private int id; 23 | 24 | private transient Order order; 25 | 26 | public Item(Coffee drink, int quantity, Milk milk, Size size) { 27 | this.drink = drink; 28 | this.quantity = quantity; 29 | this.milk = milk; 30 | this.size = size; 31 | } 32 | Item() { 33 | 34 | } 35 | 36 | public void use(Order order, int id) { 37 | this.order = order; 38 | this.id = id; 39 | } 40 | 41 | public Coffee getDrink() { 42 | return drink; 43 | } 44 | 45 | public void setDrink(Coffee name) { 46 | this.drink = name; 47 | } 48 | 49 | public int getQuantity() { 50 | return quantity; 51 | } 52 | 53 | public void setQuantity(int quantity) { 54 | this.quantity = quantity; 55 | } 56 | 57 | public Milk getMilk() { 58 | return milk; 59 | } 60 | 61 | public void setMilk(Milk milk) { 62 | this.milk = milk; 63 | } 64 | 65 | public Size getSize() { 66 | return size; 67 | } 68 | 69 | public void setSize(Size size) { 70 | this.size = size; 71 | } 72 | 73 | public List getRelations(Restfulie control) { 74 | control.relation("self").uses(ItemController.class).get(order, this); 75 | return control.getRelations(); 76 | } 77 | 78 | public int getId() { 79 | return id; 80 | } 81 | public void setId(int id) { 82 | this.id = id; 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/OrderControllerInterceptor.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import br.com.caelum.vraptor.Intercepts; 4 | import br.com.caelum.vraptor.core.RequestInfo; 5 | import br.com.caelum.vraptor.core.Routes; 6 | import br.com.caelum.vraptor.restfulie.Restfulie; 7 | import br.com.caelum.vraptor.restfulie.controller.ResourceControllerInterceptor; 8 | import br.com.caelum.vraptor.view.Status; 9 | 10 | /** 11 | * Create a generic component which looks up all StateControl's.
12 | * Registers one interceptor which receives this component and intercepts all state controls. 13 | */ 14 | @Intercepts 15 | public class OrderControllerInterceptor extends ResourceControllerInterceptor{ 16 | 17 | public OrderControllerInterceptor(OrderStateControl control, Restfulie restfulie, Status status, RequestInfo info, Routes routes) { 18 | super(control, restfulie, status, info, routes); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/OrderDatabase.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.ArrayList; 5 | import java.util.Collection; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 10 | import br.com.caelum.vraptor.ioc.Component; 11 | import br.com.caelum.vraptor.restbucks.Item.Coffee; 12 | import br.com.caelum.vraptor.restbucks.Item.Milk; 13 | import br.com.caelum.vraptor.restbucks.Item.Size; 14 | import br.com.caelum.vraptor.restbucks.Order.Location; 15 | 16 | 17 | /** 18 | * Simple database simulation. 19 | */ 20 | @Component 21 | @ApplicationScoped 22 | public class OrderDatabase { 23 | 24 | private static int total = 1; 25 | private Map orders = new HashMap(); 26 | 27 | public OrderDatabase() { 28 | Item item = new Item(Coffee.LATTE, 1, Milk.WHOLE, Size.SMALL); 29 | ArrayList items = new ArrayList(); 30 | items.add(item); 31 | 32 | Order order = new Order("unpaid", items, Location.TO_TAKE); 33 | order.setId("1"); 34 | save(order.getId(), order); 35 | 36 | order = new Order("paid", items, Location.TO_TAKE); 37 | order.pay(new Payment("1234123412341234", "guilherme silveira", 11, 12, 38 | new BigDecimal(1020.0))); 39 | order.setId("2"); 40 | save(order.getId(), order); 41 | } 42 | 43 | public synchronized void save(Order order) { 44 | order.setStatus("unpaid"); 45 | total++; 46 | String id = String.valueOf(total); 47 | order.setId(id); 48 | orders.put(id, order); 49 | } 50 | 51 | public void save(String id, Order order) { 52 | orders.put(id, order); 53 | } 54 | 55 | public boolean orderExists(String id) { 56 | return orders.containsKey(id); 57 | } 58 | 59 | private static final long serialVersionUID = 1L; 60 | 61 | public Order getOrder(String id) { 62 | return orders.get(id); 63 | } 64 | 65 | public Collection all() { 66 | return orders.values(); 67 | } 68 | 69 | public void delete(Order order) { 70 | orders.remove(order.getId()); 71 | } 72 | 73 | public void update(Order order) { 74 | orders.put(order.getId(), order); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/OrderMachineController.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import br.com.caelum.vraptor.ioc.Component; 4 | import br.com.caelum.vraptor.ioc.RequestScoped; 5 | import br.com.caelum.vraptor.restbucks.restfulie.StateMachineController; 6 | 7 | 8 | @Component 9 | @RequestScoped 10 | public class OrderMachineController implements StateMachineController{ 11 | 12 | public String getBaseUri() { 13 | return "http://localhost:8080/orders/{order.id}"; 14 | } 15 | 16 | public Order retrieve(String id) { 17 | return null; // session.load(Order.class, Long.parseLong(id)); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/OrderStateControl.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 4 | import br.com.caelum.vraptor.ioc.Component; 5 | import br.com.caelum.vraptor.restbucks.web.OrderingController; 6 | import br.com.caelum.vraptor.restfulie.controller.ResourceControl; 7 | 8 | @Component 9 | @ApplicationScoped 10 | public class OrderStateControl implements ResourceControl { 11 | 12 | private final OrderDatabase database; 13 | 14 | public OrderStateControl(OrderDatabase database) { 15 | this.database = database; 16 | } 17 | 18 | @SuppressWarnings("unchecked") 19 | public Class[] getControllers() { 20 | return new Class[]{OrderingController.class}; 21 | } 22 | 23 | public Order retrieve(String id) { 24 | return database.getOrder(id); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/Payment.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Calendar; 5 | 6 | import com.thoughtworks.xstream.annotations.XStreamAlias; 7 | 8 | @XStreamAlias("payment") 9 | public class Payment { 10 | 11 | @XStreamAlias("card-number") 12 | private String cardNumber; 13 | @XStreamAlias("cardholder-name") 14 | private String cardholderName; 15 | @XStreamAlias("expiry-month") 16 | private int expiryMonth; 17 | @XStreamAlias("expiry-year") 18 | private int expiryYear; 19 | private BigDecimal amount; 20 | private Calendar createdAt; 21 | 22 | public Payment(String cardNumber, String cardholderName, int expiryMonth, 23 | int expiryYear, BigDecimal amount) { 24 | super(); 25 | this.cardNumber = cardNumber; 26 | this.cardholderName = cardholderName; 27 | this.expiryMonth = expiryMonth; 28 | this.expiryYear = expiryYear; 29 | this.amount = amount; 30 | } 31 | 32 | public BigDecimal getAmount() { 33 | return amount; 34 | } 35 | 36 | public Calendar getCreatedAt() { 37 | return this.createdAt; 38 | } 39 | public void setCreatedAt(Calendar createdAt) { 40 | this.createdAt = createdAt; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/Receipt.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import java.util.Calendar; 4 | import java.util.List; 5 | 6 | import br.com.caelum.vraptor.restbucks.web.OrderingController; 7 | import br.com.caelum.vraptor.restfulie.Restfulie; 8 | import br.com.caelum.vraptor.restfulie.hypermedia.HypermediaResource; 9 | import br.com.caelum.vraptor.restfulie.relation.Relation; 10 | 11 | public class Receipt implements HypermediaResource{ 12 | 13 | private final Calendar paymentTime= Calendar.getInstance(); 14 | private final Order order; 15 | 16 | public Receipt(Order order) { 17 | this.order = order; 18 | } 19 | 20 | public List getRelations(Restfulie control) { 21 | control.transition("order").uses(OrderingController.class).get(order); 22 | return control.getRelations(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/XmlDeserializer.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import br.com.caelum.vraptor.deserialization.XStreamXMLDeserializer; 4 | import br.com.caelum.vraptor.http.ParameterNameProvider; 5 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 6 | import br.com.caelum.vraptor.ioc.Component; 7 | 8 | import com.thoughtworks.xstream.XStream; 9 | 10 | /** 11 | * Returns our pre-configured version of xstream. 12 | * 13 | * @author guilherme silveira 14 | */ 15 | @Component 16 | @ApplicationScoped 17 | public class XmlDeserializer extends XStreamXMLDeserializer { 18 | 19 | public XmlDeserializer(ParameterNameProvider provider) { 20 | super(provider); 21 | } 22 | 23 | @Override 24 | protected XStream getXStream() { 25 | XStream instance = super.getXStream(); 26 | instance.processAnnotations(Order.class); 27 | instance.processAnnotations(Item.class); 28 | return instance; 29 | } 30 | 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/XmlSerializer.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import javax.servlet.http.HttpServletResponse; 4 | 5 | import br.com.caelum.vraptor.config.Configuration; 6 | import br.com.caelum.vraptor.interceptor.TypeNameExtractor; 7 | import br.com.caelum.vraptor.ioc.Component; 8 | import br.com.caelum.vraptor.ioc.RequestScoped; 9 | import br.com.caelum.vraptor.restfulie.Restfulie; 10 | import br.com.caelum.vraptor.restfulie.serialization.RestfulSerialization; 11 | 12 | import com.thoughtworks.xstream.XStream; 13 | 14 | @Component 15 | @RequestScoped 16 | public class XmlSerializer extends RestfulSerialization{ 17 | 18 | public XmlSerializer(HttpServletResponse response, 19 | TypeNameExtractor extractor, Restfulie restfulie, Configuration config) { 20 | super(response, extractor, restfulie, config); 21 | } 22 | 23 | @Override 24 | protected XStream getXStream() { 25 | XStream instance = super.getXStream(); 26 | instance.processAnnotations(Order.class); 27 | instance.processAnnotations(Item.class); 28 | return instance; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/restfulie/CustomRoutes.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks.restfulie; 2 | 3 | import java.lang.reflect.TypeVariable; 4 | 5 | import br.com.caelum.vraptor.http.route.Router; 6 | import br.com.caelum.vraptor.http.route.RoutesConfiguration; 7 | import br.com.caelum.vraptor.ioc.ApplicationScoped; 8 | import br.com.caelum.vraptor.restbucks.OrderMachineController; 9 | 10 | 11 | @ApplicationScoped 12 | public class CustomRoutes implements RoutesConfiguration{ 13 | 14 | public void config(Router router) { 15 | Class type = OrderMachineController.class; 16 | TypeVariable[] types = type.getTypeParameters(); 17 | Class entityType = types[0].getClass(); 18 | try { 19 | Class stateType = Class.forName(entityType.getName() + "StateControl"); 20 | //StateControl control = (StateControl) stateType.newInstance(); 21 | //List transitions = control.getTransitions(); 22 | //for (Transition transition : transitions) { 23 | // 24 | //} 25 | } catch (Exception e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/restfulie/StateMachineController.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks.restfulie; 2 | 3 | public interface StateMachineController { 4 | 5 | public String getBaseUri(); 6 | public T retrieve(String id); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/web/ItemController.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks.web; 2 | 3 | import static br.com.caelum.vraptor.view.Results.xml; 4 | import br.com.caelum.vraptor.Path; 5 | import br.com.caelum.vraptor.Resource; 6 | import br.com.caelum.vraptor.Result; 7 | import br.com.caelum.vraptor.restbucks.Item; 8 | import br.com.caelum.vraptor.restbucks.Order; 9 | import br.com.caelum.vraptor.restbucks.OrderDatabase; 10 | import br.com.caelum.vraptor.view.Status; 11 | 12 | @Resource 13 | public class ItemController { 14 | 15 | private final Result result; 16 | private final Status status; 17 | private final OrderDatabase database; 18 | 19 | public ItemController(Result result, Status status, 20 | OrderDatabase database) { 21 | this.result = result; 22 | this.status = status; 23 | this.database = database; 24 | } 25 | 26 | @Path("/orders/{order.id}/items/{item.id}") 27 | public void get(Order order, Item item) { 28 | order = database.getOrder(order.getId()); 29 | if (order != null) { 30 | result.use(xml()).from(order.findItem(item.getId())).serialize(); 31 | } else { 32 | status.notFound(); 33 | } 34 | } 35 | 36 | @Path("/orders/{order.id}/items") 37 | public void index(Order order) { 38 | order = database.getOrder(order.getId()); 39 | if (order != null) { 40 | result.use(xml()).from(order.getItems(), "items").serialize(); 41 | } else { 42 | status.notFound(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /example/server/src/br/com/caelum/vraptor/restbucks/web/PaymentController.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks.web; 2 | 3 | import static br.com.caelum.vraptor.view.Results.xml; 4 | import br.com.caelum.vraptor.Get; 5 | import br.com.caelum.vraptor.Path; 6 | import br.com.caelum.vraptor.Resource; 7 | import br.com.caelum.vraptor.Result; 8 | import br.com.caelum.vraptor.restbucks.Order; 9 | import br.com.caelum.vraptor.restbucks.OrderDatabase; 10 | import br.com.caelum.vraptor.view.Status; 11 | 12 | @Resource 13 | public class PaymentController { 14 | 15 | private final Result result; 16 | private final Status status; 17 | private final OrderDatabase database; 18 | 19 | public PaymentController(Result result, Status status, 20 | OrderDatabase database) { 21 | this.result = result; 22 | this.status = status; 23 | this.database = database; 24 | } 25 | 26 | @Get 27 | @Path("/orders/{order.id}/payment") 28 | public void get(Order order) { 29 | order = database.getOrder(order.getId()); 30 | if (order != null) { 31 | result.use(xml()).from(order.getPayment()).serialize(); 32 | } else { 33 | status.notFound(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /example/server/test/br/com/caelum/vraptor/restbucks/OrderTest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import static org.hamcrest.Matchers.is; 4 | import static org.junit.Assert.assertThat; 5 | import static org.mockito.Mockito.mock; 6 | import static org.mockito.Mockito.when; 7 | 8 | import java.math.BigDecimal; 9 | import java.util.Calendar; 10 | 11 | import org.junit.Test; 12 | 13 | public class OrderTest { 14 | 15 | @Test 16 | public void whenOrderIsPaidMoreThan10SecondsAgoItIsReady() { 17 | 18 | Payment payment = mock(Payment.class); 19 | 20 | when(payment.getAmount()).thenReturn(BigDecimal.ZERO); 21 | when(payment.getCreatedAt()).thenReturn(someSecondsAgo(15)); 22 | 23 | Order o = new Order(); 24 | o.pay(payment); 25 | 26 | assertThat(o.isReady(), is(true)); 27 | 28 | } 29 | 30 | @Test 31 | public void whenOrderIsPaidLessThan10SecondsAgoItIsNotReadyYet() { 32 | 33 | Payment payment = mock(Payment.class); 34 | 35 | when(payment.getAmount()).thenReturn(BigDecimal.ZERO); 36 | when(payment.getCreatedAt()).thenReturn(someSecondsAgo(3)); 37 | 38 | Order o = new Order(); 39 | o.pay(payment); 40 | 41 | assertThat(o.isReady(), is(false)); 42 | 43 | } 44 | 45 | private Calendar someSecondsAgo(int secs) { 46 | Calendar fifteenSecondsAgo = Calendar.getInstance(); 47 | fifteenSecondsAgo.add(Calendar.SECOND, -secs); 48 | return fifteenSecondsAgo; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /example/server/test/br/com/caelum/vraptor/restbucks/XmlDeserializerTest.java: -------------------------------------------------------------------------------- 1 | package br.com.caelum.vraptor.restbucks; 2 | 3 | import static org.hamcrest.Matchers.equalTo; 4 | import static org.junit.Assert.assertThat; 5 | 6 | import java.io.ByteArrayInputStream; 7 | import java.io.InputStream; 8 | 9 | import org.junit.Test; 10 | 11 | import com.thoughtworks.xstream.XStream; 12 | 13 | public class XmlDeserializerTest { 14 | 15 | private String item() { 16 | return " "+ 17 | " LATTE"+ 18 | " SEMI"+ 19 | " LARGE"+ 20 | " "; 21 | } 22 | 23 | private InputStream orderXml(String items) { 24 | return streamFor(""+ 25 | ""+ 26 | " 510"+ 27 | " TO_TAKE"+ 28 | " "+ 29 | items + 30 | " "+ 31 | " "); 32 | } 33 | 34 | @Test 35 | public void shouldBeCapableOfDeserializingBasicData() { 36 | Order order = deserialize(orderXml("")); 37 | assertThat(order.getId(), equalTo("510")); 38 | assertThat(order.getLocation(), equalTo(Order.Location.TO_TAKE)); 39 | assertThat(order.getItems().size(), equalTo(0)); 40 | } 41 | 42 | private Order deserialize(InputStream input) { 43 | XStream deserializer = createXStream(); 44 | Order order = (Order) deserializer.fromXML(input); 45 | return order; 46 | } 47 | 48 | private XStream createXStream() { 49 | return new XmlDeserializer(null).getXStream(); 50 | } 51 | 52 | private InputStream streamFor(String base) { 53 | return new ByteArrayInputStream(base.getBytes()); 54 | } 55 | 56 | @Test 57 | public void shouldBeCapableOfReadingAnItem() { 58 | Order order = deserialize(orderXml(item())); 59 | assertThat(order.getItems().size(), equalTo(1)); 60 | Item item = order.getItems().get(0); 61 | assertThat(item.getDrink(), equalTo(Item.Coffee.LATTE)); 62 | assertThat(item.getMilk(), equalTo(Item.Milk.SEMI)); 63 | assertThat(item.getSize(), equalTo(Item.Size.LARGE)); 64 | } 65 | 66 | @Test 67 | public void shouldBeCapableOfReadingItems() { 68 | Order order = deserialize(orderXml(item() + item())); 69 | assertThat(order.getItems().size(), equalTo(2)); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /jruby.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caelum/restfulie-java/83f8bca69b56eacfa0b6415297f9e513533d93d0/jruby.jar --------------------------------------------------------------------------------