com.alibaba.fastjson.JSONPObject Java Examples

The following examples show how to use com.alibaba.fastjson.JSONPObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSONPResponseBodyAdvice.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                              Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
                              ServerHttpResponse response) {

    ResponseJSONP responseJsonp = returnType.getMethodAnnotation(ResponseJSONP.class);
    if(responseJsonp == null){
        responseJsonp = returnType.getContainingClass().getAnnotation(ResponseJSONP.class);
    }

    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    String callbackMethodName = servletRequest.getParameter(responseJsonp.callback());

    if (!IOUtils.isValidJsonpQueryParam(callbackMethodName)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Invalid jsonp parameter value:" + callbackMethodName);
        }
        callbackMethodName = null;
    }

    JSONPObject jsonpObject = new JSONPObject(callbackMethodName);
    jsonpObject.addParameter(body);
    beforeBodyWriteInternal(jsonpObject, selectedContentType, returnType, request, response);
    return jsonpObject;
}
 
Example #2
Source File: FastJsonJsonView.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@Override
protected void renderMergedOutputModel(Map<String, Object> model, //
                                       HttpServletRequest request, //
                                       HttpServletResponse response) throws Exception {
    Object value = filterModel(model);
    String jsonpParameterValue = getJsonpParameterValue(request);
    if(jsonpParameterValue != null) {
        JSONPObject jsonpObject = new JSONPObject(jsonpParameterValue);
        jsonpObject.addParameter(value);
        value = jsonpObject;
    }

    ByteArrayOutputStream outnew = new ByteArrayOutputStream();

    int len = JSON.writeJSONString(outnew, //
            fastJsonConfig.getCharset(), //
            value, //
            fastJsonConfig.getSerializeConfig(), //
            fastJsonConfig.getSerializeFilters(), //
            fastJsonConfig.getDateFormat(), //
            JSON.DEFAULT_GENERATE_FEATURE, //
            fastJsonConfig.getSerializerFeatures());

    if (this.updateContentLength) {
        // Write content length (determined via byte array).
        response.setContentLength(len);
    }

    // Flush byte array to servlet output stream.
    ServletOutputStream out = response.getOutputStream();
    outnew.writeTo(out);
    outnew.close();
    out.flush();
}
 
Example #3
Source File: JsonMessageConverter.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    try (ByteArrayOutputStream outnew = new ByteArrayOutputStream()) {
        HttpHeaders headers = outputMessage.getHeaders();

        //获取全局配置的filter
        SerializeFilter[] globalFilters = getFastJsonConfig().getSerializeFilters();
        List<SerializeFilter> allFilters = new ArrayList<>(Arrays.asList(globalFilters));

        boolean isJsonp = false;
        Object value = strangeCodeForJackson(object);

        if (value instanceof FastJsonContainer) {
            FastJsonContainer fastJsonContainer = (FastJsonContainer) value;
            PropertyPreFilters filters = fastJsonContainer.getFilters();
            allFilters.addAll(filters.getFilters());
            value = fastJsonContainer.getValue();
        }

        if (value instanceof MappingFastJsonValue) {
            isJsonp = true;
            value = ((MappingFastJsonValue) value).getValue();
        } else if (value instanceof JSONPObject) {
            isJsonp = true;
        }


        int len = writePrefix(outnew, object);
        len += JSON.writeJSONString(outnew, //
                getFastJsonConfig().getCharset(), //
                value, //
                getFastJsonConfig().getSerializeConfig(), //
                allFilters.toArray(new SerializeFilter[allFilters.size()]),
                getFastJsonConfig().getDateFormat(), //
                JSON.DEFAULT_GENERATE_FEATURE, //
                getFastJsonConfig().getSerializerFeatures());
        len += writeSuffix(outnew, object);

        if (isJsonp) {
            headers.setContentType(APPLICATION_JAVASCRIPT);
        }
        if (getFastJsonConfig().isWriteContentLength()) {
            headers.setContentLength(len);
        }

        headers.set("carrera_logid", RequestContext.getLogId());
        RequestContext.sendJsonResponse(outnew.toString());

        outnew.writeTo(outputMessage.getBody());

    } catch (JSONException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}
 
