PageRenderTime 26ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/org.mwc.asset.comms/docs/restlet_src/org.restlet.test/org/restlet/test/jaxrs/services/resources/ProviderTestService.java

https://bitbucket.org/haris_peco/debrief
Java | 379 lines | 292 code | 41 blank | 46 comment | 14 complexity | a9a8b9a8842ce2f87a655c6f89bedabb MD5 | raw file
  1. /**
  2. * Copyright 2005-2010 Noelios Technologies.
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL 1.0 (the
  6. * "Licenses"). You can select the license that you prefer but you may not use
  7. * this file except in compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the LGPL 3.0 license at
  10. * http://www.opensource.org/licenses/lgpl-3.0.html
  11. *
  12. * You can obtain a copy of the LGPL 2.1 license at
  13. * http://www.opensource.org/licenses/lgpl-2.1.php
  14. *
  15. * You can obtain a copy of the CDDL 1.0 license at
  16. * http://www.opensource.org/licenses/cddl1.php
  17. *
  18. * You can obtain a copy of the EPL 1.0 license at
  19. * http://www.opensource.org/licenses/eclipse-1.0.php
  20. *
  21. * See the Licenses for the specific language governing permissions and
  22. * limitations under the Licenses.
  23. *
  24. * Alternatively, you can obtain a royalty free commercial license with less
  25. * limitations, transferable or non-transferable, directly at
  26. * http://www.noelios.com/products/restlet-engine
  27. *
  28. * Restlet is a registered trademark of Noelios Technologies.
  29. */
  30. package org.restlet.test.jaxrs.services.resources;
  31. import java.io.BufferedReader;
  32. import java.io.ByteArrayInputStream;
  33. import java.io.File;
  34. import java.io.FileInputStream;
  35. import java.io.IOException;
  36. import java.io.InputStream;
  37. import java.io.Reader;
  38. import java.io.StringReader;
  39. import javax.mail.BodyPart;
  40. import javax.mail.MessagingException;
  41. import javax.mail.Multipart;
  42. import javax.ws.rs.Consumes;
  43. import javax.ws.rs.GET;
  44. import javax.ws.rs.MatrixParam;
  45. import javax.ws.rs.POST;
  46. import javax.ws.rs.Path;
  47. import javax.ws.rs.Produces;
  48. import javax.ws.rs.QueryParam;
  49. import javax.ws.rs.WebApplicationException;
  50. import javax.ws.rs.core.MultivaluedMap;
  51. import javax.ws.rs.core.Response;
  52. import javax.xml.bind.JAXBElement;
  53. import javax.xml.namespace.QName;
  54. import javax.xml.transform.Source;
  55. import javax.xml.transform.sax.SAXSource;
  56. import javax.xml.transform.stream.StreamSource;
  57. import org.restlet.data.Form;
  58. import org.restlet.engine.io.IoUtils;
  59. import org.restlet.ext.jaxrs.internal.core.MultivaluedMapImpl;
  60. import org.restlet.ext.jaxrs.internal.util.Converter;
  61. import org.restlet.test.jaxrs.services.others.Person;
  62. import org.restlet.test.jaxrs.services.tests.ProviderTest;
  63. import org.restlet.test.jaxrs.util.TestUtils;
  64. import org.xml.sax.InputSource;
  65. /**
  66. * @author Stephan Koops
  67. * @see ProviderTest
  68. * @see JsonTestService
  69. */
  70. @SuppressWarnings("all")
  71. @Path("/providerTest")
  72. public class ProviderTestService {
  73. /**
  74. *
  75. */
  76. public static final String STRING2 = "Rom" + '\u00E4' + "n"; // avoid UTF-8
  77. // bugs
  78. public static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  79. private static final int CS_LAST_CHAR = 126;
  80. /**
  81. * @return
  82. */
  83. public static String createCS() {
  84. final StringBuilder stb = new StringBuilder();
  85. for (char c = 32; c <= CS_LAST_CHAR; c++) {
  86. stb.append(c);
  87. }
  88. return stb.toString();
  89. }
  90. @GET
  91. @Path("BufferedReader")
  92. @Produces("application/octet-stream")
  93. public BufferedReader bufferedReaderGet() {
  94. return new BufferedReader(readerGet(), IoUtils.getBufferSize());
  95. }
  96. @POST
  97. @Path("BufferedReader")
  98. @Produces("text/plain")
  99. public String bufferedReaderPost(BufferedReader reader) throws IOException {
  100. final StringBuilder stb = new StringBuilder();
  101. String line;
  102. while ((line = reader.readLine()) != null) {
  103. stb.append(line);
  104. stb.append('\n');
  105. }
  106. stb.deleteCharAt(stb.length() - 1);
  107. return stb.toString();
  108. }
  109. @GET
  110. @Path("byteArray")
  111. @Produces("application/octet-stream")
  112. public byte[] byteArrayGet() {
  113. return ALPHABET.getBytes();
  114. }
  115. @POST
  116. @Path("byteArray")
  117. @Produces("text/plain")
  118. public String byteArrayPost(byte[] byteArray) {
  119. return new String(byteArray);
  120. }
  121. /**
  122. * Returns a {@link CharSequence}, which class is equals to no default.
  123. *
  124. * @return a {@link CharSequence}, which class is equals to no default.
  125. */
  126. @GET
  127. @Path("CharSequence")
  128. @Produces("text/plain")
  129. public CharSequence charSequenceGet() {
  130. return new CharSequence() {
  131. public char charAt(int index) {
  132. return (char) (index + 32);
  133. }
  134. public int length() {
  135. return CS_LAST_CHAR - 32 + 1;
  136. }
  137. public CharSequence subSequence(int start, int end) {
  138. return toString().subSequence(start, end);
  139. }
  140. @Override
  141. public String toString() {
  142. return createCS();
  143. }
  144. };
  145. }
  146. @POST
  147. @Path("CharSequence")
  148. @Produces("text/plain")
  149. @Consumes("text/plain")
  150. public String charSequencePost(CharSequence form) {
  151. return form.toString();
  152. }
  153. @GET
  154. @Path("file")
  155. @Produces("application/octet-stream")
  156. public File fileGet() {
  157. return new File(this.getClass().getResource("alphabet.txt").getPath());
  158. }
  159. @POST
  160. @Path("file")
  161. @Consumes("application/octet-stream")
  162. @Produces("text/plain")
  163. public String filePost(File file) throws IOException {
  164. final InputStream inputStream = new FileInputStream(file);
  165. return inputStreamPost(inputStream);
  166. }
  167. @GET
  168. @Path("form")
  169. @Produces("application/x-www-form-urlencoded")
  170. public Form formGet() {
  171. final Form form = new Form();
  172. form.add("firstname", "Angela");
  173. form.add("lastname", "Merkel");
  174. return form;
  175. }
  176. @POST
  177. @Path("form")
  178. @Produces("text/plain")
  179. @Consumes("application/x-www-form-urlencoded")
  180. public String formPost(Form form) {
  181. return form.toString();
  182. }
  183. @GET
  184. @Path("InputStream")
  185. @Produces("application/octet-stream")
  186. public InputStream inputStreamGet() {
  187. return new ByteArrayInputStream(ALPHABET.getBytes());
  188. }
  189. @POST
  190. @Path("InputStream")
  191. @Produces("text/plain")
  192. public String inputStreamPost(InputStream inputStream) throws IOException {
  193. final StringBuilder stb = new StringBuilder();
  194. int b;
  195. while ((b = inputStream.read()) >= 0) {
  196. stb.append((char) b);
  197. }
  198. return stb.toString();
  199. }
  200. @GET
  201. @Path("jaxbElement")
  202. @Produces("text/xml")
  203. public JAXBElement<Person> jaxbElementGet() {
  204. return new JAXBElement<Person>(new QName("qName"), Person.class,
  205. jaxbGet());
  206. }
  207. @GET
  208. @Path("jaxb")
  209. @Produces("text/xml")
  210. public Person jaxbGet() {
  211. return new Person("Angela", "Merkel");
  212. }
  213. @POST
  214. @Path("jaxbElement")
  215. @Consumes( { "text/xml", "application/xml" })
  216. @Produces("text/plain")
  217. public String jaxbPost(JAXBElement<Person> person) {
  218. if (person == null) {
  219. throw new WebApplicationException(Response.serverError().entity(
  220. "the JAXBElement is null").build());
  221. }
  222. if (person.getValue() == null) {
  223. return null;
  224. }
  225. return person.getValue().toString();
  226. }
  227. @POST
  228. @Path("jaxbElement/rootElement")
  229. @Consumes( { "text/xml", "application/xml" })
  230. @Produces("text/plain")
  231. public String jaxbPostRootElement(JAXBElement<Person> person) {
  232. if (person == null) {
  233. throw new WebApplicationException(Response.serverError().entity(
  234. "the JAXBElement is null").build());
  235. }
  236. if (person.getValue() == null) {
  237. return null;
  238. }
  239. return person.getName().toString();
  240. }
  241. @POST
  242. @Path("jaxb")
  243. @Consumes( { "text/xml", "application/xml" })
  244. @Produces("text/plain")
  245. public String jaxbPost(Person person) {
  246. return person.toString();
  247. }
  248. @GET
  249. @Path("MultivaluedMap")
  250. @Produces("application/x-www-form-urlencoded")
  251. public MultivaluedMap<String, String> mMapGet() {
  252. final MultivaluedMap<String, String> mmap = new MultivaluedMapImpl<String, String>();
  253. mmap.add("firstname", "Angela");
  254. mmap.add("lastname", "Merkel");
  255. return mmap;
  256. }
  257. @POST
  258. @Path("MultivaluedMap")
  259. @Consumes("application/x-www-form-urlencoded")
  260. @Produces("text/plain")
  261. public String mMapPost(MultivaluedMap<String, String> mmap) {
  262. return Converter.toForm(mmap).toString();
  263. }
  264. @POST
  265. @Path("multipart/form-data")
  266. @Consumes("multipart/form-data")
  267. public Object multipartPost(@QueryParam("attrNo") int attrNo,
  268. Multipart multipart) throws MessagingException, IOException {
  269. final BodyPart bodyPart = multipart.getBodyPart(attrNo);
  270. return bodyPart.getInputStream();
  271. }
  272. @GET
  273. @Path("Reader")
  274. @Produces("application/octet-stream")
  275. public Reader readerGet() {
  276. return new StringReader(ALPHABET);
  277. }
  278. @POST
  279. @Path("Reader")
  280. @Produces("text/plain")
  281. public String readerPost(Reader reader) throws IOException {
  282. final StringBuilder stb = new StringBuilder();
  283. int c;
  284. while ((c = reader.read()) >= 0) {
  285. stb.append((char) c);
  286. }
  287. return stb.toString();
  288. }
  289. @GET
  290. @Path("StringBuilder")
  291. @Produces("text/plain")
  292. public StringBuilder stringBuilderGet() {
  293. return new StringBuilder(ALPHABET);
  294. }
  295. @GET
  296. @Path("String")
  297. @Produces("text/plain")
  298. public String stringGet() {
  299. return ALPHABET;
  300. }
  301. @GET
  302. @Path("String2")
  303. @Produces("text/plain")
  304. public String string2Get() {
  305. return STRING2;
  306. }
  307. @POST
  308. @Path("String")
  309. @Produces("text/plain")
  310. @Consumes("text/plain")
  311. public String stringPost(String entity) {
  312. return entity;
  313. }
  314. @GET
  315. @Path("String/substring")
  316. @Produces("text/plain")
  317. public String subStringGet(@MatrixParam("start") int start,
  318. @MatrixParam("end") int end) {
  319. if (end >= ALPHABET.length()) {
  320. return ALPHABET.substring(start);
  321. }
  322. return ALPHABET.substring(start, end);
  323. }
  324. @GET
  325. @Path("source")
  326. @Produces("text/xml")
  327. public Source xsltGet() {
  328. return new StreamSource(new ByteArrayInputStream("<abc/>".getBytes()));
  329. }
  330. @POST
  331. @Path("source")
  332. @Consumes("text/xml")
  333. @Produces("text/plain")
  334. public byte[] xsltPost(Source source) throws IOException {
  335. final InputSource inputSource = SAXSource.sourceToInputSource(source);
  336. return TestUtils.getByteArray(inputSource.getByteStream());
  337. }
  338. }