Skip to content

Commit

Permalink
refactoring, no change
Browse files Browse the repository at this point in the history
  • Loading branch information
andrus committed Jan 9, 2022
1 parent 6bf29b6 commit 0391777
Showing 1 changed file with 45 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,68 +1,69 @@
package io.agrest.provider;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import com.fasterxml.jackson.core.JsonGenerator;
import io.agrest.SimpleResponse;
import io.agrest.runtime.AgRuntime;
import io.agrest.runtime.jackson.IJacksonService;

import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.core.JsonGenerator;
import io.agrest.SimpleResponse;
import io.agrest.runtime.AgRuntime;
import io.agrest.runtime.jackson.IJacksonService;
import io.agrest.runtime.jackson.JsonConvertable;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

@Provider
public class SimpleResponseWriter implements MessageBodyWriter<SimpleResponse> {

private IJacksonService jacksonService;
private IJacksonService jacksonService;

@Context
private Configuration configuration;

@Context
private Configuration configuration;
@Override
public long getSize(SimpleResponse t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}

@Override
public long getSize(SimpleResponse t, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
return -1;
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return SimpleResponse.class.isAssignableFrom(type);
}

@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return SimpleResponse.class.isAssignableFrom(type);
}
@Override
public void writeTo(
SimpleResponse t,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException {

@Override
public void writeTo(final SimpleResponse t, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException {
getJacksonService().outputJson(out -> writeData(t, out), entityStream);
}

getJacksonService().outputJson(new JsonConvertable() {
@Override
public void generateJSON(JsonGenerator out) throws IOException {
out.writeStartObject();
out.writeBooleanField("success", t.isSuccess());
private IJacksonService getJacksonService() {
if (jacksonService == null) {
jacksonService = AgRuntime.service(IJacksonService.class, configuration);
}

if (t.getMessage() != null) {
out.writeStringField("message", t.getMessage());
}
return jacksonService;
}

out.writeEndObject();
}
}, entityStream);
}
protected void writeData(SimpleResponse t, JsonGenerator out) throws IOException {
out.writeStartObject();
out.writeBooleanField("success", t.isSuccess());

private IJacksonService getJacksonService() {
if (jacksonService == null) {
jacksonService = AgRuntime.service(IJacksonService.class, configuration);
}
if (t.getMessage() != null) {
out.writeStringField("message", t.getMessage());
}

return jacksonService;
}
out.writeEndObject();
}

}

0 comments on commit 0391777

Please sign in to comment.