Search in sources :

Example 1 with BindingStreamConverter

use of jp.ossc.nimbus.util.converter.BindingStreamConverter in project nimbus by nimbus-org.

the class BeanFlowRestServerService method processReadRequestBody.

protected boolean processReadRequestBody(RestRequest request, RestResponse response, List paths, ResourceMetaData resource) throws Exception {
    String contentType = request.request.getHeader(HTTP_HEADER_NAME_CONTENT_TYPE);
    BindingStreamConverter requestConverter = null;
    if (contentType == null) {
        if (requestConverterMapping != null) {
            requestConverter = (BindingStreamConverter) requestConverterMapping.values().iterator().next();
        }
    } else {
        final MediaType mediaType = new MediaType(contentType);
        if (requestConverterMapping != null) {
            requestConverter = (BindingStreamConverter) requestConverterMapping.get(mediaType.getMediaType());
        }
    }
    if (requestConverter == null) {
        return processReadParameter(request, response, paths, resource);
    } else if (request.getRequestObject() != null) {
        String encode = null;
        if (requestConverter instanceof StreamStringConverter) {
            if (request.getRequest().getCharacterEncoding() != null && !request.getRequest().getCharacterEncoding().equals(((StreamStringConverter) requestConverter).getCharacterEncodingToObject())) {
                requestConverter = (BindingStreamConverter) ((StreamStringConverter) requestConverter).cloneCharacterEncodingToObject(request.getRequest().getCharacterEncoding());
            }
            encode = ((StreamStringConverter) requestConverter).getCharacterEncodingToObject();
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayInputStream bais = null;
        try {
            final ServletInputStream sis = request.getRequest().getInputStream();
            byte[] bytes = new byte[2048];
            try {
                int size = 0;
                while ((size = sis.read(bytes)) != -1) {
                    baos.write(bytes, 0, size);
                }
            } finally {
                if (sis != null) {
                    sis.close();
                }
                bytes = null;
            }
            decompress(request.getRequest(), baos);
            if (journal != null) {
                Object requestBody = null;
                if (encode == null) {
                    requestBody = baos.toByteArray();
                } else {
                    try {
                        requestBody = new String(baos.toByteArray(), encode);
                    } catch (UnsupportedEncodingException e) {
                        requestBody = baos.toByteArray();
                    }
                }
                journal.addInfo(JOURNAL_KEY_REQUEST_BODY, requestBody);
            }
            bais = new ByteArrayInputStream(baos.toByteArray());
            baos = null;
            request.setRequestObject(requestConverter.convertToObject(bais, request.getRequestObject()));
        } catch (ConvertException e) {
            getLogger().write("BFRS_00018", new Object[] { resource.resourcePath.path, request.getRequestObject().getClass().getName() }, e);
            if (journal != null) {
                journal.addInfo(JOURNAL_KEY_EXCEPTION, e);
            }
            response.setResult(HttpServletResponse.SC_BAD_REQUEST);
            return false;
        } finally {
            if (bais != null) {
                try {
                    bais.close();
                } catch (IOException e) {
                }
            }
        }
    }
    return true;
}
Also used : BindingStreamConverter(jp.ossc.nimbus.util.converter.BindingStreamConverter) ServletInputStream(javax.servlet.ServletInputStream) ConvertException(jp.ossc.nimbus.util.converter.ConvertException) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamStringConverter(jp.ossc.nimbus.util.converter.StreamStringConverter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with BindingStreamConverter

use of jp.ossc.nimbus.util.converter.BindingStreamConverter in project nimbus by nimbus-org.

the class FileToObjectConvertActionService method execute.

/**
 * リソースの内容を読み込んで、ファイルをオブジェクトに変換する。<p>
 * リソースのフォーマットは、以下。<br>
 * <pre>
 * filePath
 * bindingObjectScript|format
 * </pre>
 * filePathは、ファイルのパスを指定する。パスは、絶対パスまたは、相対パスで指定する。<br>
 * bindingObjectScriptは、設定されている{@link StreamConverter}が、{@link BindingStreamConverter}の場合に、変換後オブジェクトとしてバインドするオブジェクトを生成するスクリプトを指定する。スクリプトは、{@link Interpreter#evaluate(String,Map)}で評価され、引数の変数マップには、"context"で{@link TestContext}、"preResult"で引数のpreResultが渡される。<br>
 * formatは、設定されている{@link FormatConverter}の{@link FormatConverter#setFormat(String) setFormat(format)}に渡すフォーマット文字列を指定する。指定しない場合は、フォーマットは設定しない。<br>
 *
 * @param context コンテキスト
 * @param actionId アクションID
 * @param preResult 変換後オブジェクトとしてバインドするオブジェクト
 * @param resource リソース
 * @return 変換後オブジェクト
 */
public Object execute(TestContext context, String actionId, Object preResult, Reader resource) throws Exception {
    BufferedReader br = new BufferedReader(resource);
    File file = null;
    String bindingObjectScript = null;
    String format = null;
    try {
        final String filePath = br.readLine();
        if (filePath == null) {
            throw new Exception("Unexpected EOF on filePath");
        }
        file = new File(filePath);
        if (!file.exists()) {
            file = new File(context.getCurrentDirectory(), filePath);
        }
        if (!file.exists()) {
            throw new Exception("File not found. filePath=" + filePath);
        }
        String line = null;
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            while ((line = br.readLine()) != null) {
                pw.println(line);
            }
            pw.flush();
            if (streamConverter != null) {
                bindingObjectScript = sw.toString();
                if (bindingObjectScript.length() == 0) {
                    bindingObjectScript = null;
                }
            } else {
                format = sw.toString();
                if (format.length() == 0) {
                    format = null;
                }
            }
        } finally {
            sw.close();
            pw.close();
        }
        if (bindingObjectScript != null) {
            if (streamConverter == null) {
                throw new UnsupportedOperationException("StreamConverter is null.");
            }
            if (interpreter == null) {
                throw new UnsupportedOperationException("Interpreter is null.");
            }
            if (!(streamConverter instanceof BindingStreamConverter)) {
                throw new Exception("StreamConverter is not BindingStreamConverter. streamConverter=" + streamConverter.getClass());
            }
        }
    } finally {
        br.close();
        br = null;
    }
    if (streamConverter != null) {
        Object bindingObject = null;
        if (bindingObjectScript != null) {
            final Map params = new HashMap();
            params.put("context", context);
            params.put("preResult", preResult);
            bindingObject = interpreter.evaluate(bindingObjectScript, params);
        } else {
            bindingObject = preResult;
        }
        FileInputStream fis = new FileInputStream(file);
        try {
            StreamConverter sc = streamConverter;
            if (fileEncoding != null && sc instanceof StreamStringConverter && !fileEncoding.equals(((StreamStringConverter) sc).getCharacterEncodingToObject())) {
                sc = ((StreamStringConverter) sc).cloneCharacterEncodingToObject(fileEncoding);
            }
            if (bindingObject != null && (sc instanceof BindingStreamConverter)) {
                return ((BindingStreamConverter) sc).convertToObject(fis, bindingObject);
            } else {
                return sc.convertToObject(fis);
            }
        } finally {
            fis.close();
        }
    } else {
        InputStreamReader isr = fileEncoding == null ? new InputStreamReader(new FileInputStream(file)) : new InputStreamReader(new FileInputStream(file), fileEncoding);
        StringWriter sw = new StringWriter();
        String str = null;
        try {
            int len = 0;
            char[] buf = new char[1024];
            while ((len = isr.read(buf, 0, buf.length)) > 0) {
                sw.write(buf, 0, len);
            }
            str = sw.toString();
        } finally {
            isr.close();
            sw.close();
        }
        FormatConverter fc = formatConverter;
        if (formatConverterServiceName != null) {
            fc = (FormatConverter) ServiceManagerFactory.getServiceObject(formatConverterServiceName);
        }
        if (format != null) {
            fc.setFormat(format);
        }
        return fc.convert(str);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) FormatConverter(jp.ossc.nimbus.util.converter.FormatConverter) FileInputStream(java.io.FileInputStream) BindingStreamConverter(jp.ossc.nimbus.util.converter.BindingStreamConverter) StringWriter(java.io.StringWriter) BufferedReader(java.io.BufferedReader) BindingStreamConverter(jp.ossc.nimbus.util.converter.BindingStreamConverter) StreamConverter(jp.ossc.nimbus.util.converter.StreamConverter) StreamStringConverter(jp.ossc.nimbus.util.converter.StreamStringConverter) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Aggregations

BindingStreamConverter (jp.ossc.nimbus.util.converter.BindingStreamConverter)2 StreamStringConverter (jp.ossc.nimbus.util.converter.StreamStringConverter)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ServletInputStream (javax.servlet.ServletInputStream)1 ConvertException (jp.ossc.nimbus.util.converter.ConvertException)1 FormatConverter (jp.ossc.nimbus.util.converter.FormatConverter)1 StreamConverter (jp.ossc.nimbus.util.converter.StreamConverter)1