Java Code Examples for org.apache.cxf.jaxrs.utils.HttpUtils#getSetEncoding()

The following examples show how to use org.apache.cxf.jaxrs.utils.HttpUtils#getSetEncoding() . 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: StringTextProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void writeTo(String obj, Class<?> type, Type genType, Annotation[] anns,
                    MediaType mt, MultivaluedMap<String, Object> headers,
                    OutputStream os) throws IOException {
    String encoding = HttpUtils.getSetEncoding(mt, headers, StandardCharsets.UTF_8.name());
    //REVISIT try to avoid instantiating the whole byte array
    byte[] bytes = obj.getBytes(encoding);
    if (bytes.length > bufferSize) {
        int pos = 0;
        while (pos < bytes.length) {
            int bl = bytes.length - pos;
            if (bl > bufferSize) {
                bl = bufferSize;
            }
            os.write(bytes, pos, bl);
            pos += bl;
        }
    } else {
        os.write(bytes);
    }
}
 
Example 2
Source File: AegisElementProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void writeTo(T obj, Class<?> type, Type genericType, Annotation[] anns,
    MediaType m, MultivaluedMap<String, Object> headers, OutputStream os)
    throws IOException {
    if (type == null) {
        type = obj.getClass();
    }
    if (genericType == null) {
        genericType = type;
    }
    AegisContext context = getAegisContext(type, genericType);
    AegisType aegisType = context.getTypeMapping().getType(genericType);
    AegisWriter<XMLStreamWriter> aegisWriter = context.createXMLStreamWriter();
    try {
        String enc = HttpUtils.getSetEncoding(m, headers, StandardCharsets.UTF_8.name());
        XMLStreamWriter xmlStreamWriter = createStreamWriter(aegisType.getSchemaType(), enc, os);
        // use type qname as element qname?
        xmlStreamWriter.writeStartDocument();
        aegisWriter.write(obj, aegisType.getSchemaType(), false, xmlStreamWriter, aegisType);
        xmlStreamWriter.writeEndDocument();
        xmlStreamWriter.close();
    } catch (Exception e) {
        throw ExceptionUtils.toInternalServerErrorException(e, null);
    }
}
 
Example 3
Source File: PrimitiveTextProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void writeTo(T obj, Class<?> type, Type genType, Annotation[] anns,
                    MediaType mt, MultivaluedMap<String, Object> headers,
                    OutputStream os) throws IOException {
    String encoding = HttpUtils.getSetEncoding(mt, headers, StandardCharsets.UTF_8.name());
    byte[] bytes = obj.toString().getBytes(encoding);
    os.write(bytes);

}
 
Example 4
Source File: DataBindingProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void writeTo(T o, Class<?> clazz, Type genericType, Annotation[] annotations,
                    MediaType m, MultivaluedMap<String, Object> headers, OutputStream os)
    throws IOException {
    XMLStreamWriter writer = null;
    try {
        String enc = HttpUtils.getSetEncoding(m, headers, StandardCharsets.UTF_8.name());
        writer = createWriter(clazz, genericType, enc, os);
        writeToWriter(writer, o);
    } catch (Exception ex) {
        throw ExceptionUtils.toInternalServerErrorException(ex, null);
    } finally {
        StaxUtils.close(writer);
    }
}
 
Example 5
Source File: FormEncodingProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void writeTo(T obj, Class<?> c, Type t, Annotation[] anns,
                    MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os)
    throws IOException, WebApplicationException {

    MultivaluedMap<String, String> map =
        (MultivaluedMap<String, String>)(obj instanceof Form ? ((Form)obj).asMap() : obj);
    boolean encoded = keepEncoded(anns);

    String enc = HttpUtils.getSetEncoding(mt, headers, StandardCharsets.UTF_8.name());

    FormUtils.writeMapToOutputStream(map, os, enc, encoded);

}