Search in sources :

Example 1 with ConvertException

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

the class HttpResponseImplTest method testSetObjectByServiceName.

/**
 * HttpResponseのストリームを設定、取得するテスト。
 * <p>
 * 条件:
 * <ul>
 * <li>XMLストリームデータをデータセットに変換して取得する</li>
 * <li>次の内容をDataSetXMLConverterで変換する<BR>
 * <PRE>
 * <?xml version="1.0" encoding="UTF-8"?>
 *  <dataSet>
 *   <schema>
 *    <header name="TestHeader">
 *     :A,java.util.Date,
 *     "jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format="yyyy-MM-DD"}",
 *     "jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format="yyyy-MM-DD"}",
 *     "@value@ != null"
 *     :B,java.lang.String,,,
 *    </header>
 *   </schema>
 *    <header name="TestHeader"><A>2008-01-28</A><B>TestValue</B></header></dataSet>
 * <PRE></li>
 * <li>上記のストリームデータとコンバータをプロパティに設定して、<BR>
 * HttpResponse#getObjectを実行</li>
 * </ul>
 * 確認:
 * <ul>
 * <li>正しい値が返ってくることを確認</li>
 * </ul>
 */
public void testSetObjectByServiceName() {
    try {
        // コンバータサービスの定義ファイルをロード
        if (!ServiceManagerFactory.loadManager("jp/ossc/nimbus/service/http/httpclient/service-conv.xml")) {
            System.exit(-1);
        }
        HttpResponseImpl res = new HttpResponseImpl();
        // CharacterEncodingを設定しておく
        res.headerMap = new HashMap();
        String[] vals = new String[] { "application/xml;charset=Shift_JIS" };
        res.headerMap.put("Content-Type", vals);
        String inxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<dataSet><schema><header name=\"TestHeader\">" + ":A,java.util.Date," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format=\"yyyy-MM-DD\"}\"," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format=\"yyyy-MM-DD\"}\"," + "\"@value@ != null\"\n:B,java.lang.String,,," + "</header></schema><header name=\"TestHeader\">" + "<A>2008-01-28</A><B>TestValue</B></header></dataSet>";
        // 入力ストリームとコンバーターサービス名をプロパティにセット
        InputStream is = new ByteArrayInputStream(inxml.getBytes());
        res.setInputStream(is);
        ServiceName name = new ServiceName("DataSetXMLConverter");
        res.setStreamConverterServiceName(name);
        // 
        DataSet dataset = (DataSet) res.getObject();
        assertEquals("TestHeader", dataset.getHeader("TestHeader").getName());
        assertEquals(":A,java.util.Date," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format=\"yyyy-MM-DD\"}\"," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format=\"yyyy-MM-DD\"}\"," + "\"@value@ != null\"\n:B,java.lang.String,,,", dataset.getHeader("TestHeader").getSchema());
        assertEquals("2008-01-28", dataset.getHeader("TestHeader").getFormatProperty("A"));
        assertEquals("TestValue", dataset.getHeader("TestHeader").getProperty("B"));
    } catch (PropertySchemaDefineException e) {
        e.printStackTrace();
        fail("例外発生");
    } catch (ConvertException e) {
        e.printStackTrace();
        fail("例外発生");
    } finally {
        ServiceManagerFactory.unloadManager("jp/ossc/nimbus/service/http/httpclient/service-conv.xml");
    }
}
Also used : PropertySchemaDefineException(jp.ossc.nimbus.beans.dataset.PropertySchemaDefineException) ConvertException(jp.ossc.nimbus.util.converter.ConvertException) ServiceName(jp.ossc.nimbus.core.ServiceName) DataSet(jp.ossc.nimbus.beans.dataset.DataSet)

Example 2 with ConvertException

use of jp.ossc.nimbus.util.converter.ConvertException 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 3 with ConvertException

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

the class BeanFlowRestServerService method processWriteResponseBody.

