http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/DefaultContentTypesResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/DefaultContentTypesResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/DefaultContentTypesResource.java new file mode 100644 index 0000000..01dd128 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/DefaultContentTypesResource.java @@ -0,0 +1,128 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import static org.apache.juneau.rest.annotation.Inherit.*; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.serializer.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testDefaultContentTypes", + defaultRequestHeaders={" Accept : text/s2 "," Content-Type : text/p2 "}, + parsers={DefaultContentTypesResource.P1.class,DefaultContentTypesResource.P2.class}, serializers={DefaultContentTypesResource.S1.class,DefaultContentTypesResource.S2.class} +) +@SuppressWarnings("synthetic-access") +public class DefaultContentTypesResource extends RestServlet { + private static final long serialVersionUID = 1L; + + @Consumes("text/p1") + public static class P1 extends DummyParser { public P1() {super("p1");}} + + @Consumes("text/p2") + public static class P2 extends DummyParser { public P2() {super("p2");}} + + @Consumes("text/p3") + public static class P3 extends DummyParser { public P3() {super("p3");}} + + @Produces("text/s1") + public static class S1 extends DummySerializer { public S1() {super("s1");}} + + @Produces("text/s2") + public static class S2 extends DummySerializer { public S2() {super("s2");}} + + @Produces("text/s3") + public static class S3 extends DummySerializer { public S3() {super("s3");}} + + /** + * Test that default Accept and Content-Type headers on servlet annotation are picked up. + */ + @RestMethod(name="PUT", path="/testDefaultHeadersOnServletAnnotation") + public String testDefaultHeadersOnServletAnnotation(@Body String in) { + return in; + } + + //==================================================================================================== + // Test that default Accept and Content-Type headers on servlet annotation are picked up + // when @RestMethod.parsers/serializers annotations are used. + //==================================================================================================== + @RestMethod(name="PUT", path="/testRestMethodParsersSerializers", parsers=P3.class, serializers=S3.class) + public String testRestMethodParsersSerializers(@Body String in) { + return in; + } + + //==================================================================================================== + // Test that default Accept and Content-Type headers on servlet annotation are picked up + // when @RestMethod.addParsers/addSerializers annotations are used. + //==================================================================================================== + @RestMethod(name="PUT", path="/testRestMethodAddParsersSerializers", parsers=P3.class, parsersInherit=PARSERS, serializers=S3.class, serializersInherit=SERIALIZERS) + public String testRestMethodAddParsersSerializers(@Body String in) { + return in; + } + + //==================================================================================================== + // Various Accept incantations. + //==================================================================================================== + @RestMethod(name="PUT", path="/testAccept") + public String testAccept(@Body String in) { + return in; + } + + //==================================================================================================== + // Test that default Accept and Content-Type headers on method annotation are picked up + // when @RestMethod.parsers/serializers annotations are used. + //==================================================================================================== + @RestMethod(name="PUT", path="/testRestMethodParserSerializerAnnotations", defaultRequestHeaders={"Accept: text/s3","Content-Type: text/p3"}, parsers=P3.class, serializers=S3.class) + public String testRestMethodParserSerializerAnnotations(@Body String in) { + return in; + } + + //==================================================================================================== + // Test that default Accept and Content-Type headers on method annotation are picked up + // when @RestMethod.addParsers/addSerializers annotations are used. + //==================================================================================================== + @RestMethod(name="PUT", path="/testRestMethodAddParsersSerializersAnnotations", defaultRequestHeaders={"Accept: text/s3","Content-Type: text/p3"}, parsers=P3.class, parsersInherit=PARSERS, serializers=S3.class, serializersInherit=SERIALIZERS) + public String testRestMethodAddParsersSerializersAnnotations(@Body String in) { + return in; + } + + public static class DummyParser extends ReaderParser { + private String name; + private DummyParser(String name) { + this.name = name; + } + @SuppressWarnings("unchecked") + @Override /* Parser */ + protected <T> T doParse(ParserSession session, ClassMeta<T> type) throws Exception { + return (T)name; + } + } + + public static class DummySerializer extends WriterSerializer { + private String name; + private DummySerializer(String name) { + this.name = name; + } + @Override /* Serializer */ + protected void doSerialize(SerializerSession session, Object output) throws Exception { + session.getWriter().write(name + "/" + output); + } + } +}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/ErrorConditionsResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/ErrorConditionsResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/ErrorConditionsResource.java new file mode 100644 index 0000000..8cb0890 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/ErrorConditionsResource.java @@ -0,0 +1,135 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * JUnit automated testcase resource. + * Validates correct parser is used. + */ +@RestResource( + path="/testErrorConditions" +) +public class ErrorConditionsResource extends RestServletDefault { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // Test non-existent properties + //==================================================================================================== + @RestMethod(name="PUT", path="/testNonExistentBeanProperties") + public String testNonExistentBeanProperties(@Body Test1 in) { + return "OK"; + } + + public static class Test1 { + public String f1; + } + + //==================================================================================================== + // Test trying to set properties to wrong data type + //==================================================================================================== + @RestMethod(name="PUT", path="/testWrongDataType") + public String testWrongDataType(@Body Test2 in) { + return "OK"; + } + + public static class Test2 { + public int f1; + } + + //==================================================================================================== + // Test trying to parse into class with non-public no-arg constructor. + //==================================================================================================== + @RestMethod(name="PUT", path="/testParseIntoNonConstructableBean") + public String testParseIntoNonConstructableBean(@Body Test3a in) { + return "OK"; + } + + public static class Test3a { + public int f1; + private Test3a(){} + } + + //==================================================================================================== + // Test trying to parse into non-static inner class + //==================================================================================================== + @RestMethod(name="PUT", path="/testParseIntoNonStaticInnerClass") + public String testParseIntoNonStaticInnerClass(@Body Test3b in) { + return "OK"; + } + + public class Test3b { + public Test3b(){} + } + + //==================================================================================================== + // Test trying to parse into non-public inner class + //==================================================================================================== + @RestMethod(name="PUT", path="/testParseIntoNonPublicInnerClass") + public String testParseIntoNonPublicInnerClass(@Body Test3b1 in) { + return "OK"; + } + + static class Test3b1 { + public Test3b1(){} + } + + //==================================================================================================== + // Test exception thrown during bean construction. + //==================================================================================================== + @RestMethod(name="PUT", path="/testThrownConstructorException") + public String testThrownConstructorException(@Body Test3c in) { + return "OK"; + } + + public static class Test3c { + public int f1; + private Test3c(){} + public static Test3c valueOf(String s) { + throw new RuntimeException("Test error"); + } + } + + //==================================================================================================== + // Test trying to set parameters to invalid types. + //==================================================================================================== + @RestMethod(name="PUT", path="/testSetParameterToInvalidTypes/{a1}") + public String testSetParameterToInvalidTypes(@Query("p1") int t1, @Path int a1, @Header("h1") int h1) { + return "OK"; + } + + //==================================================================================================== + // Test SC_NOT_FOUND & SC_METHOD_NOT_ALLOWED + //==================================================================================================== + @RestMethod(name="GET", path="/test404and405") + public String test404and405() { + return "OK"; + } + + //==================================================================================================== + // Test SC_PRECONDITION_FAILED + //==================================================================================================== + @RestMethod(name="GET", path="/test412", matchers=NeverMatcher.class) + public String test412() { + return "OK"; + } + + public static class NeverMatcher extends RestMatcher { + @Override /* RestMatcher */ + public boolean matches(RestRequest req) { + return false; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GroupsResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GroupsResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GroupsResource.java new file mode 100644 index 0000000..eafefd4 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GroupsResource.java @@ -0,0 +1,72 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.apache.juneau.internal.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.serializer.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testGroups" +) +public class GroupsResource extends RestServlet { + private static final long serialVersionUID = 1L; + + @Produces("text/s1,text/s2") + public static class SSerializer extends WriterSerializer { + @Override /* Serializer */ + protected void doSerialize(SerializerSession session, Object output) throws Exception { + session.getWriter().write("text/s," + output); + } + } + + @Consumes("text/p1,text/p2") + public static class PParser extends ReaderParser { + @SuppressWarnings("unchecked") + @Override /* Parser */ + protected <T> T doParse(ParserSession session, ClassMeta<T> type) throws Exception { + return (T)IOUtils.read(session.getReader()); + } + } + + + @Override /* RestServlet */ + public SerializerGroup createSerializers(ObjectMap properties, Class<?>[] beanFilters, Class<?>[] pojoSwaps) throws Exception { + return new SerializerGroup().append(SSerializer.class).setProperties(properties).addBeanFilters(beanFilters).addPojoSwaps(pojoSwaps); + } + + @Override /* RestServlet */ + public ParserGroup createParsers(ObjectMap properties, Class<?>[] beanFilters, Class<?>[] pojoSwaps) throws Exception { + return new ParserGroup().append(PParser.class).setProperties(properties).addBeanFilters(beanFilters).addPojoSwaps(pojoSwaps); + } + + //==================================================================================================== + // Serializer defined on class. + //==================================================================================================== + @RestMethod(name="GET", path="/testSerializerDefinedOnClass") + public String testSerializerDefinedOnClass_get() { + return "GET"; + } + + @RestMethod(name="PUT", path="/testSerializerDefinedOnClass") + public String testSerializerDefinedOnClass_put(@Body String in) { + return in; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GzipResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GzipResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GzipResource.java new file mode 100644 index 0000000..2595768 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/GzipResource.java @@ -0,0 +1,111 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import java.io.*; + +import org.apache.juneau.encoders.*; +import org.apache.juneau.plaintext.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * JUnit automated testcase resource. + */ +public class GzipResource { + + //================================================================================ + // Encoder for "myencoding" encoding + //================================================================================ + public static class MyEncoder extends GzipEncoder { + @Override /* Encoder */ + public String[] getCodings() { + return new String[]{"mycoding"}; + } + } + + //==================================================================================================== + // Test with no compression enabled. + //==================================================================================================== + @RestResource( + path="/testGzipOff", + serializers=PlainTextSerializer.class, + parsers=PlainTextParser.class + ) + public static class TestGzipOff extends RestServlet { + private static final long serialVersionUID = 1L; + @RestMethod(name="GET", path="/") + public String test1get() { + return "foo"; + } + @RestMethod(name="PUT", path="/") + public String test1put(@Body String in) { + return in; + } + } + + //==================================================================================================== + // Test with compression enabled. + //==================================================================================================== + @RestResource( + path="/testGzipOn", + serializers=PlainTextSerializer.class, + parsers=PlainTextParser.class, + encoders=MyEncoder.class + ) + public static class TestGzipOn extends RestServlet { + private static final long serialVersionUID = 1L; + @RestMethod(name="GET", path="/") + public String test1() { + return "foo"; + } + @RestMethod(name="PUT", path="/") + public String test1put(@Body String in) { + return in; + } + // This method bypasses the content type and encoding from + // the serializers and encoders when calling getOutputStream() directly. + @RestMethod(name="GET", path="/direct") + public void direct(RestResponse res) throws Exception { + res.setContentType("text/direct"); + OutputStream os = res.getOutputStream(); + os.write("test".getBytes()); + os.flush(); + } + + // This method bypasses the content type and encoding from + // the serializers and encoders when calling getWriter() directly. + @RestMethod(name="GET", path="/direct2") + public void direct2(RestResponse res) throws Exception { + Writer w = res.getWriter(); + w.append("test"); + w.flush(); + } + + // This method uses getNegotiatedWriter() which should use GZip encoding. + @RestMethod(name="GET", path="/direct3") + public void direct3(RestResponse res) throws Exception { + Writer w = res.getNegotiatedWriter(); + w.append("test"); + w.flush(); + } + + // This method overrides the set of encoders at the method level and so shouldn't use GZip encoding. + @RestMethod(name="GET", path="/direct4", inheritEncoders=false) + public void direct4(RestResponse res) throws Exception { + Writer w = res.getNegotiatedWriter(); + w.append("test"); + w.flush(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/InheritanceResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/InheritanceResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/InheritanceResource.java new file mode 100644 index 0000000..0e41572 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/InheritanceResource.java @@ -0,0 +1,317 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import static org.apache.juneau.rest.annotation.Inherit.*; + +import java.io.*; +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.apache.juneau.encoders.*; +import org.apache.juneau.json.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.rest.annotation.Properties; +import org.apache.juneau.serializer.*; +import org.apache.juneau.transform.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testInheritance", + serializers={InheritanceResource.S1.class,InheritanceResource.S2.class}, + parsers={InheritanceResource.P1.class,InheritanceResource.P2.class}, + encoders={InheritanceResource.E1.class,InheritanceResource.E2.class}, + pojoSwaps={InheritanceResource.F1Swap.class}, + properties={@Property(name="p1",value="v1"), @Property(name="p2",value="v2")} +) +public class InheritanceResource extends RestServlet { + private static final long serialVersionUID = 1L; + + @RestResource( + serializers={S3.class,S4.class}, + parsers={P3.class,P4.class}, + encoders={E3.class,E4.class}, + pojoSwaps={F2Swap.class}, + properties={@Property(name="p2",value="v2a"), @Property(name="p3",value="v3"), @Property(name="p4",value="v4")} + ) + public static class Sub extends InheritanceResource { + private static final long serialVersionUID = 1L; + } + + //==================================================================================================== + // Test serializer inheritance. + //==================================================================================================== + @RestResource(path="/testInheritanceSerializers") + public static class TestSerializers extends Sub { + private static final long serialVersionUID = 1L; + + // Should show ['text/s3','text/s4','text/s1','text/s2'] + @RestMethod( + name="GET", + path="/test1" + ) + public Reader test1(RestResponse res) { + return new StringReader(new ObjectList(res.getSupportedMediaTypes()).toString()); + } + + // Should show ['text/s5'] + @RestMethod( + name="GET", + path="/test2", + serializers=S5.class + ) + public Reader test2(RestResponse res) { + return new StringReader(new ObjectList(res.getSupportedMediaTypes()).toString()); + } + + // Should show ['text/s5','text/s3','text/s4','text/s1','text/s2'] + @RestMethod( + name="GET", + path="/test3", + serializers=S5.class, + serializersInherit=SERIALIZERS + ) + public Reader test3(RestResponse res) { + return new StringReader(new ObjectList(res.getSupportedMediaTypes()).toString()); + } + } + + //==================================================================================================== + // Test parser inheritance. + //==================================================================================================== + @RestResource(path="/testInheritanceParsers") + public static class TestParsers extends Sub { + private static final long serialVersionUID = 1L; + + // Should show ['text/p3','text/p4','text/p1','text/p2'] + @RestMethod( + name="GET", + path="/test1" + ) + public Reader test1(RestRequest req) { + return new StringReader(new ObjectList(req.getSupportedMediaTypes()).toString()); + } + + // Should show ['text/p5'] + @RestMethod( + name="GET", + path="/test2", + parsers=P5.class + ) + public Reader test2(RestRequest req) { + return new StringReader(new ObjectList(req.getSupportedMediaTypes()).toString()); + } + + // Should show ['text/p5','text/p3','text/p4','text/p1','text/p2'] + @RestMethod( + name="GET", + path="/test3", + parsers=P5.class, + parsersInherit=PARSERS + ) + public Reader test3(RestRequest req) { + return new StringReader(new ObjectList(req.getSupportedMediaTypes()).toString()); + } + } + + //==================================================================================================== + // Test encoder inheritance. + //==================================================================================================== + @RestResource(path="/testInheritanceEncoders") + public static class TestEncoders extends Sub { + private static final long serialVersionUID = 1L; + + // Should show ['e3','e4','e1','e2','identity'] + @RestMethod(name="GET", path="/test") + public Reader test(RestResponse res) throws RestServletException { + return new StringReader(new ObjectList(res.getSupportedEncodings()).toString()); + } + } + + //==================================================================================================== + // Test filter inheritance. + //==================================================================================================== + @RestResource(path="/testInheritanceTransforms", serializers=JsonSerializer.Simple.class) + public static class TestTransforms extends Sub { + private static final long serialVersionUID = 1L; + + // Should show ['F1Swap','F2Swap','Foo3'] + @RestMethod(name="GET", path="/test1") + public Object[] test1() { + return new Object[]{new Foo1(), new Foo2(), new Foo3()}; + } + + // Should show ['F1Swap','F2Swap','F3Swap'] + // Inherited serializer already has parent filters applied. + @RestMethod(name="GET", path="/test2", pojoSwaps=F3Swap.class) + public Object[] test2() { + return new Object[]{new Foo1(), new Foo2(), new Foo3()}; + } + + // Should show ['F1Swap','F2Swap','F3Swap'] + @RestMethod(name="GET", path="/test3", pojoSwaps=F3Swap.class, serializersInherit=TRANSFORMS) + public Object[] test3() { + return new Object[]{new Foo1(), new Foo2(), new Foo3()}; + } + + // Should show ['Foo1','Foo2','F3Swap'] + // Overriding serializer does not have parent filters applied. + @RestMethod(name="GET", path="/test4", serializers=JsonSerializer.Simple.class, pojoSwaps=F3Swap.class) + public Object[] test4() { + return new Object[]{new Foo1(), new Foo2(), new Foo3()}; + } + + // Should show ['F1Swap','F2Swap','F3Swap'] + // Overriding serializer does have parent filters applied. + @RestMethod(name="GET", path="/test5", serializers=JsonSerializer.Simple.class, pojoSwaps=F3Swap.class, serializersInherit=TRANSFORMS) + public Object[] test5() { + return new Object[]{new Foo1(), new Foo2(), new Foo3()}; + } + } + + //==================================================================================================== + // Test properties inheritance. + //==================================================================================================== + @RestResource(path="/testInheritanceProperties", serializers=JsonSerializer.Simple.class) + public static class TestProperties extends Sub { + private static final long serialVersionUID = 1L; + + // Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4'} + @RestMethod(name="GET", path="/test1") + public ObjectMap test1(@Properties ObjectMap properties) { + return transform(properties); + } + + // Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'} when override is false. + // Should show {p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'} when override is true. + @RestMethod(name="GET", path="/test2", + properties={@Property(name="p4",value="v4a"), @Property(name="p5", value="v5")}) + public ObjectMap test2(@Properties ObjectMap properties, @HasQuery("override") boolean override) { + if (override) { + properties.put("p1", "x"); + properties.put("p2", "x"); + properties.put("p3", "x"); + properties.put("p4", "x"); + properties.put("p5", "x"); + } + return transform(properties); + } + + private ObjectMap transform(ObjectMap properties) { + ObjectMap m = new ObjectMap(); + for (Map.Entry<String,Object> e : properties.entrySet()) { + if (e.getKey().startsWith("p")) + m.put(e.getKey(), e.getValue()); + } + return m; + } + } + + public static class DummyParser extends ReaderParser { + @Override /* Parser */ + protected <T> T doParse(ParserSession session, ClassMeta<T> type) throws Exception { + return null; + } + } + + public static class DummySerializer extends WriterSerializer { + @Override /* Serializer */ + protected void doSerialize(SerializerSession session, Object o) throws Exception { + session.getWriter().write(o.toString()); + } + } + + @Consumes("text/p1") + public static class P1 extends DummyParser{} + + @Consumes("text/p2") + public static class P2 extends DummyParser{} + + @Consumes("text/p3") + public static class P3 extends DummyParser{} + + @Consumes("text/p4") + public static class P4 extends DummyParser{} + + @Consumes("text/p5") + public static class P5 extends DummyParser{} + + @Produces("text/s1") + public static class S1 extends DummySerializer{} + + @Produces("text/s2") + public static class S2 extends DummySerializer{} + + @Produces("text/s3") + public static class S3 extends DummySerializer{} + + @Produces("text/s4") + public static class S4 extends DummySerializer{} + + @Produces("text/s5") + public static class S5 extends DummySerializer{} + + public static class E1 extends IdentityEncoder { + @Override public String[] getCodings() { + return new String[]{"e1"}; + } + } + + public static class E2 extends IdentityEncoder { + @Override public String[] getCodings() { + return new String[]{"e2"}; + } + } + + public static class E3 extends IdentityEncoder { + @Override public String[] getCodings() { + return new String[]{"e3"}; + } + } + + public static class E4 extends IdentityEncoder { + @Override public String[] getCodings() { + return new String[]{"e4"}; + } + } + + public static class Foo1 {@Override public String toString(){return "Foo1";}} + public static class Foo2 {@Override public String toString(){return "Foo2";}} + public static class Foo3 {@Override public String toString(){return "Foo3";}} + + public static class F1Swap extends StringSwap<Foo1> { + @Override /* PojoSwap */ + public String swap(BeanSession session, Foo1 o) throws SerializeException { + return "F1"; + } + } + + public static class F2Swap extends StringSwap<Foo2> { + @Override /* PojoSwap */ + public String swap(BeanSession session, Foo2 o) throws SerializeException { + return "F2"; + } + } + + public static class F3Swap extends StringSwap<Foo3> { + @Override /* PojoSwap */ + public String swap(BeanSession session, Foo3 o) throws SerializeException { + return "F3"; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/LargePojo.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/LargePojo.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/LargePojo.java new file mode 100644 index 0000000..3ff1c5a --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/LargePojo.java @@ -0,0 +1,45 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import java.util.*; + +/** + * A large POJO object. + */ +@SuppressWarnings("serial") +public class LargePojo { + public A1Map a1Map; + public A1List a1List; + public A1[] a1Array; + + public static LargePojo create() { + LargePojo a = new LargePojo(); + a.a1Map = new A1Map(); + a.a1List = new A1List(); + for (int i = 0; i < 20000; i++) { + a.a1Map.put(String.valueOf(i), new A1()); + a.a1List.add(new A1()); + } + a.a1Array = a.a1List.toArray(new A1[0]); + return a; + } + + public static class A1 { + public String f1 = "a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789"; + } + + public static class A1Map extends LinkedHashMap<String,A1> {} + + public static class A1List extends LinkedList<A1> {} +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/LargePojosResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/LargePojosResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/LargePojosResource.java new file mode 100644 index 0000000..26932b2 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/LargePojosResource.java @@ -0,0 +1,39 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.rest.jena.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testLargePojos" +) +public class LargePojosResource extends RestServletJenaDefault { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // Test how long it takes to serialize/parse various content types. + //==================================================================================================== + @RestMethod(name="GET", path="/") + public LargePojo testGet() { + return LargePojo.create(); + } + + @RestMethod(name="PUT", path="/") + public String testPut(@Body LargePojo in) { + return "ok"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/MessagesResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/MessagesResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/MessagesResource.java new file mode 100644 index 0000000..14c4b28 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/MessagesResource.java @@ -0,0 +1,62 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import java.util.*; + +import org.apache.juneau.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.transform.*; + +/** + * JUnit automated testcase resource. + * Validates that resource bundles can be defined on both parent and child classes. + */ +@RestResource( + path="/testMessages", + messages="MessagesResource", + pojoSwaps={ + MessagesResource.ResourceBundleSwap.class + } +) +public class MessagesResource extends RestServletDefault { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // Return contents of resource bundle. + //==================================================================================================== + @RestMethod(name="GET", path="/test") + public Object test(@Messages ResourceBundle nls) { + return nls; + } + + + @SuppressWarnings("serial") + @RestResource( + path="/testMessages2", + messages="Messages2Resource" + ) + public static class Messages2Resource extends MessagesResource {} + + public static class ResourceBundleSwap extends MapSwap<ResourceBundle> { + @Override /* Transform */ + public ObjectMap swap(BeanSession session, ResourceBundle o) throws SerializeException { + ObjectMap m = new ObjectMap(); + for (String k : o.keySet()) + m.put(k, o.getString(k)); + return m; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NlsPropertyResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NlsPropertyResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NlsPropertyResource.java new file mode 100644 index 0000000..a53055b --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NlsPropertyResource.java @@ -0,0 +1,61 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import org.apache.juneau.annotation.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.serializer.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testNlsProperty", + serializers={NlsPropertyResource.TestSerializer.class}, + properties={ + @Property(name="TestProperty",value="$L{key1}") + }, + messages="NlsPropertyResource" +) +public class NlsPropertyResource extends RestServlet { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // Test getting an NLS property defined on a class. + //==================================================================================================== + @RestMethod(name="GET", path="/testInheritedFromClass") + public String testInheritedFromClass() { + return null; + } + + //==================================================================================================== + // Test getting an NLS property defined on a method. + //==================================================================================================== + @RestMethod(name="GET", path="/testInheritedFromMethod", + properties={ + @Property(name="TestProperty",value="$L{key2}") + } + ) + public String testInheritedFromMethod() { + return null; + } + + @Produces("text/plain") + public static class TestSerializer extends WriterSerializer { + @Override /* Serializer */ + protected void doSerialize(SerializerSession session, Object o) throws Exception { + session.getWriter().write(session.getProperties().getString("TestProperty")); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NlsResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NlsResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NlsResource.java new file mode 100644 index 0000000..d9ce1e7 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NlsResource.java @@ -0,0 +1,193 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.utils.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testNls", + children={ + NlsResource.Test1.class, + NlsResource.Test2.class, + NlsResource.Test3.class, + NlsResource.Test4.class, + NlsResource.Test5.class, + NlsResource.Test6.class + } +) +@SuppressWarnings({"serial"}) +public class NlsResource extends RestServletGroupDefault { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // test1 - Pull labels from annotations only. + //==================================================================================================== + @RestResource( + path="/test1", + messages="NlsResource", + title="Test1.a", + description="Test1.b" + ) + public static class Test1 extends RestServletDefault { + + @RestMethod( + name="POST", path="/{a}", + description="Test1.c", + parameters={ + @Parameter(in="path", name="a", description="Test1.d"), + @Parameter(in="query", name="b", description="Test1.e"), + @Parameter(in="body", description="Test1.f"), + @Parameter(in="header", name="D", description="Test1.g"), + @Parameter(in="path", name="a2", description="Test1.h"), + @Parameter(in="query", name="b2", description="Test1.i"), + @Parameter(in="header", name="D2", description="Test1.j"), + }, + responses={ + @Response(200), + @Response(value=201, + description="Test1.l", + headers={ + @Parameter(in="foo", name="bar", description="Test1.m"), + } + ) + } + ) + public String test1(@Path("a") String a, @Query("b") String b, @Body String c, @Header("D") String d, + @Path("e") String e, @Query("f") String f, @Header("g") String g) { + return null; + } + } + + //==================================================================================================== + // test2 - Pull labels from resource bundles only - simple keys. + //==================================================================================================== + @RestResource( + path="/test2", + messages="NlsResource" + ) + public static class Test2 extends RestServletDefault { + + @RestMethod( + name="POST", path="/{a}" + ) + public String test2(@Path("a") String a, @Query("b") String b, @Body String c, @Header("D") String d, + @Path("e") String e, @Query("f") String f, @Header("g") String g) { + return null; + } + } + + //==================================================================================================== + // test3 - Pull labels from resource bundles only - keys with class names. + //==================================================================================================== + @RestResource( + path="/test3", + messages="NlsResource" + ) + public static class Test3 extends RestServletDefault { + + @RestMethod( + name="POST", path="/{a}" + ) + public String test3(@Path("a") String a, @Query("b") String b, @Body String c, @Header("D") String d, + @Path("e") String e, @Query("f") String f, @Header("g") String g) { + return null; + } + + @RestMethod( + name="GET", path="/" + ) + public Object test3a(@Messages MessageBundle mb) { + return mb; + } + } + + //==================================================================================================== + // test4 - Pull labels from resource bundles only. Values have localized variables to resolve. + //==================================================================================================== + @RestResource( + path="/test4", + messages="NlsResource" + ) + public static class Test4 extends RestServletDefault { + + @RestMethod( + name="POST", path="/{a}" + ) + public String test4(@Path("a") String a, @Query("b") String b, @Body String c, @Header("D") String d, + @Path("e") String e, @Query("f") String f, @Header("g") String g) { + return null; + } + } + + //==================================================================================================== + // test5 - Pull labels from resource bundles only. Values have request variables to resolve. + //==================================================================================================== + @RestResource( + path="/test5", + messages="NlsResource" + ) + public static class Test5 extends RestServletDefault { + + @RestMethod( + name="POST", path="/{a}" + ) + public String test5(@Path("a") String a, @Query("b") String b, @Body String c, @Header("D") String d, + @Path("e") String e, @Query("f") String f, @Header("g") String g) { + return null; + } + } + + //==================================================================================================== + // test6 - Pull labels from annotations only, but annotations contain variables. + //==================================================================================================== + @RestResource( + path="/test6", + messages="NlsResource", + title="$L{foo}", + description="$L{foo}" + ) + public static class Test6 extends RestServletDefault { + + @RestMethod( + name="POST", path="/{a}", + description="$L{foo}", + parameters={ + @Parameter(in="path", name="a", description="$L{foo}"), + @Parameter(in="query", name="b", description="$L{foo}"), + @Parameter(in="body", description="$L{foo}"), + @Parameter(in="header", name="D", description="$L{foo}"), + @Parameter(in="path", name="a2", description="$L{foo}"), + @Parameter(in="query", name="b2", description="$L{foo}"), + @Parameter(in="header", name="D2", description="$L{foo}") + }, + responses={ + @Response(200), + @Response(value=201, + description="$L{foo}", + headers={ + @Parameter(in="foo", name="bar", description="$L{foo}"), + } + ) + } + ) + public String test6(@Path("a") String a, @Query("b") String b, @Body String c, @Header("D") String d, + @Path("e") String e, @Query("f") String f, @Header("g") String g) { + return null; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NoParserInputResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NoParserInputResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NoParserInputResource.java new file mode 100644 index 0000000..51a578e --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/NoParserInputResource.java @@ -0,0 +1,56 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import java.io.*; + +import org.apache.juneau.internal.*; +import org.apache.juneau.plaintext.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testNoParserInput", + serializers=PlainTextSerializer.class +) +public class NoParserInputResource extends RestServlet { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // @Body annotated InputStream. + //==================================================================================================== + @RestMethod(name="PUT", path="/testInputStream") + public String testInputStream(@Body InputStream in) throws Exception { + return IOUtils.read(in); + } + + //==================================================================================================== + // @Body annotated Reader. + //==================================================================================================== + @RestMethod(name="PUT", path="/testReader") + public String testReader(@Body Reader in) throws Exception { + return IOUtils.read(in); + } + + //==================================================================================================== + // @Body annotated PushbackReader. + // This should always fail since the servlet reader is not a pushback reader. + //==================================================================================================== + @RestMethod(name="PUT", path="/testPushbackReader") + public String testPushbackReader(@Body PushbackReader in) throws Exception { + return IOUtils.read(in); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPostCallResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPostCallResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPostCallResource.java new file mode 100644 index 0000000..01add72 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPostCallResource.java @@ -0,0 +1,94 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.serializer.*; + +/** + * JUnit automated testcase resource. + * Validates that headers + */ +@RestResource( + path="/testOnPostCall", + serializers=OnPostCallResource.TestSerializer.class, + properties={ + @Property(name="p1",value="sp1"), // Unchanged servlet-level property. + @Property(name="p2",value="sp2"), // Servlet-level property overridden by onPostCall. + @Property(name="p3",value="sp3"), // Servlet-level property overridded by method. + @Property(name="p4",value="sp4") // Servlet-level property overridden by method then onPostCall. + } +) +public class OnPostCallResource extends RestServlet { + private static final long serialVersionUID = 1L; + + @Produces("text/s1,text/s2,text/s3") + public static class TestSerializer extends WriterSerializer { + @Override /* Serializer */ + protected void doSerialize(SerializerSession session, Object o) throws Exception { + ObjectMap p = session.getProperties(); + session.getWriter().write("p1="+p.get("p1")+",p2="+p.get("p2")+",p3="+p.get("p3")+",p4="+p.get("p4")+",p5="+p.get("p5")+",contentType="+session.getProperties().getString("mediaType")); + } + @Override /* Serializer */ + public ObjectMap getResponseHeaders(ObjectMap properties) { + if (properties.containsKey("Override-Content-Type")) + return new ObjectMap().append("Content-Type", properties.get("Override-Content-Type")); + return null; + } + } + + @Override /* RestServlet */ + protected void onPostCall(RestRequest req, RestResponse res) { + ObjectMap properties = req.getProperties(); + properties.put("p2", "xp2"); + properties.put("p4", "xp4"); + properties.put("p5", "xp5"); // New property + String overrideAccept = req.getHeader("Override-Accept"); + if (overrideAccept != null) + req.setHeader("Accept", overrideAccept); + String overrideContentType = req.getHeader("Override-Content-Type"); + if (overrideContentType != null) + properties.put("Override-Content-Type", overrideContentType); + } + + + //==================================================================================================== + // Test1 - Properties overridden via properties annotation. + //==================================================================================================== + @RestMethod(name="PUT", path="/testPropertiesOverridenByAnnotation", + properties={ + @Property(name="p3",value="mp3"), + @Property(name="p4",value="mp4") + }, + defaultRequestHeaders="Accept: text/s2" + ) + public String testPropertiesOverridenByAnnotation() { + return ""; + } + + //==================================================================================================== + // Test2 - Properties overridden programmatically. + //==================================================================================================== + @RestMethod(name="PUT", path="/testPropertiesOverriddenProgramatically") + public String testPropertiesOverriddenProgramatically(RestRequest req, @Properties ObjectMap properties) throws Exception { + properties.put("p3", "pp3"); + properties.put("p4", "pp4"); + String accept = req.getHeader("Accept"); + if (accept == null || accept.isEmpty()) + req.setHeader("Accept", "text/s2"); + return ""; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPreCallResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPreCallResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPreCallResource.java new file mode 100644 index 0000000..e2661cb --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OnPreCallResource.java @@ -0,0 +1,85 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import org.apache.juneau.*; +import org.apache.juneau.annotation.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.plaintext.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * JUnit automated testcase resource. + * Validates that headers + */ +@RestResource( + path="/testOnPreCall", + parsers=OnPreCallResource.TestParserA.class, + serializers=PlainTextSerializer.class, + properties={ + @Property(name="p1",value="sp1"), // Unchanged servlet-level property. + @Property(name="p2",value="sp2"), // Servlet-level property overridden by onPreCall. + @Property(name="p3",value="sp3"), // Servlet-level property overridded by method. + @Property(name="p4",value="sp4") // Servlet-level property overridden by method then onPreCall. + } +) +public class OnPreCallResource extends RestServlet { + private static final long serialVersionUID = 1L; + + @Consumes("text/a1,text/a2,text/a3") + public static class TestParserA extends ReaderParser { + @SuppressWarnings("unchecked") + @Override /* Parser */ + protected <T> T doParse(ParserSession session, ClassMeta<T> type) throws Exception { + ObjectMap p = session.getProperties(); + String matchingContentType = session.getProperties().getString("mediaType"); + return (T)("p1="+p.get("p1")+",p2="+p.get("p2")+",p3="+p.get("p3")+",p4="+p.get("p4")+",p5="+p.get("p5")+",contentType="+matchingContentType); + } + } + + @Override /* RestServlet */ + protected void onPreCall(RestRequest req) { + ObjectMap properties = req.getProperties(); + properties.put("p2", "xp2"); + properties.put("p4", "xp4"); + properties.put("p5", "xp5"); // New property + String overrideContentType = req.getHeader("Override-Content-Type"); + if (overrideContentType != null) + req.setHeader("Content-Type", overrideContentType); + } + + + //==================================================================================================== + // Properties overridden via properties annotation. + //==================================================================================================== + @RestMethod(name="PUT", path="/testPropertiesOverriddenByAnnotation", + properties={ + @Property(name="p3",value="mp3"), + @Property(name="p4",value="mp4") + } + ) + public String testPropertiesOverriddenByAnnotation(@Body String in) { + return in; + } + + //==================================================================================================== + // Properties overridden programmatically. + //==================================================================================================== + @RestMethod(name="PUT", path="/testPropertiesOverriddenProgrammatically") + public String testPropertiesOverriddenProgrammatically(RestRequest req, @Properties ObjectMap properties) throws Exception { + properties.put("p3", "pp3"); + properties.put("p4", "pp4"); + return req.getBody(String.class); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OptionsWithoutNlsResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OptionsWithoutNlsResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OptionsWithoutNlsResource.java new file mode 100644 index 0000000..f31a1b1 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OptionsWithoutNlsResource.java @@ -0,0 +1,44 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import org.apache.juneau.dto.swagger.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testOptionsWithoutNls" +) +public class OptionsWithoutNlsResource extends RestServletDefault { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // Should get to the options page without errors + //==================================================================================================== + @RestMethod(name="OPTIONS", path="/testOptions/*") + public Swagger testOptions(RestRequest req) { + return req.getSwagger(); + } + + //==================================================================================================== + // Missing resource bundle should cause {!!x} string. + //==================================================================================================== + @RestMethod(name="GET", path="/testMissingResourceBundle") + public String test(RestRequest req) { + return req.getMessage("bad", 1, 2, 3); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/91a388d0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OverlappingMethodsResource.java ---------------------------------------------------------------------- diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OverlappingMethodsResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OverlappingMethodsResource.java new file mode 100644 index 0000000..6bfef72 --- /dev/null +++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/OverlappingMethodsResource.java @@ -0,0 +1,146 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import org.apache.juneau.plaintext.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testOverlappingMethods", + serializers=PlainTextSerializer.class +) +public class OverlappingMethodsResource extends RestServletDefault { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // Overlapping guards + //==================================================================================================== + @RestMethod(name="GET", path="/testOverlappingGuards1", guards=Test1Guard.class) + public String testOverlappingGuards1() { + return "test1_doGet"; + } + + //==================================================================================================== + // Overlapping guards + //==================================================================================================== + @RestMethod(name="GET", path="/testOverlappingGuards2", guards={Test1Guard.class, Test2Guard.class}) + public String testOverlappingGuards2() { + return "test2_doGet"; + } + + public static class Test1Guard extends RestGuard { + @Override /* RestGuard */ + public boolean isRequestAllowed(RestRequest req) { + return req.getQueryParameter("t1","").equals("1"); + } + } + + public static class Test2Guard extends RestGuard { + @Override /* RestGuard */ + public boolean isRequestAllowed(RestRequest req) { + return req.getQueryParameter("t2","").equals("2"); + } + } + + //==================================================================================================== + // Overlapping matchers + //==================================================================================================== + @RestMethod(name="GET", path="/testOverlappingMatchers1", matchers=Test3aMatcher.class) + public String testOverlappingMatchers1() { + return "test3a"; + } + + @RestMethod(name="GET", path="/testOverlappingMatchers1", matchers=Test3bMatcher.class) + public String test3b_doGet() { + return "test3b"; + } + + @RestMethod(name="GET", path="/testOverlappingMatchers1") + public String test3c_doGet() { + return "test3c"; + } + + public static class Test3aMatcher extends RestMatcher { + @Override /* RestMatcher */ + public boolean matches(RestRequest req) { + return req.getQueryParameter("t1","").equals("1"); + } + } + + public static class Test3bMatcher extends RestMatcher { + @Override /* RestMatcher */ + public boolean matches(RestRequest req) { + return req.getQueryParameter("t2","").equals("2"); + } + } + + //==================================================================================================== + // Overlapping matchers + //==================================================================================================== + @RestMethod(name="GET", path="/testOverlappingMatchers2") + public String test4a_doGet() { + return "test4a"; + } + + @RestMethod(name="GET", path="/testOverlappingMatchers2", matchers={Test3aMatcher.class, Test3bMatcher.class}) + public String test4b_doGet() { + return "test4b"; + } + + //==================================================================================================== + // Overlapping URL patterns + //==================================================================================================== + @RestMethod(name="GET", path="/testOverlappingUrlPatterns") + public String testOverlappingUrlPatterns1() { + return "test5a"; + } + + @RestMethod(name="GET", path="/testOverlappingUrlPatterns/*") + public String testOverlappingUrlPatterns2() { + return "test5b"; + } + + @RestMethod(name="GET", path="/testOverlappingUrlPatterns/foo") + public String testOverlappingUrlPatterns3() { + return "test5c"; + } + + @RestMethod(name="GET", path="/testOverlappingUrlPatterns/foo/*") + public String testOverlappingUrlPatterns4() { + return "test5d"; + } + + @RestMethod(name="GET", path="/testOverlappingUrlPatterns/{id}") + public String testOverlappingUrlPatterns5() { + return "test5e"; + } + + @RestMethod(name="GET", path="/testOverlappingUrlPatterns/{id}/*") + public String testOverlappingUrlPatterns6() { + return "test5f"; + } + + @RestMethod(name="GET", path="/testOverlappingUrlPatterns/{id}/foo") + public String testOverlappingUrlPatterns7() { + return "test5g"; + } + + @RestMethod(name="GET", path="/testOverlappingUrlPatterns/{id}/foo/*") + public String testOverlappingUrlPatterns8() { + return "test5h"; + } +}