aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-11-25 10:04:29 +0300
committerGravatar Vitaly Takmazov2016-11-25 10:04:29 +0300
commit68b230f42f93a2e801292a2085cc3fce3a676823 (patch)
treed2da2ed337641689829c1e483305046de01080b3 /juick-api/src
parent6b018f65a4f0391bb7f8e2a06616b8541d0a452e (diff)
juick-api: JSONP and CORS
Diffstat (limited to 'juick-api/src')
-rw-r--r--juick-api/src/main/java/com/juick/api/ApiServer.java24
-rw-r--r--juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java10
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/Messages.java1
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/util/JsonpAdvice.java14
4 files changed, 22 insertions, 27 deletions
diff --git a/juick-api/src/main/java/com/juick/api/ApiServer.java b/juick-api/src/main/java/com/juick/api/ApiServer.java
index c76c165a..80b2b31b 100644
--- a/juick-api/src/main/java/com/juick/api/ApiServer.java
+++ b/juick-api/src/main/java/com/juick/api/ApiServer.java
@@ -28,10 +28,6 @@ import rocks.xmpp.core.session.XmppSessionConfiguration;
import rocks.xmpp.extensions.component.accept.ExternalComponent;
import javax.annotation.PostConstruct;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.io.PrintWriter;
/**
* @author Ugnich Anton
@@ -91,26 +87,6 @@ public class ApiServer implements AutoCloseable {
}
}
- public static void replyJSON(HttpServletRequest request, HttpServletResponse response, String json) throws IOException {
- response.setContentType("application/json; charset=UTF-8");
- response.setHeader("Access-Control-Allow-Origin", "*");
-
- String callback = request.getParameter("callback");
- if (callback != null && (callback.length() > 64 || !callback.matches("[a-zA-Z0-9\\-\\_]+"))) {
- callback = null;
- }
-
- try (PrintWriter out = response.getWriter()) {
- if (callback != null) {
- out.print(callback + "(");
- out.print(json);
- out.print(")");
- } else {
- out.print(json);
- }
- }
- }
-
public ExternalComponent getXmpp() {
return xmpp;
}
diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java
index 7183ab1f..8e0087d7 100644
--- a/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java
+++ b/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java
@@ -2,13 +2,12 @@ package com.juick.api.configuration;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
-import com.juick.api.ApiServer;
-import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@@ -18,7 +17,7 @@ import java.util.List;
* Created by vitalyster on 28.06.2016.
*/
@Configuration
-@ComponentScan(basePackages = {"com.juick.api"})
+@ComponentScan(basePackages = {"com.juick.api.controllers"})
public class ApiMvcConfiguration extends WebMvcConfigurationSupport {
@Override
@@ -40,4 +39,9 @@ public class ApiMvcConfiguration extends WebMvcConfigurationSupport {
converters.add(converter);
super.configureMessageConverters(converters);
}
+
+ @Override
+ protected void addCorsMappings(CorsRegistry registry) {
+ registry.addMapping("/**");
+ }
}
diff --git a/juick-api/src/main/java/com/juick/api/controllers/Messages.java b/juick-api/src/main/java/com/juick/api/controllers/Messages.java
index a6bd3f6d..f4cde321 100644
--- a/juick-api/src/main/java/com/juick/api/controllers/Messages.java
+++ b/juick-api/src/main/java/com/juick/api/controllers/Messages.java
@@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
diff --git a/juick-api/src/main/java/com/juick/api/controllers/util/JsonpAdvice.java b/juick-api/src/main/java/com/juick/api/controllers/util/JsonpAdvice.java
new file mode 100644
index 00000000..cc14976b
--- /dev/null
+++ b/juick-api/src/main/java/com/juick/api/controllers/util/JsonpAdvice.java
@@ -0,0 +1,14 @@
+package com.juick.api.controllers.util;
+
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;
+
+/**
+ * Created by vitalyster on 25.11.2016.
+ */
+@ControllerAdvice
+public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
+ public JsonpAdvice() {
+ super("callback");
+ }
+}