-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
44 deletions.
There are no files selected for viewing
89 changes: 45 additions & 44 deletions
89
agrest-engine/src/main/java/io/agrest/provider/SimpleResponseWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |