Skip to content

Commit

Permalink
Clean up rest api, increase testing via postman
Browse files Browse the repository at this point in the history
  • Loading branch information
mbuckton committed Dec 20, 2024
1 parent 258f8b1 commit 25f0af0
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
@Path(URI_PATH + "/messaging")
public class MessagingApi extends BaseRestApi {

@Path("/messaging/publish")
@Path("/publish")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Publish a message", description = "Publishes a message to a specified topic")
Expand All @@ -56,7 +56,7 @@ public Response publishMessage(@Valid PublishRequestDTO publishRequest) {
return Response.ok().entity("Message published successfully").build();
}

@Path("/messaging/subscribe")
@Path("/subscribe")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Subscribe to a topic", description = "Subscribes to a specified topic")
Expand All @@ -73,7 +73,7 @@ public Response subscribeToTopic(@Valid SubscriptionRequestDTO subscriptionReque
return Response.ok().entity("Subscribed to topic successfully").build();
}

@Path("/messaging/consume/{subscriptionName}")
@Path("/consume/{subscriptionName}")
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Get messages", description = "Retrieves messages for a specified subscription")
@ApiResponse(responseCode = "200",
Expand All @@ -90,7 +90,7 @@ public Response consumeMessages(@PathParam("subscriptionName") String subscripti
return Response.ok().entity("Messages retrieved successfully").build();
}

@Path("/messaging/consume")
@Path("/consume")
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Get all messages", description = "Retrieves messages for a specified subscription")
@ApiResponse(responseCode = "200",
Expand All @@ -108,7 +108,7 @@ public Response consumeAllMessages() {
}

@GET
@Path("/messaging/subscriptionDepth/{subscriptionName}")
@Path("/subscriptionDepth/{subscriptionName}")
@Operation(summary = "Get message depth", description = "Get the depth of the queue for a specified subscription")
@ApiResponse(responseCode = "200",
description = "Message depth retrieved successfully",
Expand All @@ -125,7 +125,7 @@ public Response getSubscriptionDepth(@PathParam("subscriptionName") String subsc
}

@GET
@Path("/messaging/subscriptionDepth")
@Path("/subscriptionDepth")
@Operation(summary = "Get all message depth", description = "Get the depth of the queue for all subscriptions")
@ApiResponse(responseCode = "200",
description = "Message depths retrieved successfully",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright [ 2020 - 2024 ] [Matthew Buckton]
* Copyright [ 2024 - 2024 ] [Maps Messaging B.V.]
*
* 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 io.mapsmessaging.rest.translation;

import com.google.gson.*;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class GsonDateTimeDeserialiser implements JsonDeserializer<Object> {

private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

@Override
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
if (json.isJsonNull()) {
return null;
}

// Handle LocalDateTime specifically
if (typeOfT == LocalDateTime.class) {
return LocalDateTime.parse(json.getAsString(), FORMATTER);
}

// Delegate to Gson for other types
return context.deserialize(json, typeOfT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright [ 2020 - 2024 ] [Matthew Buckton]
* Copyright [ 2024 - 2024 ] [Maps Messaging B.V.]
*
* 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 io.mapsmessaging.rest.translation;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class GsonDateTimeSerialiser implements JsonSerializer<Object> {

private static final DateTimeFormatter FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

@Override
public JsonElement serialize(Object src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) {
return context.serialize(null);
}

// Handle LocalDateTime specifically
if (src instanceof LocalDateTime) {
LocalDateTime localDateTime = (LocalDateTime) src;
return context.serialize(localDateTime.format(FORMATTER));
}

// Handle other types explicitly if needed
if (src instanceof String || src instanceof Number || src instanceof Boolean) {
return context.serialize(src);
}

// For custom objects, serialize fields into a JsonObject
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("class", src.getClass().getName());
jsonObject.add("fields", context.serialize(src)); // Serialize object fields

return jsonObject;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package io.mapsmessaging.rest.translation;

import com.google.gson.Gson;
import com.google.gson.*;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.MessageBodyReader;
Expand All @@ -27,12 +27,16 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class GsonMessageBodyReader implements MessageBodyReader<Object> {

private final Gson gson = new Gson();
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new GsonDateTimeSerialiser())
.registerTypeAdapter(LocalDateTime.class, new GsonDateTimeDeserialiser())
.create();

@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, jakarta.ws.rs.core.MediaType mediaType) {
Expand All @@ -44,5 +48,6 @@ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annota
jakarta.ws.rs.core.MultivaluedMap<String, String> httpHeaders, java.io.InputStream entityStream) {
return gson.fromJson(new InputStreamReader(entityStream, StandardCharsets.UTF_8), genericType);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.mapsmessaging.rest.translation;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.MessageBodyWriter;
Expand All @@ -27,12 +28,16 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class GsonMessageBodyWriter implements MessageBodyWriter<Object> {

private final Gson gson = new Gson();
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new GsonDateTimeSerialiser())
.registerTypeAdapter(LocalDateTime.class, new GsonDateTimeDeserialiser())
.create();

@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, jakarta.ws.rs.core.MediaType mediaType) {
Expand Down

0 comments on commit 25f0af0

Please sign in to comment.