You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2008/02/22 21:59:48 UTC
svn commit: r630317 - in /incubator/cxf/trunk/rt/frontend/jaxrs/src:
main/java/org/apache/cxf/jaxrs/provider/
test/java/org/apache/cxf/jaxrs/model/
test/java/org/apache/cxf/jaxrs/provider/
Author: dkulp
Date: Fri Feb 22 12:59:45 2008
New Revision: 630317
URL: http://svn.apache.org/viewvc?rev=630317&view=rev
Log:
Forgot to svn add some files
Added:
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java (with props)
incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java (with props)
incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java (with props)
Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java?rev=630317&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java (added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java Fri Feb 22 12:59:45 2008
@@ -0,0 +1,92 @@
+/**
+ * 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.cxf.jaxrs.provider;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.ext.HeaderProvider;
+
+public class MediaTypeHeaderProvider implements HeaderProvider<MediaType> {
+
+ public MediaType fromString(String mType) throws ParseException {
+
+ if (mType.equals(MediaType.MEDIA_TYPE_WILDCARD) || mType.startsWith("*;")) {
+ return new MediaType("*", "*");
+ }
+
+ int i = mType.indexOf('/');
+ if (i == -1) {
+ throw new ParseException("Media type separator is missing", 0);
+ }
+
+ int paramsStart = mType.indexOf(';', i + 1);
+ int end = paramsStart == -1 ? mType.length() : paramsStart;
+
+ String type = mType.substring(0, i);
+ String subtype = mType.substring(i + 1, end);
+
+ Map<String, String> parameters = null;
+ if (paramsStart != -1) {
+ // Using Pattern.compile might be marginally faster ?
+ // though it's rare when more than one parameter is provided
+ parameters = new LinkedHashMap<String, String>();
+ StringTokenizer st =
+ new StringTokenizer(mType.substring(paramsStart + 1), ";");
+ while (st.hasMoreTokens()) {
+ String token = st.nextToken();
+ int equalSign = token.indexOf('=');
+ if (equalSign == -1) {
+ throw new ParseException("Wrong media type parameter, seperator is missing", 0);
+ }
+ parameters.put(token.substring(0, equalSign).trim().toLowerCase(),
+ token.substring(equalSign + 1).trim().toLowerCase());
+ }
+
+ }
+
+ return new MediaType(type.trim().toLowerCase(),
+ subtype.trim().toLowerCase(),
+ parameters);
+ }
+
+ public boolean supports(Class<?> clazz) {
+ return MediaType.class.isAssignableFrom(clazz);
+ }
+
+ public String toString(MediaType type) {
+ StringBuilder sb = new StringBuilder();
+ sb.append(type.getType()).append('/').append(type.getSubtype());
+
+ for (Iterator<Map.Entry<String, String>> iter = type.getParameters().entrySet().iterator();
+ iter.hasNext();) {
+ Map.Entry<String, String> entry = iter.next();
+ sb.append(';').append(entry.getKey()).append('=').append(entry.getValue());
+ }
+
+ return sb.toString();
+ }
+
+
+}
Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java?rev=630317&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java (added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java Fri Feb 22 12:59:45 2008
@@ -0,0 +1,88 @@
+/**
+ * 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.cxf.jaxrs.model;
+
+import java.util.List;
+
+import javax.ws.rs.ConsumeMime;
+import javax.ws.rs.ProduceMime;
+import javax.ws.rs.core.MediaType;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class OperationResourceInfoTest extends Assert {
+
+ @ProduceMime("text/xml")
+ @ConsumeMime("application/xml")
+ static class TestClass {
+ @ProduceMime("text/plain")
+ public void doIt() {
+ // empty
+ };
+ @ConsumeMime("application/atom+xml")
+ public void doThat() {
+ // empty
+ };
+
+ }
+
+ @Test
+ public void testConsumeTypes() throws Exception {
+ OperationResourceInfo ori1 = new OperationResourceInfo(
+ TestClass.class.getMethod("doIt", new Class[]{}),
+ new ClassResourceInfo(TestClass.class));
+
+ List<MediaType> ctypes = ori1.getConsumeTypes();
+ assertEquals("Single media type expected", 1, ctypes.size());
+ assertEquals("Class resource consume type should be used",
+ "application/xml", ctypes.get(0).toString());
+
+ OperationResourceInfo ori2 = new OperationResourceInfo(
+ TestClass.class.getMethod("doThat", new Class[]{}),
+ new ClassResourceInfo(TestClass.class));
+ ctypes = ori2.getConsumeTypes();
+ assertEquals("Single media type expected", 1, ctypes.size());
+ assertEquals("Method consume type should be used",
+ "application/atom+xml", ctypes.get(0).toString());
+ }
+
+ @Test
+ public void testProduceTypes() throws Exception {
+
+ OperationResourceInfo ori1 = new OperationResourceInfo(
+ TestClass.class.getMethod("doIt", new Class[]{}),
+ new ClassResourceInfo(TestClass.class));
+
+ List<MediaType> ctypes = ori1.getProduceTypes();
+ assertEquals("Single media type expected", 1, ctypes.size());
+ assertEquals("Method produce type should be used",
+ "text/plain", ctypes.get(0).toString());
+
+ OperationResourceInfo ori2 = new OperationResourceInfo(
+ TestClass.class.getMethod("doThat", new Class[]{}),
+ new ClassResourceInfo(TestClass.class));
+ ctypes = ori2.getProduceTypes();
+ assertEquals("Single media type expected", 1, ctypes.size());
+ assertEquals("Class resource produce type should be used",
+ "text/xml", ctypes.get(0).toString());
+ }
+
+}
Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java?rev=630317&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java (added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java Fri Feb 22 12:59:45 2008
@@ -0,0 +1,125 @@
+/**
+ * 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.cxf.jaxrs.provider;
+
+import java.text.ParseException;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import javax.ws.rs.core.EntityTag;
+import javax.ws.rs.core.MediaType;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MediaTypeHeaderProviderTest extends Assert {
+
+ @Test
+ public void testSimpleType() {
+ MediaType m = MediaType.parse("text/html");
+ assertEquals("Media type was not parsed correctly",
+ m, new MediaType("text", "html"));
+ assertEquals("Media type was not parsed correctly",
+ MediaType.parse("text/html "), new MediaType("text", "html"));
+ }
+
+ @Test
+ public void testShortWildcard() {
+ MediaType m = MediaType.parse("*");
+ assertEquals("Media type was not parsed correctly",
+ m, new MediaType("*", "*"));
+ }
+
+ @Test
+ public void testShortWildcardWithParameters() {
+ MediaType m = MediaType.parse("*;q=0.2");
+ assertEquals("Media type was not parsed correctly",
+ m, new MediaType("*", "*"));
+ }
+
+ @Test
+ public void testBadType() {
+ try {
+ new MediaTypeHeaderProvider().fromString("texthtml");
+ fail("Parse exception must've been thrown");
+ } catch (ParseException pe) {
+ // expected
+ }
+
+ }
+
+ @Test
+ public void testBadParameter() {
+ try {
+ new MediaTypeHeaderProvider().fromString("text/html;*");
+ fail("Parse exception must've been thrown");
+ } catch (ParseException pe) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testTypeWithParameters() {
+ MediaType m = MediaType.parse("text/html;q=1234;b=4321");
+
+ Map<String, String> params = new HashMap<String, String>();
+ params.put("q", "1234");
+ params.put("b", "4321");
+
+ MediaType expected = new MediaType("text", "html", params);
+
+ assertEquals("Media type was not parsed correctly", expected, m);
+ }
+
+ @Test
+ public void testSupports() {
+ MediaTypeHeaderProvider provider =
+ new MediaTypeHeaderProvider();
+
+ assertTrue(provider.supports(MediaType.class));
+ // I think we should have a single default header provider
+ assertFalse(provider.supports(EntityTag.class));
+ }
+
+ @Test
+ public void testSimpleToString() {
+ MediaTypeHeaderProvider provider =
+ new MediaTypeHeaderProvider();
+
+ assertEquals("simple media type is not serialized", "text/plain",
+ provider.toString(new MediaType("text", "plain")));
+ }
+
+ @Test
+ public void testComplexToString() {
+ MediaTypeHeaderProvider provider =
+ new MediaTypeHeaderProvider();
+
+ Map<String, String> params = new LinkedHashMap<String, String>();
+ params.put("foo", "bar");
+ params.put("q", "0.2");
+
+ assertEquals("complex media type is not serialized", "text/plain;foo=bar;q=0.2",
+ provider.toString(new MediaType("text", "plain", params)));
+
+ }
+
+}
Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Re: svn commit: r630317 - in /incubator/cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ test/java/org/apache/cxf/jaxrs/model/ test/java/org/apache/cxf/jaxrs/provider/
Posted by Daniel Kulp <dk...@apache.org>.
On Friday 22 February 2008, Glen Mazza wrote:
> Am Freitag, den 22.02.2008, 20:59 +0000 schrieb dkulp@apache.org:
> > Author: dkulp
> > Date: Fri Feb 22 12:59:45 2008
> > New Revision: 630317
> >
> > URL: http://svn.apache.org/viewvc?rev=630317&view=rev
> > Log:
> > Forgot to svn add some files
> >
> > Added:
> >
> > incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/j
> >axrs/provider/MediaTypeHeaderProvider.java (with props)
> > incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/j
> >axrs/model/OperationResourceInfoTest.java (with props)
> > incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/j
> >axrs/provider/MediaTypeHeaderProviderTest.java (with props)
>
> BTW, what does "(with props)" mean?
>
> Glen
If you scroll down the commit message furthur, you see things like:
Propchange:
incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Basically, every object in svn can have properties attached to them. See
the commands "svn propedit", "svn propset", etc... For the most part,
I like to have the svn:eol-style = native set on everything as it works
better on Windows/Unix and stuff. You'll see props like mime types
(useful when browsing the svn repo with your web browser), keywords,
etc... The important one has normally been the eol-style. Lots of
editors have issues if that isn't set. (example: notepad on windows
will display entire files on one line if the file came from a Unix user.
Some versions of emacs on linux/unix will display ^M things on every
line for files coming from windows folks.)
Actually, I have a script that goes through and makes sure all the
properties are set. I haven't run it in quite a while. Probably
should. Thanks for reminding me. Expect a gigantic commit to fix
them all. :-)
--
J. Daniel Kulp
Principal Engineer, IONA
dkulp@apache.org
http://www.dankulp.com/blog
Re: svn commit: r630317 - in
/incubator/cxf/trunk/rt/frontend/jaxrs/src:
main/java/org/apache/cxf/jaxrs/provider/ test/java/org/apache/cxf/jaxrs/model/
test/java/org/apache/cxf/jaxrs/provider/
Posted by Glen Mazza <gl...@verizon.net>.
Am Freitag, den 22.02.2008, 20:59 +0000 schrieb dkulp@apache.org:
> Author: dkulp
> Date: Fri Feb 22 12:59:45 2008
> New Revision: 630317
>
> URL: http://svn.apache.org/viewvc?rev=630317&view=rev
> Log:
> Forgot to svn add some files
>
> Added:
> incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java (with props)
> incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java (with props)
> incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java (with props)
>
BTW, what does "(with props)" mean?
Glen