[New] Added hooks to ToFlatJsonVisitor
This commit is contained in:
parent
313eaf0026
commit
c5cd74b168
|
@ -18,7 +18,7 @@ package li.strolch.rest.filters;
|
|||
import static li.strolch.rest.StrolchRestfulConstants.STROLCH_CERTIFICATE;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ws.rs.container.ContainerRequestContext;
|
||||
|
@ -48,7 +48,9 @@ public class AuthenicationRequestFilter implements ContainerRequestFilter {
|
|||
private static final Logger logger = LoggerFactory.getLogger(AuthenicationRequestFilter.class);
|
||||
|
||||
protected List<String> getUnsecuredPaths() {
|
||||
return Arrays.asList("strolch/authentication");
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("strolch/authentication");
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
package li.strolch.rest.helper;
|
||||
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import li.strolch.utils.helper.ExceptionHelper;
|
||||
import li.strolch.utils.helper.StringHelper;
|
||||
|
||||
/**
|
||||
* Created by eitch on 29.08.16.
|
||||
*/
|
||||
public class ResponseUtil {
|
||||
|
||||
public static final String MSG = "msg";
|
||||
|
||||
public static Response toResponse() {
|
||||
JsonObject response = new JsonObject();
|
||||
response.addProperty(MSG, StringHelper.DASH);
|
||||
|
||||
String json = new Gson().toJson(response);
|
||||
|
||||
return Response.ok(json, MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
||||
public static Response toResponse(String prop, String value) {
|
||||
JsonObject response = new JsonObject();
|
||||
response.addProperty(MSG, StringHelper.DASH);
|
||||
response.addProperty(prop, value);
|
||||
|
||||
String json = new Gson().toJson(response);
|
||||
|
||||
return Response.ok(json, MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
||||
public static Response toResponse(String member, JsonElement jsonElement) {
|
||||
JsonObject response = new JsonObject();
|
||||
response.addProperty(MSG, StringHelper.DASH);
|
||||
response.add(member, jsonElement);
|
||||
|
||||
String json = new Gson().toJson(response);
|
||||
|
||||
return Response.ok(json, MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
||||
public static Response toResponse(Throwable t) {
|
||||
JsonObject response = new JsonObject();
|
||||
response.addProperty(MSG, ExceptionHelper.getExceptionMessageWithCauses(t));
|
||||
String json = new Gson().toJson(response);
|
||||
|
||||
return Response.serverError().entity(json).type(MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
}
|
|
@ -5,7 +5,7 @@ import com.google.gson.JsonObject;
|
|||
import li.strolch.model.activity.Activity;
|
||||
import li.strolch.model.visitor.ActivityVisitor;
|
||||
|
||||
public class ActivityToFlatJsonVisitor extends ToFlatJsonVisitor implements ActivityVisitor<JsonObject> {
|
||||
public class ActivityToFlatJsonVisitor extends ToFlatJsonVisitor<Activity> implements ActivityVisitor<JsonObject> {
|
||||
|
||||
@Override
|
||||
public JsonObject visit(Activity element) {
|
||||
|
|
|
@ -5,7 +5,7 @@ import com.google.gson.JsonObject;
|
|||
import li.strolch.model.Order;
|
||||
import li.strolch.model.visitor.OrderVisitor;
|
||||
|
||||
public class OrderToFlatJsonVisitor extends ToFlatJsonVisitor implements OrderVisitor<JsonObject> {
|
||||
public class OrderToFlatJsonVisitor extends ToFlatJsonVisitor<Order> implements OrderVisitor<JsonObject> {
|
||||
|
||||
@Override
|
||||
public JsonObject visit(Order element) {
|
||||
|
|
|
@ -5,7 +5,7 @@ import com.google.gson.JsonObject;
|
|||
import li.strolch.model.Resource;
|
||||
import li.strolch.model.visitor.ResourceVisitor;
|
||||
|
||||
public class ResourceToFlatJsonVisitor extends ToFlatJsonVisitor implements ResourceVisitor<JsonObject> {
|
||||
public class ResourceToFlatJsonVisitor extends ToFlatJsonVisitor<Resource> implements ResourceVisitor<JsonObject> {
|
||||
|
||||
@Override
|
||||
public JsonObject visit(Resource element) {
|
||||
|
|
|
@ -1,7 +1,13 @@
|
|||
package li.strolch.rest.visitor;
|
||||
|
||||
import static li.strolch.model.Tags.ID;
|
||||
import static li.strolch.model.Tags.NAME;
|
||||
import static li.strolch.model.Tags.OBJECT_TYPE;
|
||||
import static li.strolch.model.Tags.TYPE;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
|
@ -25,9 +31,10 @@ import li.strolch.utils.collections.MapOfSets;
|
|||
*
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class ToFlatJsonVisitor {
|
||||
public class ToFlatJsonVisitor<T extends StrolchRootElement> {
|
||||
|
||||
private MapOfSets<String, String> ignoredKeys;
|
||||
private BiConsumer<T, JsonObject> hook;
|
||||
|
||||
public ToFlatJsonVisitor() {
|
||||
this.ignoredKeys = new MapOfSets<>();
|
||||
|
@ -37,6 +44,10 @@ public class ToFlatJsonVisitor {
|
|||
this.ignoredKeys = new MapOfSets<>();
|
||||
}
|
||||
|
||||
public void setHook(BiConsumer<T, JsonObject> hook) {
|
||||
this.hook = hook;
|
||||
}
|
||||
|
||||
public void ignoreBag(String bagId) {
|
||||
this.ignoredKeys.addSet(bagId, Collections.emptySet());
|
||||
}
|
||||
|
@ -45,10 +56,15 @@ public class ToFlatJsonVisitor {
|
|||
this.ignoredKeys.addElement(bagId, paramId);
|
||||
}
|
||||
|
||||
public JsonObject toJson(StrolchRootElement element) {
|
||||
public JsonObject toJson(T element) {
|
||||
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
|
||||
jsonObject.addProperty(ID, element.getId());
|
||||
jsonObject.addProperty(NAME, element.getName());
|
||||
jsonObject.addProperty(TYPE, element.getType());
|
||||
jsonObject.addProperty(OBJECT_TYPE, element.getClass().getSimpleName());
|
||||
|
||||
Set<String> bagKeySet = element.getParameterBagKeySet();
|
||||
for (String bagId : bagKeySet) {
|
||||
|
||||
|
@ -83,6 +99,9 @@ public class ToFlatJsonVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
if (hook != null)
|
||||
this.hook.accept(element, jsonObject);
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue