mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-20 04:35:32 +00:00
refactored to remove duplications from AbstractSailingServerResource; fixed flush() problem for StreamingOutput also in shared...
This commit is contained in:
@@ -53,3 +53,4 @@ Require-Bundle: com.sap.sailing.domain,
|
||||
Bundle-ClassPath: .
|
||||
Web-ContextPath: /sharedsailingserver
|
||||
Automatic-Module-Name: com.sap.sailing.shared.server.gateway
|
||||
Export-Package: com.sap.sailing.shared.server.gateway.jaxrs
|
||||
|
||||
+31
-1
@@ -1,11 +1,19 @@
|
||||
package com.sap.sailing.shared.server.gateway.jaxrs;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.StreamingOutput;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.util.tracker.ServiceTracker;
|
||||
|
||||
@@ -27,7 +35,7 @@ import com.sap.sse.security.SecurityService;
|
||||
import com.sap.sse.util.DateParser;
|
||||
import com.sun.jersey.api.core.ResourceContext;
|
||||
|
||||
public abstract class AbstractSailingServerResource {
|
||||
public abstract class SharedAbstractSailingServerResource {
|
||||
private static final String SLASH_ENCODING = "__";
|
||||
@Context ServletContext servletContext;
|
||||
@Context ResourceContext resourceContext;
|
||||
@@ -146,4 +154,26 @@ public abstract class AbstractSailingServerResource {
|
||||
bigDecimal = bigDecimal.setScale(places, RoundingMode.HALF_UP);
|
||||
return bigDecimal.doubleValue();
|
||||
}
|
||||
|
||||
protected StreamingOutput streamingOutput(JSONObject jsonObject) {
|
||||
return new StreamingOutput() {
|
||||
@Override
|
||||
public void write(OutputStream output) throws IOException, WebApplicationException {
|
||||
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(output));
|
||||
jsonObject.writeJSONString(bufferedWriter);
|
||||
bufferedWriter.flush();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected StreamingOutput streamingOutput(JSONArray jsonArray) {
|
||||
return new StreamingOutput() {
|
||||
@Override
|
||||
public void write(OutputStream output) throws IOException, WebApplicationException {
|
||||
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(output));
|
||||
jsonArray.writeJSONString(bufferedWriter);
|
||||
bufferedWriter.flush();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+5
-11
@@ -1,8 +1,5 @@
|
||||
package com.sap.sailing.shared.server.gateway.jaxrs.api;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -16,7 +13,6 @@ import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.StreamingOutput;
|
||||
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.json.simple.JSONArray;
|
||||
@@ -28,11 +24,11 @@ import com.sap.sailing.server.gateway.deserialization.JsonDeserializer;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.CourseTemplateJsonDeserializer;
|
||||
import com.sap.sailing.server.gateway.serialization.JsonSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.CourseTemplateJsonSerializer;
|
||||
import com.sap.sailing.shared.server.gateway.jaxrs.AbstractSailingServerResource;
|
||||
import com.sap.sailing.shared.server.gateway.jaxrs.SharedAbstractSailingServerResource;
|
||||
import com.sun.jersey.api.client.ClientResponse.Status;
|
||||
|
||||
@Path("/v1/coursetemplates")
|
||||
public class CourseTemplateResource extends AbstractSailingServerResource {
|
||||
public class CourseTemplateResource extends SharedAbstractSailingServerResource {
|
||||
|
||||
private final JsonSerializer<CourseTemplate> courseTemplateSerializer;
|
||||
|
||||
@@ -62,8 +58,7 @@ public class CourseTemplateResource extends AbstractSailingServerResource {
|
||||
for (CourseTemplate courseTemplate : courseTemplateList) {
|
||||
result.add(courseTemplateSerializer.serialize(courseTemplate));
|
||||
}
|
||||
final String json = result.toJSONString();
|
||||
return Response.ok(json).build();
|
||||
return Response.ok(streamingOutput(result)).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@@ -74,9 +69,8 @@ public class CourseTemplateResource extends AbstractSailingServerResource {
|
||||
if (courseTemplate == null) {
|
||||
return getCourseTemplateNotFoundErrorResponse();
|
||||
}
|
||||
|
||||
final JSONObject serializedMarkProperties = courseTemplateSerializer.serialize(courseTemplate);
|
||||
return Response.ok((StreamingOutput) (OutputStream output)->serializedMarkProperties.writeJSONString(new BufferedWriter(new OutputStreamWriter(output)))).build();
|
||||
return Response.ok(streamingOutput(serializedMarkProperties)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@@ -99,7 +93,7 @@ public class CourseTemplateResource extends AbstractSailingServerResource {
|
||||
deserializedCourseTemplate.getDefaultMarkTemplatesForMarkRoles(), deserializedCourseTemplate.getRepeatablePart(),
|
||||
deserializedCourseTemplate.getTags(), deserializedCourseTemplate.getOptionalImageURL(), deserializedCourseTemplate.getDefaultNumberOfLaps());
|
||||
final JSONObject serializedMarkProperties = courseTemplateSerializer.serialize(createdCourseTemplate);
|
||||
return Response.ok((StreamingOutput) (OutputStream output)->serializedMarkProperties.writeJSONString(new BufferedWriter(new OutputStreamWriter(output)))).build();
|
||||
return Response.ok(streamingOutput(serializedMarkProperties)).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
|
||||
+7
-12
@@ -1,8 +1,5 @@
|
||||
package com.sap.sailing.shared.server.gateway.jaxrs.api;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
@@ -18,7 +15,6 @@ import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.StreamingOutput;
|
||||
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.json.simple.JSONArray;
|
||||
@@ -39,14 +35,14 @@ import com.sap.sailing.server.gateway.serialization.impl.DeviceIdentifierJsonSer
|
||||
import com.sap.sailing.server.gateway.serialization.impl.MarkPropertiesJsonSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.racelog.tracking.DeviceIdentifierJsonHandler;
|
||||
import com.sap.sailing.server.gateway.serialization.racelog.tracking.impl.PlaceHolderDeviceIdentifierJsonHandler;
|
||||
import com.sap.sailing.shared.server.gateway.jaxrs.AbstractSailingServerResource;
|
||||
import com.sap.sailing.shared.server.gateway.jaxrs.SharedAbstractSailingServerResource;
|
||||
import com.sap.sse.common.Color;
|
||||
import com.sap.sse.common.TypeBasedServiceFinder;
|
||||
import com.sap.sse.common.impl.RGBColor;
|
||||
import com.sun.jersey.api.client.ClientResponse.Status;
|
||||
|
||||
@Path("/v1/markproperties")
|
||||
public class MarkPropertiesResource extends AbstractSailingServerResource {
|
||||
public class MarkPropertiesResource extends SharedAbstractSailingServerResource {
|
||||
private JsonSerializer<MarkProperties> markPropertiesSerializer;
|
||||
|
||||
public MarkPropertiesResource() {
|
||||
@@ -69,8 +65,7 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
|
||||
for (MarkProperties markProperties : markPropertiesList) {
|
||||
result.add(getMarkPropertiesSerializer().serialize(markProperties));
|
||||
}
|
||||
final String json = result.toJSONString();
|
||||
return Response.ok(json).build();
|
||||
return Response.ok(streamingOutput(result)).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@@ -82,7 +77,7 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
|
||||
return getMarkPropertiesNotFoundErrorResponse();
|
||||
}
|
||||
final JSONObject serializedMarkProperties = getMarkPropertiesSerializer().serialize(markProperties);
|
||||
return Response.ok((StreamingOutput) (OutputStream output)->serializedMarkProperties.writeJSONString(new BufferedWriter(new OutputStreamWriter(output)))).build();
|
||||
return Response.ok(streamingOutput(serializedMarkProperties)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@@ -133,7 +128,7 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
|
||||
getSharedSailingData().setFixedPositionForMarkProperties(createdMarkProperties, fixedPosition);
|
||||
}
|
||||
final JSONObject serializedMarkProperties = getMarkPropertiesSerializer().serialize(createdMarkProperties);
|
||||
return Response.ok((StreamingOutput) (OutputStream output)->serializedMarkProperties.writeJSONString(new BufferedWriter(new OutputStreamWriter(output)))).build();
|
||||
return Response.ok(streamingOutput(serializedMarkProperties)).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@@ -156,7 +151,7 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
|
||||
getSharedSailingData().clearPositioningForMarkProperties(markProperties);
|
||||
}
|
||||
final JSONObject serializedMarkProperties = getMarkPropertiesSerializer().serialize(markProperties);
|
||||
return Response.ok((StreamingOutput) (OutputStream output)->serializedMarkProperties.writeJSONString(new BufferedWriter(new OutputStreamWriter(output)))).build();
|
||||
return Response.ok(streamingOutput(serializedMarkProperties)).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@@ -208,7 +203,7 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
|
||||
}
|
||||
getSharedSailingData().updateMarkProperties(markPropertiesUUID, markPropertiesBuilder.build(), positioningInformation, tags);
|
||||
final JSONObject serializedMarkProperties = getMarkPropertiesSerializer().serialize(markProperties);
|
||||
return Response.ok((StreamingOutput) (OutputStream output)->serializedMarkProperties.writeJSONString(new BufferedWriter(new OutputStreamWriter(output)))).build();
|
||||
return Response.ok(streamingOutput(serializedMarkProperties)).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
|
||||
+5
-10
@@ -1,8 +1,5 @@
|
||||
package com.sap.sailing.shared.server.gateway.jaxrs.api;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.ws.rs.FormParam;
|
||||
@@ -13,7 +10,6 @@ import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.StreamingOutput;
|
||||
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.json.simple.JSONArray;
|
||||
@@ -22,11 +18,11 @@ import org.json.simple.JSONObject;
|
||||
import com.sap.sailing.domain.coursetemplate.MarkRole;
|
||||
import com.sap.sailing.server.gateway.serialization.JsonSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.MarkRoleJsonSerializer;
|
||||
import com.sap.sailing.shared.server.gateway.jaxrs.AbstractSailingServerResource;
|
||||
import com.sap.sailing.shared.server.gateway.jaxrs.SharedAbstractSailingServerResource;
|
||||
import com.sun.jersey.api.client.ClientResponse.Status;
|
||||
|
||||
@Path("/v1/markroles")
|
||||
public class MarkRoleResource extends AbstractSailingServerResource {
|
||||
public class MarkRoleResource extends SharedAbstractSailingServerResource {
|
||||
|
||||
private final JsonSerializer<MarkRole> markRoleSerializer;
|
||||
|
||||
@@ -51,8 +47,7 @@ public class MarkRoleResource extends AbstractSailingServerResource {
|
||||
for (MarkRole markRole : markRolesList) {
|
||||
result.add(markRoleSerializer.serialize(markRole));
|
||||
}
|
||||
final String json = result.toJSONString();
|
||||
return Response.ok(json).build();
|
||||
return Response.ok(streamingOutput(result)).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@@ -64,7 +59,7 @@ public class MarkRoleResource extends AbstractSailingServerResource {
|
||||
return getMarkRoleNotFoundErrorResponse();
|
||||
}
|
||||
final JSONObject serializedMarkRole = markRoleSerializer.serialize(markRole);
|
||||
return Response.ok((StreamingOutput) (OutputStream output)->serializedMarkRole.writeJSONString(new BufferedWriter(new OutputStreamWriter(output)))).build();
|
||||
return Response.ok(streamingOutput(serializedMarkRole)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@@ -75,6 +70,6 @@ public class MarkRoleResource extends AbstractSailingServerResource {
|
||||
}
|
||||
final MarkRole markRole = getSharedSailingData().createMarkRole(name, shortName);
|
||||
final JSONObject serializedMarkRole = markRoleSerializer.serialize(markRole);
|
||||
return Response.ok((StreamingOutput) (OutputStream output)->serializedMarkRole.writeJSONString(new BufferedWriter(new OutputStreamWriter(output)))).build();
|
||||
return Response.ok(streamingOutput(serializedMarkRole)).build();
|
||||
}
|
||||
}
|
||||
|
||||
+4
-11
@@ -1,8 +1,5 @@
|
||||
package com.sap.sailing.shared.server.gateway.jaxrs.api;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.ws.rs.FormParam;
|
||||
@@ -13,7 +10,6 @@ import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.StreamingOutput;
|
||||
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.json.simple.JSONArray;
|
||||
@@ -24,13 +20,13 @@ import com.sap.sailing.domain.coursetemplate.MarkTemplate;
|
||||
import com.sap.sailing.domain.coursetemplate.impl.MarkTemplateImpl;
|
||||
import com.sap.sailing.server.gateway.serialization.JsonSerializer;
|
||||
import com.sap.sailing.server.gateway.serialization.impl.MarkTemplateJsonSerializer;
|
||||
import com.sap.sailing.shared.server.gateway.jaxrs.AbstractSailingServerResource;
|
||||
import com.sap.sailing.shared.server.gateway.jaxrs.SharedAbstractSailingServerResource;
|
||||
import com.sap.sse.common.Color;
|
||||
import com.sap.sse.common.impl.RGBColor;
|
||||
import com.sun.jersey.api.client.ClientResponse.Status;
|
||||
|
||||
@Path("/v1/marktemplates")
|
||||
public class MarkTemplateResource extends AbstractSailingServerResource {
|
||||
public class MarkTemplateResource extends SharedAbstractSailingServerResource {
|
||||
|
||||
private Response getBadMarkTemplateValidationErrorResponse(String errorText) {
|
||||
return Response.status(Status.BAD_REQUEST).entity(StringEscapeUtils.escapeHtml(errorText) + ".")
|
||||
@@ -64,7 +60,7 @@ public class MarkTemplateResource extends AbstractSailingServerResource {
|
||||
}
|
||||
JsonSerializer<MarkTemplate> markTemplateSerializer = new MarkTemplateJsonSerializer();
|
||||
final JSONObject serializedMarkTemplate = markTemplateSerializer.serialize(markTemplate);
|
||||
return Response.ok((StreamingOutput) (OutputStream output)->serializedMarkTemplate.writeJSONString(new BufferedWriter(new OutputStreamWriter(output)))).build();
|
||||
return Response.ok(streamingOutput(serializedMarkTemplate)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@@ -77,7 +73,6 @@ public class MarkTemplateResource extends AbstractSailingServerResource {
|
||||
return getBadMarkTemplateValidationErrorResponse("name must be given");
|
||||
}
|
||||
final String effectiveShortName = shortName == null || shortName.isEmpty() ? name : shortName;
|
||||
|
||||
Color color = null;
|
||||
if (rgbColor != null && rgbColor.length() > 0) {
|
||||
try {
|
||||
@@ -90,12 +85,10 @@ public class MarkTemplateResource extends AbstractSailingServerResource {
|
||||
if (markType != null && markType.length() > 0) {
|
||||
type = MarkType.valueOf(markType);
|
||||
}
|
||||
|
||||
final MarkTemplate markTemplate = new MarkTemplateImpl(name, effectiveShortName, color, shape, pattern, type);
|
||||
final MarkTemplate createdMarkTemplate = getSharedSailingData().createMarkTemplate(markTemplate);
|
||||
JsonSerializer<MarkTemplate> markTemplateSerializer = new MarkTemplateJsonSerializer();
|
||||
final JSONObject serializedMarkTemplate = markTemplateSerializer.serialize(createdMarkTemplate);
|
||||
return Response.ok((StreamingOutput) (OutputStream output)->serializedMarkTemplate.writeJSONString(new BufferedWriter(new OutputStreamWriter(output)))).build();
|
||||
return Response.ok(streamingOutput(serializedMarkTemplate)).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user