Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#146 - Respond with a 404 for endpoints that do not exist #147

Merged
merged 1 commit into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions katharsis-core/src/main/java/io/katharsis/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,31 @@ public static String join(String delimiter, Iterable<String> stringsIterable) {
}
return ab.toString();
}

/**
* <p>Checks if a String is whitespace, empty ("") or null.</p>
* <p>
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("katharsis") = false
* StringUtils.isBlank(" katharsis ") = false
* </pre>
*
* @param value the String to check, may be null
* @return <code>true</code> if the String is null, empty or whitespace
*/
public static boolean isBlank(String value) {
int strLen;
if (value == null || (strLen = value.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((!Character.isWhitespace(value.charAt(i)))) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.util.Collections;
import java.util.List;

import static junit.framework.TestCase.assertFalse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;

public class StringUtilsTest {

Expand Down Expand Up @@ -34,4 +36,15 @@ public void onTwoElementsShouldReturnJoinedValues() throws Exception {
// THEN
assertThat(result).isEqualTo("hello world");
}

@Test
public void onIsBlankValues() throws Exception {
assertTrue(StringUtils.isBlank(null));
assertTrue(StringUtils.isBlank(""));
assertTrue(StringUtils.isBlank(" "));
assertFalse(StringUtils.isBlank("katharsis"));
assertFalse(StringUtils.isBlank(" katharsis "));
}


}
3 changes: 3 additions & 0 deletions katharsis-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,32 @@
*/
package io.katharsis.invoker;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.net.MediaType;

import io.katharsis.dispatcher.RequestDispatcher;
import io.katharsis.errorhandling.exception.KatharsisMappableException;
import io.katharsis.errorhandling.exception.KatharsisMatchingException;
import io.katharsis.errorhandling.mapper.KatharsisExceptionMapper;
import io.katharsis.jackson.exception.JsonDeserializationException;
import io.katharsis.queryParams.QueryParamsBuilder;
import io.katharsis.queryspec.internal.QueryAdapter;
import io.katharsis.queryspec.internal.QueryParamsAdapter;
import io.katharsis.repository.RepositoryMethodParameterProvider;
import io.katharsis.request.dto.RequestBody;
import io.katharsis.request.path.JsonPath;
import io.katharsis.request.path.PathBuilder;
import io.katharsis.resource.registry.ResourceRegistry;
import io.katharsis.response.BaseResponseContext;
import io.katharsis.servlet.util.QueryStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
* Katharsis dispatcher invoker.
Expand All @@ -62,7 +56,7 @@ public class KatharsisInvoker {
private ResourceRegistry resourceRegistry;
private RequestDispatcher requestDispatcher;

public KatharsisInvoker(ObjectMapper objectMapper,
public KatharsisInvoker(ObjectMapper objectMapper,
ResourceRegistry resourceRegistry, RequestDispatcher requestDispatcher) {
this.objectMapper = objectMapper;
this.resourceRegistry = resourceRegistry;
Expand Down Expand Up @@ -99,7 +93,7 @@ private void dispatchRequest(KatharsisInvokerContext invokerContext) throws Exce
String method = invokerContext.getRequestMethod();
RepositoryMethodParameterProvider parameterProvider = invokerContext.getParameterProvider();
katharsisResponse = requestDispatcher.dispatchRequest(jsonPath, method, parameters, parameterProvider,
requestBody);
requestBody);
} catch (KatharsisMappableException e) {
if (log.isDebugEnabled()) {
log.warn("Error occurred while dispatching katharsis request. " + e, e);
Expand Down Expand Up @@ -131,6 +125,8 @@ private void dispatchRequest(KatharsisInvokerContext invokerContext) throws Exce
closeQuietly(baos);
closeQuietly(out);
}
} else if (passToMethodMatcher) {
invokerContext.setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
} else {
invokerContext.setResponseStatus(HttpServletResponse.SC_NO_CONTENT);
}
Expand All @@ -141,7 +137,7 @@ private boolean isAcceptableMediaType(KatharsisInvokerContext invokerContext) {
String acceptHeader = invokerContext.getRequestHeader("Accept");

if (acceptHeader != null) {
String [] accepts = acceptHeader.split(",");
String[] accepts = acceptHeader.split(",");
MediaType acceptableType;

for (String mediaTypeItem : accepts) {
Expand All @@ -157,8 +153,8 @@ private boolean isAcceptableMediaType(KatharsisInvokerContext invokerContext) {
}

private Map<String, Set<String>> getParameters(KatharsisInvokerContext invokerContext) {
return
QueryStringUtils.parseQueryStringAsSingleValueMap(invokerContext);
return
QueryStringUtils.parseQueryStringAsSingleValueMap(invokerContext);
}

private RequestBody inputStreamToBody(InputStream is) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,8 @@
*/
package io.katharsis.servlet;

import static net.javacrumbs.jsonunit.JsonAssert.assertJsonPartEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import io.katharsis.invoker.JsonApiMediaType;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

import io.katharsis.utils.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -37,9 +28,18 @@
import org.springframework.mock.web.MockServletConfig;
import org.springframework.mock.web.MockServletContext;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import static net.javacrumbs.jsonunit.JsonAssert.assertJsonPartEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* Test for {@link AbstractKatharsisServlet}.
*/
Expand Down Expand Up @@ -73,9 +73,9 @@ public void before() throws Exception {
((MockServletContext) servletContext).setContextPath("");
servletConfig = new MockServletConfig(servletContext);
((MockServletConfig) servletConfig).addInitParameter(KatharsisProperties.RESOURCE_SEARCH_PACKAGE,
RESOURCE_SEARCH_PACKAGE);
RESOURCE_SEARCH_PACKAGE);
((MockServletConfig) servletConfig).addInitParameter(KatharsisProperties.RESOURCE_DEFAULT_DOMAIN,
RESOURCE_DEFAULT_DOMAIN);
RESOURCE_DEFAULT_DOMAIN);

katharsisServlet.init(servletConfig);
}
Expand Down Expand Up @@ -193,4 +193,23 @@ public void testUnacceptableRequestContentType() throws Exception {
assertTrue(responseContent == null || "".equals(responseContent.trim()));
}

@Test
public void testKatharsisMatchingException() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
request.setMethod("GET");
request.setContextPath("");
request.setServletPath("/api");
request.setPathInfo("/tasks-matching-exception");
request.setRequestURI("/api/matching-exception");
request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
request.addHeader("Accept", "*/*");
MockHttpServletResponse response = new MockHttpServletResponse();
katharsisServlet.service(request, response);

String responseContent = response.getContentAsString();
assertTrue("Response should not be returned for non matching resource type", StringUtils.isBlank(responseContent));
assertEquals(404, response.getStatus());

}

}