aboutsummaryrefslogtreecommitdiffstats
blob: 5e045a72e0c23d2f6a6285f6ef3d88fbb5f2cb8b (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*******************************************************************************
 * Copyright (c) 2012, 2013 IBM Corporation.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 *
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *
 *     Russell Boykin       - initial API and implementation
 *     Alberto Giammaria    - initial API and implementation
 *     Chris Peters         - initial API and implementation
 *     Gianluca Bernardini  - initial API and implementation
 *     Steve Pitschke       - Add support for FilteredResource and
 *                            ResponseInfo
 *******************************************************************************/
package org.eclipse.lyo.oslc4j.provider.json4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import org.eclipse.lyo.oslc4j.core.OSLC4JUtils;
import org.eclipse.lyo.oslc4j.core.model.FilteredResource;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.oslc4j.core.model.ResponseInfo;
import org.eclipse.lyo.oslc4j.core.model.ResponseInfoArray;
import org.eclipse.lyo.oslc4j.core.model.ResponseInfoCollection;

@Provider
@Produces(OslcMediaType.APPLICATION_JSON)
@Consumes(OslcMediaType.APPLICATION_JSON)
public final class OslcRdfJsonProvider
       extends AbstractOslcRdfJsonProvider
       implements MessageBodyReader<Object>,
                  MessageBodyWriter<Object>
{
    public OslcRdfJsonProvider()
    {
        super();
    }

    @Override
    public long getSize(final Object       object,
                        final Class<?>     type,
                        final Type         genericType,
                        final Annotation[] annotation,
                        final MediaType    mediaType)
    {
        return -1;
    }

    @Override
    public boolean isWriteable(final Class<?>     type,
                               final Type         genericType,
                               final Annotation[] annotations,
                               final MediaType    mediaType)
    {
        Class<?> actualType;
        
        if (FilteredResource.class.isAssignableFrom(type) &&
            (genericType instanceof ParameterizedType))
        {
            ParameterizedType parameterizedType = (ParameterizedType)genericType;
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            
            if (actualTypeArguments.length != 1)
            {
                return false;
            }
            
            if (actualTypeArguments[0] instanceof Class<?>)
            {
                actualType = (Class<?>)actualTypeArguments[0];
            }
            else if (actualTypeArguments[0] instanceof ParameterizedType)
            {
                parameterizedType = (ParameterizedType)actualTypeArguments[0];
                actualTypeArguments = parameterizedType.getActualTypeArguments();
                
                if (actualTypeArguments.length != 1 ||
                    ! (actualTypeArguments[0] instanceof Class<?>))
                {
                    return false;
                }
                
                actualType = (Class<?>)actualTypeArguments[0];
            }
            else if (actualTypeArguments[0] instanceof GenericArrayType)
            {
                GenericArrayType genericArrayType =
                    (GenericArrayType)actualTypeArguments[0];
                Type componentType = genericArrayType.getGenericComponentType();
                
                if (! (componentType instanceof Class<?>))
                {
                    return false;
                }
                
                actualType = (Class<?>)componentType;
            }
            else
            {
                return false;
            }
        }
        else
        {
            actualType = type;
        }
        
        return isWriteable(actualType,
                           annotations,
                           OslcMediaType.APPLICATION_JSON_TYPE,
                           mediaType);
    }

    @Override
    public void writeTo(final Object                         object,
                        final Class<?>                       type,
                        final Type                           genericType,
                        final Annotation[]                   annotations,
                        final MediaType                      mediaType,
                        final MultivaluedMap<String, Object> map,
                        final OutputStream                   outputStream)
           throws IOException,
                  WebApplicationException
    {
        Object[]                        objects;
        Map<String, Object>             properties = null;
        String                          descriptionURI = null;
        String                          responseInfoURI = null;
        ResponseInfo<?>					responseInfo = null;
        
        if (object instanceof FilteredResource<?>)
        {
            final FilteredResource<?> filteredResource =
                (FilteredResource<?>)object;
            
            properties = filteredResource.properties();
            
            if (filteredResource instanceof ResponseInfo<?>)
            {
                descriptionURI =  OSLC4JUtils.resolveURI(httpServletRequest, true);
                responseInfoURI = descriptionURI;
                
                final String queryString = httpServletRequest.getQueryString();
                
                if ((queryString != null) &&
                    (isOslcQuery(queryString)))
                {
                    responseInfoURI += "?" + queryString;
                }
                
                if (filteredResource instanceof ResponseInfoArray<?>)
                {
                    objects = ((ResponseInfoArray<?>)filteredResource).array();
                }
                else
                {
                    Collection<?> collection =
                        ((ResponseInfoCollection<?>)filteredResource).collection();
                    
                    objects = collection.toArray(new Object[collection.size()]);
                }
                
                responseInfo = (ResponseInfo<?>)filteredResource;
            }
            else
            {
                Object nestedObject = filteredResource.resource();
                
                if (nestedObject instanceof Object[])
                {
                    objects = (Object[])nestedObject;
                }
                else if (nestedObject instanceof Collection<?>)
                {
                    objects = ((Collection<?>)nestedObject).toArray();
                }
                else
                {
                    objects = new Object[] { object };
                }
            }
        }
        else
        {
            objects = new Object[] { object };
        }
        
        writeTo(objects,
                mediaType,
                map,
                outputStream,
                properties,
                descriptionURI,
                responseInfoURI,
                responseInfo);
    }

    @Override
    public boolean isReadable(final Class<?>     type,
                              final Type         genericType,
                              final Annotation[] annotations,
                              final MediaType    mediaType)
    {
        return isReadable(type,
                          OslcMediaType.APPLICATION_JSON_TYPE,
                          mediaType);
    }

    @Override
    public Object readFrom(final Class<Object>                  type,
                           final Type                           genericType,
                           final Annotation[]                   annotations,
                           final MediaType                      mediaType,
                           final MultivaluedMap<String, String> map,
                           final InputStream                    inputStream)
           throws IOException,
                  WebApplicationException
    {
        final Object[] objects = readFrom(type,
                                          mediaType,
                                          map,
                                          inputStream);

        if ((objects != null) &&
            (objects.length > 0))
        {
            return objects[0];
        }

        return null;
    }
}