protected boolean processWriteResponseBody(RestRequest request, RestResponse response, List paths, ResourceMetaData resource) throws Exception {
    Object responseObject = response.getResponseObject();
    if (responseObject == null) {
        return true;
    }
    if (journal != null) {
        journal.addInfo(JOURNAL_KEY_RESPONSE_OBJECT, responseObject);
    }
    String acceptStr = request.request.getHeader(HTTP_HEADER_NAME_ACCEPT);
    StreamConverter responseConverter = null;
    String mediaType = null;
    if (acceptStr == null) {
        mediaType = (String) responseConverterMapping.keySet().iterator().next();
        responseConverter = (StreamConverter) responseConverterMapping.get(mediaType);
    } else {
        Accept accept = null;
        try {
            accept = new Accept(acceptStr);
        } catch (IllegalArgumentException e) {
        // 事前にチェックするため起こらないはず
        }
        for (int i = 0; i < accept.mediaRanges.size(); i++) {
            MediaRange mr = (MediaRange) accept.mediaRanges.get(i);
            responseConverter = (StreamConverter) responseConverterMapping.get(mr.getMediaType());
            if (responseConverter != null) {
                mediaType = mr.getMediaType();
                break;
            }
        }
    }
    String encode = null;
    if (responseConverter instanceof StreamStringConverter) {
        if (response.getResponse().getCharacterEncoding() != null && !response.getResponse().getCharacterEncoding().equals(((StreamStringConverter) responseConverter).getCharacterEncodingToStream())) {
            responseConverter = ((StreamStringConverter) responseConverter).cloneCharacterEncodingToStream(response.getResponse().getCharacterEncoding());
        }
        encode = ((StreamStringConverter) responseConverter).getCharacterEncodingToStream();
    }
    response.getResponse().setContentType(new ContentType(mediaType, response.getResponse().getCharacterEncoding()).toString());
    InputStream is = null;
    try {
        is = responseConverter.convertToStream(responseObject);
    } catch (ConvertException e) {
        getLogger().write("BFRS_00024", new Object[] { resource.resourcePath.path, responseObject.getClass().getName() }, e);
        if (journal != null) {
            journal.addInfo(JOURNAL_KEY_EXCEPTION, e);
        }
        response.setResult(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return false;
    }
    final ServletOutputStream sos = response.getResponse().getOutputStream();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] bytes = new byte[2048];
    int readLen = 0;
    while ((readLen = is.read(bytes)) != -1) {
        baos.write(bytes, 0, readLen);
        sos.write(bytes, 0, readLen);
    }
    if (journal != null) {
        Object responseBody = null;
        if (encode == null) {
            responseBody = baos.toByteArray();
        } else {
            try {
                responseBody = new String(baos.toByteArray(), encode);
            } catch (UnsupportedEncodingException e) {
                responseBody = baos.toByteArray();
            }
        }
        journal.addInfo(JOURNAL_KEY_RESPONSE_BODY, responseBody);
    }
    return true;
}
Also used : ConvertException(jp.ossc.nimbus.util.converter.ConvertException) ServletOutputStream(javax.servlet.ServletOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamConverter(jp.ossc.nimbus.util.converter.StreamConverter) BindingStreamConverter(jp.ossc.nimbus.util.converter.BindingStreamConverter) StreamStringConverter(jp.ossc.nimbus.util.converter.StreamStringConverter)

Example 4 with ConvertException

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

the class HttpResponseImplTest method testSetObject.

/**
 * HttpResponseのストリームを設定、取得するテスト。
 * <p>
 * 条件:
 * <ul>
 * <li>XMLストリームデータをデータセットに変換して取得する</li>
 * <li>次の内容をDataSetXMLConverterで変換する<BR>
 * <PRE>
 * <?xml version="1.0" encoding="UTF-8"?>
 *  <dataSet>
 *   <schema>
 *    <header name="TestHeader">
 *     :A,java.util.Date,
 *     "jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format="yyyy-MM-DD"}",
 *     "jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format="yyyy-MM-DD"}",
 *     "@value@ != null"
 *     :B,java.lang.String,,,
 *    </header>
 *   </schema>
 *    <header name="TestHeader"><A>2008-01-28</A><B>TestValue</B></header></dataSet>
 * <PRE></li>
 * <li>上記のストリームデータとコンバータをプロパティに設定して、<BR>
 * HttpResponse#getObjectを実行</li>
 * </ul>
 * 確認:
 * <ul>
 * <li>正しい値が返ってくることを確認</li>
 * </ul>
 */
public void testSetObject() {
    try {
        HttpResponseImpl res = new HttpResponseImpl();
        // CharacterEncodingを設定しておく
        res.headerMap = new HashMap();
        String[] vals = new String[] { "application/xml;charset=Shift_JIS" };
        res.headerMap.put("Content-Type", vals);
        String inxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<dataSet><schema><header name=\"TestHeader\">" + ":A,java.util.Date," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format=\"yyyy-MM-DD\"}\"," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format=\"yyyy-MM-DD\"}\"," + "\"@value@ != null\"\n:B,java.lang.String,,," + "</header></schema><header name=\"TestHeader\">" + "<A>2008-01-28</A><B>TestValue</B></header></dataSet>";
        // 入力ストリームとコンバーターをプロパティにセット
        InputStream is = new ByteArrayInputStream(inxml.getBytes());
        res.setInputStream(is);
        DataSetXMLConverter conv = new DataSetXMLConverter(DataSetXMLConverter.XML_TO_DATASET);
        res.setStreamConverter(conv);
        // 
        DataSet dataset = (DataSet) res.getObject();
        assertEquals("TestHeader", dataset.getHeader("TestHeader").getName());
        assertEquals(":A,java.util.Date," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format=\"yyyy-MM-DD\"}\"," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format=\"yyyy-MM-DD\"}\"," + "\"@value@ != null\"\n:B,java.lang.String,,,", dataset.getHeader("TestHeader").getSchema());
        assertEquals("2008-01-28", dataset.getHeader("TestHeader").getFormatProperty("A"));
        assertEquals("TestValue", dataset.getHeader("TestHeader").getProperty("B"));
    } catch (PropertySchemaDefineException e) {
        e.printStackTrace();
        fail("例外発生");
    } catch (ConvertException e) {
        e.printStackTrace();
        fail("例外発生");
    }
}
Also used : PropertySchemaDefineException(jp.ossc.nimbus.beans.dataset.PropertySchemaDefineException) ConvertException(jp.ossc.nimbus.util.converter.ConvertException) DataSet(jp.ossc.nimbus.beans.dataset.DataSet) DataSetXMLConverter(jp.ossc.nimbus.util.converter.DataSetXMLConverter)

Example 5 with ConvertException

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

the class RecordListCodeMasterConverter method convert.

public Object convert(Object master, Object obj) throws ConvertException {
    if (!(master instanceof RecordList)) {
        throw new ConvertException("CodeMaster is not RecordList!");
    }
    final RecordList recList = (RecordList) master;
    Record record = null;
    try {
        if (obj instanceof DynamicCondition) {
            final DynamicCondition cnd = (DynamicCondition) obj;
            final List list = recList.createView().searchKeyElement(cnd.conditionName, null).getResultList();
            final Iterator itr = list.iterator();
            if (itr.hasNext()) {
                record = (Record) itr.next();
            }
        } else if (obj instanceof DynamicConditionReal) {
            final DynamicConditionReal cnd = (DynamicConditionReal) obj;
            if (cnd.condition == null) {
                throw new ConvertException("DynamicConditionReal.condition is null.");
            }
            final RecordList list = recList.realSearch(cnd.condition, cnd.valueMap);
            list.sort(cnd.orderBy, cnd.isAsc);
            final Iterator itr = list.iterator();
            if (itr.hasNext()) {
                record = (Record) itr.next();
            }
        } else if (obj instanceof DynamicKey) {
            final DynamicKey cnd = (DynamicKey) obj;
            Record key = null;
            if (cnd.keyMap != null) {
                if (cnd.keyMap.size() == 0) {
                    if (isThrowOnNullKey) {
                        throw new ConvertException("DynamicKey.keyMap is empty.");
                    }
                    return nullKeyObject;
                }
                key = recList.createRecord();
                Iterator entries = cnd.keyMap.entrySet().iterator();
                while (entries.hasNext()) {
                    Map.Entry entry = (Map.Entry) entries.next();
                    String propName = (String) entry.getKey();
                    Object propValue = entry.getValue();
                    int index = recList.getRecordSchema().getPropertyIndex(propName);
                    if (index == -1) {
                        throw new ConvertException("Property '" + propName + "' is not found.");
                    }
                    key.setProperty(index, propValue);
                }
            } else {
                if (cnd.keyRecord == null) {
                    if (isThrowOnNullKey) {
                        throw new ConvertException("DynamicKey.keyRecord is null.");
                    }
                    return nullKeyObject;
                }
                key = cnd.keyRecord;
            }
            final List list = recList.createView().searchByElement(key, cnd.conditionName, null).getResultList(cnd.orderBy, cnd.isAsc);
            if (list != null && list.size() > 0) {
                record = (Record) list.get(0);
            }
        } else if (obj instanceof Key) {
            final Key cnd = (Key) obj;
            if (cnd.key != null) {
                PropertySchema[] schemata = recList.getRecordSchema().getPrimaryKeyPropertySchemata();
                if (schemata == null || schemata.length != 1) {
                    throw new ConvertException("Size of primary key property not equal 1.");
                }
                Record key = recList.createRecord();
                key.setProperty(schemata[0].getName(), cnd.key);
                record = recList.searchByPrimaryKey(key);
            } else {
                Record key = null;
                if (cnd.keyMap != null) {
                    if (cnd.keyMap.size() == 0) {
                        if (isThrowOnNullKey) {
                            throw new ConvertException("Key.keyMap is empty.");
                        }
                        return nullKeyObject;
                    }
                    key = recList.createRecord();
                    Iterator entries = cnd.keyMap.entrySet().iterator();
                    while (entries.hasNext()) {
                        Map.Entry entry = (Map.Entry) entries.next();
                        String propName = (String) entry.getKey();
                        Object propValue = entry.getValue();
                        int index = recList.getRecordSchema().getPropertyIndex(propName);
                        if (index == -1) {
                            throw new ConvertException("Property '" + propName + "' is not found.");
                        }
                        key.setProperty(index, propValue);
                    }
                } else {
                    if (cnd.keyRecord == null) {
                        if (isThrowOnNullKey) {
                            throw new ConvertException("Key.keyRecord is null.");
                        }
                        return nullKeyObject;
                    }
                    key = cnd.keyRecord;
                }
                record = recList.searchByPrimaryKey(key);
            }
        } else {
            if (obj == null) {
                if (isThrowOnNullKey) {
                    throw new ConvertException("Key is null.");
                }
                return nullKeyObject;
            }
            if (obj instanceof Record) {
                record = recList.searchByPrimaryKey((Record) obj);
            } else {
                PropertySchema[] schemata = recList.getRecordSchema().getPrimaryKeyPropertySchemata();
                if (schemata == null || schemata.length != 1) {
                    throw new ConvertException("Size of primary key property not equal 1.");
                }
                Record key = recList.createRecord();
                key.setProperty(schemata[0].getName(), obj);
                record = recList.searchByPrimaryKey(key);
            }
        }
    } catch (ConvertException e) {
        throw e;
    } catch (Exception e) {
        throw new ConvertException(e);
    }
    if (record == null) {
        if (isThrowOnNotFound) {
            throw new ConvertException("Record is not found. key=" + obj);
        }
        return notFoundObject;
    }
    return valueFieldName == null ? record : record.get(valueFieldName);
}
Also used : ConvertException(jp.ossc.nimbus.util.converter.ConvertException) ConvertException(jp.ossc.nimbus.util.converter.ConvertException)

Aggregations

ConvertException (jp.ossc.nimbus.util.converter.ConvertException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ServletInputStream (javax.servlet.ServletInputStream)2 DataSet (jp.ossc.nimbus.beans.dataset.DataSet)2 PropertySchemaDefineException (jp.ossc.nimbus.beans.dataset.PropertySchemaDefineException)2 BindingStreamConverter (jp.ossc.nimbus.util.converter.BindingStreamConverter)2 StreamStringConverter (jp.ossc.nimbus.util.converter.StreamStringConverter)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 InflaterInputStream (java.util.zip.InflaterInputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 ServiceName (jp.ossc.nimbus.core.ServiceName)1 DataSetXMLConverter (jp.ossc.nimbus.util.converter.DataSetXMLConverter)1 StreamConverter (jp.ossc.nimbus.util.converter.StreamConverter)1