-
-
Save tnine/4ff589df2ac25da1df9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright 2012 Apigee Corporation | |
* | |
* Licensed 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.usergrid.rest.filters; | |
import static org.junit.Assert.assertEquals; | |
import static org.usergrid.utils.MapUtils.hashMap; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.util.Map; | |
import javax.ws.rs.core.HttpHeaders; | |
import javax.ws.rs.core.MediaType; | |
import org.apache.http.Header; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpHost; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.ParseException; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.params.BasicHttpParams; | |
import org.apache.http.params.HttpParams; | |
import org.apache.http.util.EntityUtils; | |
import org.codehaus.jackson.JsonNode; | |
import org.junit.Test; | |
import org.usergrid.rest.AbstractRestTest; | |
import org.usergrid.utils.JsonUtils; | |
/** | |
* @author tnine | |
* | |
*/ | |
public class ContentTypeResourceTest extends AbstractRestTest { | |
/** | |
* Creates a simple entity of type game. Does not set the content type. The | |
* type should be set to json to match the body | |
* @throws | |
* @throws Exception | |
*/ | |
@Test | |
public void correctHeaders() throws Exception { | |
Map<String, String> data = hashMap("name", "Solitaire"); | |
String json = JsonUtils.mapToFormattedJsonString(data); | |
DefaultHttpClient client = new DefaultHttpClient(); | |
HttpHost host = new HttpHost(super.getBaseURI().getHost(), super.getBaseURI().getPort()); | |
HttpPost post = new HttpPost("/test-organization/test-app/games"); | |
post.setEntity(new StringEntity(json)); | |
post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ access_token); | |
post.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); | |
post.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); | |
HttpResponse rsp = client.execute(host, post); | |
printResponse(rsp); | |
assertEquals(200, rsp.getStatusLine().getStatusCode()); | |
} | |
/** | |
* Creates a simple entity of type game. Does not set the accept or content type headers to match the json body. The | |
* type should be set to json to match the body | |
* @throws | |
* @throws Exception | |
*/ | |
@Test | |
public void contentTypeJson() throws Exception { | |
Map<String, String> data = hashMap("name", "Solitaire"); | |
String json = JsonUtils.mapToFormattedJsonString(data); | |
DefaultHttpClient client = new DefaultHttpClient(); | |
HttpHost host = new HttpHost(super.getBaseURI().getHost(), super.getBaseURI().getPort()); | |
HttpPost post = new HttpPost("/test-organization/test-app/games"); | |
post.setEntity(new StringEntity(json)); | |
post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ access_token); | |
post.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_FORM_URLENCODED); | |
post.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED); | |
HttpResponse rsp = client.execute(host, post); | |
printResponse(rsp); | |
//should change content type was set | |
assertEquals(200, rsp.getStatusLine().getStatusCode()); | |
} | |
/** | |
* Creates a simple entity of type game. Does not set the content type. The | |
* type should be set to json to match the body | |
* @throws | |
* @throws Exception | |
*/ | |
@Test | |
public void textHtmlContentType() throws Exception { | |
Map<String, String> data = hashMap("name", "Solitaire"); | |
String json = JsonUtils.mapToFormattedJsonString(data); | |
DefaultHttpClient client = new DefaultHttpClient(); | |
HttpHost host = new HttpHost(super.getBaseURI().getHost(), super.getBaseURI().getPort()); | |
HttpPost post = new HttpPost("/test-organization/test-app/games"); | |
post.setEntity(new StringEntity(json)); | |
post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ access_token); | |
post.setHeader(HttpHeaders.ACCEPT, MediaType.TEXT_HTML); | |
post.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML); | |
HttpResponse rsp = client.execute(host, post); | |
printResponse(rsp); | |
//should be an error, no content type was set | |
assertEquals(200, rsp.getStatusLine().getStatusCode()); | |
} | |
/** | |
* Creates a simple entity of type game. Does not set the content type or accept. The | |
* type should be set to json to match the body | |
* @throws | |
* @throws Exception | |
*/ | |
@Test | |
public void missingAccept() throws Exception { | |
Map<String, String> data = hashMap("name", "Solitaire"); | |
String json = JsonUtils.mapToFormattedJsonString(data); | |
DefaultHttpClient client = new DefaultHttpClient(); | |
HttpHost host = new HttpHost(super.getBaseURI().getHost(), super.getBaseURI().getPort()); | |
HttpPost post = new HttpPost("/test-organization/test-app/games"); | |
post.setEntity(new StringEntity(json)); | |
post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ access_token); | |
HttpResponse rsp = client.execute(host, post); | |
printResponse(rsp); | |
assertEquals(400, rsp.getStatusLine().getStatusCode()); | |
} | |
private void printResponse(HttpResponse rsp) throws ParseException, IOException{ | |
HttpEntity entity = rsp.getEntity(); | |
System.out.println("----------------------------------------"); | |
System.out.println(rsp.getStatusLine()); | |
Header[] headers = rsp.getAllHeaders(); | |
for (int i = 0; i < headers.length; i++) { | |
System.out.println(headers[i]); | |
} | |
System.out.println("----------------------------------------"); | |
if (entity != null) { | |
System.out.println(EntityUtils.toString(entity)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright 2012 Apigee Corporation | |
* | |
* Licensed 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.usergrid.rest.filters; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import javax.ws.rs.core.HttpHeaders; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.MultivaluedMap; | |
import scala.util.parsing.json.JSONType; | |
import com.sun.jersey.core.header.InBoundHeaders; | |
import com.sun.jersey.core.header.MediaTypes; | |
import com.sun.jersey.spi.container.ContainerRequest; | |
import com.sun.jersey.spi.container.ContainerRequestFilter; | |
/** | |
* @author tnine | |
* | |
*/ | |
public class DefaultContentTypeFilter implements ContainerRequestFilter { | |
//non whitespace/non white space [possible whitespace][possible comma] ; | |
// private static final Pattern REPLACE_TYPES = Pattern.compile("(\\S+/\\S+\\s*,?)+;?.*"); | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* com.sun.jersey.spi.container.ContainerRequestFilter#filter(com.sun.jersey | |
* .spi.container.ContainerRequest) | |
*/ | |
@Override | |
public ContainerRequest filter(ContainerRequest request) { | |
String accept = request.getHeaderValue(HttpHeaders.ACCEPT); | |
if(accept != null && !accept.contains(MediaType.APPLICATION_JSON)){ | |
request.getRequestHeaders().putSingle(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); | |
} | |
String contentType = request.getHeaderValue(HttpHeaders.CONTENT_TYPE); | |
if(contentType != null && !contentType.contains(MediaType.APPLICATION_JSON)){ | |
request.getRequestHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); | |
} | |
return request; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2012-06-22 07:20:18,877 ERROR (Grizzly(2)) [package org.usergrid.rest.exceptions] - Error in request (Bad Request) | |
javax.ws.rs.WebApplicationException | |
at com.sun.jersey.server.impl.uri.rules.TerminatingRule.accept(TerminatingRule.java:66) | |
at com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:134) | |
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) | |
at com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:134) | |
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) | |
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) | |
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) | |
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) | |
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469) | |
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400) | |
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) | |
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) | |
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) | |
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) | |
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708) | |
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) | |
at org.glassfish.grizzly.servlet.ServletHandler$FilterChainImpl.doFilter(ServletHandler.java:985) | |
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) | |
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) | |
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) | |
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) | |
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:380) | |
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) | |
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) | |
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) | |
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) | |
at org.glassfish.grizzly.servlet.ServletHandler$FilterChainImpl.doFilter(ServletHandler.java:969) | |
at org.glassfish.grizzly.servlet.ServletHandler$FilterChainImpl.invokeFilterChain(ServletHandler.java:928) | |
at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:382) | |
at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:330) | |
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:163) | |
at org.glassfish.grizzly.http.server.HttpHandlerChain.service(HttpHandlerChain.java:195) | |
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:163) | |
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:158) | |
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) | |
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:286) | |
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:223) | |
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:155) | |
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:134) | |
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:78) | |
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:827) | |
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:103) | |
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:111) | |
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) | |
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:131) | |
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:508) | |
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:488) | |
at java.lang.Thread.run(Thread.java:680) | |
2012-06-22 07:20:18,877 ERROR (Grizzly(2)) [package org.usergrid.rest.exceptions] - Error in request (Bad Request): | |
{"error":"web_application","timestamp":1340374818877,"duration":0,"exception":"javax.ws.rs.WebApplicationException"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment