/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *  
 *******************************************************************************/
package org.apache.wink.providers.json;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

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

import org.apache.wink.common.RestConstants;
import org.apache.wink.common.internal.i18n.Messages;
import org.apache.wink.common.internal.utils.MediaTypeUtils;
import org.apache.wink.common.utils.ProviderUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Provider
@Consumes( {MediaType.APPLICATION_JSON, MediaTypeUtils.JAVASCRIPT})
@Produces( {MediaType.APPLICATION_JSON, MediaTypeUtils.JAVASCRIPT})
public class JsonArrayProvider implements MessageBodyWriter<JSONArray>,
    MessageBodyReader<JSONArray> {

    private static final Logger logger = LoggerFactory.getLogger(JsonProvider.class);

    @Context
    private UriInfo             uriInfo;

    public long getSize(JSONArray t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType) {
        return -1;
    }

    public boolean isWriteable(Class<?> type,
                               Type genericType,
                               Annotation[] annotations,
                               MediaType mediaType) {
        return type == JSONArray.class;
    }

    public void writeTo(JSONArray t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream) throws IOException, WebApplicationException {
        String jsonString = null;
        try {
            jsonString = t.toString(2);
        } catch (JSONException e) {
            logger.error(Messages.getMessage("jsonFailWriteJSONArray"));
            throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
        }

        String callbackParam =
            uriInfo.getQueryParameters().getFirst(RestConstants.REST_PARAM_JSON_CALLBACK);
        OutputStreamWriter writer =
            new OutputStreamWriter(entityStream, ProviderUtils.getCharset(mediaType));
        if (callbackParam != null) {
            writer.write(callbackParam);
            writer.write("(");
        }
        writer.write(jsonString);
        if (callbackParam != null) {
            writer.write(")");
        }
        writer.flush();
    }

    public boolean isReadable(Class<?> type,
                              Type genericType,
                              Annotation[] annotations,
                              MediaType mediaType) {
        return type == JSONArray.class;
    }

    public JSONArray readFrom(Class<JSONArray> type,
                              Type genericType,
                              Annotation[] annotations,
                              MediaType mediaType,
                              MultivaluedMap<String, String> httpHeaders,
                              InputStream entityStream) throws IOException, WebApplicationException {
        try {
            return new JSONArray(new JSONTokener(ProviderUtils
                .createReader(entityStream, mediaType)));
        } catch (JSONException e) {
            logger.error(Messages.getMessage("jsonFailReadJSONArray"));
            throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
        }
    }

}

@@ -3551,17 +3551,69 @@
 kParam =
-%0A
+ null;%0A        try %7B%0A            callbackParam =%0A    
         
@@ -3695,16 +3695,127 @@
 LBACK);%0A
+        %7D catch (Exception e) %7B%0A            logger.debug(%22Could not get the URI callback param%22, e);%0A        %7D%0A
         

Add try catch for JSONArray uriinfo for client