Example #4
Source File: JsonMessageConverter.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    try (ByteArrayOutputStream outnew = new ByteArrayOutputStream()) {
        HttpHeaders headers = outputMessage.getHeaders();

        //获取全局配置的filter
        SerializeFilter[] globalFilters = getFastJsonConfig().getSerializeFilters();
        List<SerializeFilter> allFilters = new ArrayList<>(Arrays.asList(globalFilters));

        boolean isJsonp = false;
        Object value = strangeCodeForJackson(object);

        if (value instanceof FastJsonContainer) {
            FastJsonContainer fastJsonContainer = (FastJsonContainer) value;
            PropertyPreFilters filters = fastJsonContainer.getFilters();
            allFilters.addAll(filters.getFilters());
            value = fastJsonContainer.getValue();
        }

        if (value instanceof MappingFastJsonValue) {
            isJsonp = true;
            value = ((MappingFastJsonValue) value).getValue();
        } else if (value instanceof JSONPObject) {
            isJsonp = true;
        }


        int len = writePrefix(outnew, object);
        len += JSON.writeJSONString(outnew, //
                getFastJsonConfig().getCharset(), //
                value, //
                getFastJsonConfig().getSerializeConfig(), //
                allFilters.toArray(new SerializeFilter[allFilters.size()]),
                getFastJsonConfig().getDateFormat(), //
                JSON.DEFAULT_GENERATE_FEATURE, //
                getFastJsonConfig().getSerializerFeatures());
        len += writeSuffix(outnew, object);

        if (isJsonp) {
            headers.setContentType(APPLICATION_JAVASCRIPT);
        }
        if (getFastJsonConfig().isWriteContentLength()) {
            headers.setContentLength(len);
        }

        headers.set("carrera_logid", RequestContext.getLogId());
        RequestContext.sendJsonResponse(outnew.toString());

        outnew.writeTo(outputMessage.getBody());

    } catch (JSONException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}
 
Example #5
Source File: JSONPDeserializer.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONLexerBase lexer = (JSONLexerBase) parser.getLexer();

    SymbolTable symbolTable = parser.getSymbolTable();

    String funcName = lexer.scanSymbolUnQuoted(symbolTable);
    lexer.nextToken();

    int tok = lexer.token();

    if (tok == JSONToken.DOT) {
        String name = lexer.scanSymbolUnQuoted(parser.getSymbolTable());
        funcName += ".";
        funcName += name;
        lexer.nextToken();
        tok = lexer.token();
    }

    JSONPObject jsonp = new JSONPObject(funcName);

    if (tok != JSONToken.LPAREN) {
        throw new JSONException("illegal jsonp : " + lexer.info());
    }
    lexer.nextToken();
    for (;;) {
        Object arg = parser.parse();
        jsonp.addParameter(arg);

        tok = lexer.token();
        if (tok == JSONToken.COMMA) {
            lexer.nextToken();
        } else if (tok == JSONToken.RPAREN) {
            lexer.nextToken();
            break;
        } else {
            throw new JSONException("illegal jsonp : " + lexer.info());
        }
     }
    tok = lexer.token();
    if (tok == JSONToken.SEMI) {
        lexer.nextToken();
    }

    return (T) jsonp;
}
 
Example #6
Source File: FastJsonHttpMessageConverter.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {

    ByteArrayOutputStream outnew = new ByteArrayOutputStream();
    try {
        HttpHeaders headers = outputMessage.getHeaders();

        //获取全局配置的filter
        SerializeFilter[] globalFilters = fastJsonConfig.getSerializeFilters();
        List<SerializeFilter> allFilters = new ArrayList<SerializeFilter>(Arrays.asList(globalFilters));

        boolean isJsonp = false;

        //不知道为什么会有这行代码, 但是为了保持和原来的行为一致,还是保留下来
        Object value = strangeCodeForJackson(object);

        if (value instanceof FastJsonContainer) {
            FastJsonContainer fastJsonContainer = (FastJsonContainer) value;
            PropertyPreFilters filters = fastJsonContainer.getFilters();
            allFilters.addAll(filters.getFilters());
            value = fastJsonContainer.getValue();
        }

        //revise 2017-10-23 ,
        // 保持原有的MappingFastJsonValue对象的contentType不做修改 保持旧版兼容。
        // 但是新的JSONPObject将返回标准的contentType:application/javascript ,不对是否有function进行判断
        if (value instanceof MappingFastJsonValue) {
            if(!StringUtils.isEmpty(((MappingFastJsonValue) value).getJsonpFunction())){
                isJsonp = true;
            }
        } else if (value instanceof JSONPObject) {
            isJsonp = true;
        }


        int len = JSON.writeJSONString(outnew, //
                fastJsonConfig.getCharset(), //
                value, //
                fastJsonConfig.getSerializeConfig(), //
                //fastJsonConfig.getSerializeFilters(), //
                allFilters.toArray(new SerializeFilter[allFilters.size()]),
                fastJsonConfig.getDateFormat(), //
                JSON.DEFAULT_GENERATE_FEATURE, //
                fastJsonConfig.getSerializerFeatures());

        if (isJsonp) {
            headers.setContentType(APPLICATION_JAVASCRIPT);
        }

        if (fastJsonConfig.isWriteContentLength()) {
            headers.setContentLength(len);
        }

        outnew.writeTo(outputMessage.getBody());

    } catch (JSONException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    } finally {
        outnew.close();
    }
}
 
Example #7
Source File: JSONPResponseBodyAdvice.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public void beforeBodyWriteInternal(JSONPObject jsonpObject, MediaType contentType,
                                    MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
    //MediaType contentTypeToUse = getContentType(contentType, request, response);
    //response.getHeaders().setContentType(contentTypeToUse);
}