1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
package org.eclipse.osee.ats.rest.internal.config;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import javax.ws.rs.WebApplicationException;
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 org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.eclipse.osee.ats.api.IAtsConfigObject;
import org.eclipse.osee.ats.impl.IAtsServer;
import org.eclipse.osee.jaxrs.mvc.IdentityView;
import org.eclipse.osee.orcs.data.AttributeTypes;
/**
* @author Donald G. Dunne
*/
@Provider
public class ConfigsJsonWriter implements MessageBodyWriter<Collection<IAtsConfigObject>> {
private JsonFactory jsonFactory;
private IAtsServer atsServer;
public void setAtsServer(IAtsServer atsServer) {
this.atsServer = atsServer;
}
public void start() {
jsonFactory = new JsonFactory();
}
public void stop() {
jsonFactory = null;
}
@Override
public long getSize(Collection<IAtsConfigObject> data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
boolean isWriteable = false;
if (Collection.class.isAssignableFrom(type) && genericType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericType;
Type[] actualTypeArgs = parameterizedType.getActualTypeArguments();
if (actualTypeArgs.length == 1) {
Type t = actualTypeArgs[0];
if (t instanceof Class) {
Class<?> clazz = (Class<?>) t;
isWriteable = IAtsConfigObject.class.isAssignableFrom(clazz);
}
}
}
return isWriteable;
}
private boolean matches(Class<? extends Annotation> toMatch, Annotation[] annotations) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().isAssignableFrom(toMatch)) {
return true;
}
}
return false;
}
private AttributeTypes getAttributeTypes() {
return atsServer.getOrcsApi().getOrcsTypes(null).getAttributeTypes();
}
@Override
public void writeTo(Collection<IAtsConfigObject> programs, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
JsonGenerator writer = null;
try {
writer = jsonFactory.createJsonGenerator(entityStream);
writer.writeStartArray();
for (IAtsConfigObject program : programs) {
ConfigJsonWriter.addProgramObject(atsServer, program, annotations, writer,
matches(IdentityView.class, annotations), getAttributeTypes());
}
writer.writeEndArray();
} finally {
if (writer != null) {
writer.flush();
}
}
}
}
|