axis のレスポンスで、日本語文字列がUTF-16の数値文字参照に置き換わってしまう件。
なんでいまさら axis1 なんて?というのはおいといて、他にもいろいろ問題があり、axis 関連で丸々一週間持っていかれたので・・・。恨みをこめてここに記す。(もっとマシな解決方法あったら教えてください。ぜひ)
axis の組み立てるレスポンスが、UTF-8 ではなく、UTF-16 の、しかも数値文字参照(ሴ みたいな)に置き換わってしまう、という問題。要するに Java の内部文字表現がそのまま数値文字参照に置き換えられてるわけだ。
[AXIS-840] Character entities are escaped too aggressively - ASF JIRA
[AXIS-2342] Reopen issue: Character entities are escaped too aggressively - ASF JIRA
あたりが該当の issue。
1.4 で再現。けどたぶん、1.2、1.3 でも同じ問題が出そう。
これ、Java クライアントでは内部文字表現が UTF-16 なので、(偶然)問題が発覚しないのだが、PHP4 みたいな、Java 以外の言語だと文字化けとして発火する。
とりあえず、axis の、org.apache.axis.components.encoding.UTF8Encoder を上書きすることで解決した。修正内容は
if (character < 0x20) { throw new IllegalArgumentException(Messages.getMessage( "invalidXmlCharacter00", Integer.toHexString(character), xmlString.substring(0, i))); } else if (character > 0x7F) { writer.write("&#x"); writer.write(Integer.toHexString(character).toUpperCase()); writer.write(";"); } else { writer.write(character); }
の、 else if 部が諸悪の根源なので、ここをコメントアウトするだけ。全ソースを以下に貼り付けておく。
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed 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.axis.components.encoding; import org.apache.axis.i18n.Messages; import java.io.IOException; import java.io.Writer; /** * UTF-8 Encoder. * * @author <a href="mailto:jens@void.fm">Jens Schumann</a> * @see <a href="http://encoding.org">encoding.org</a> * @see <a href="http://czyborra.com/utf/#UTF-8">UTF 8 explained</a> */ public class UTF8Encoder extends AbstractXMLEncoder { /** * gets the encoding supported by this encoder * * @return string */ public String getEncoding() { return XMLEncoderFactory.ENCODING_UTF_8; } /** * write the encoded version of a given string * * @param writer writer to write this string to * @param xmlString string to be encoded */ public void writeEncoded(Writer writer, String xmlString) throws IOException { if (xmlString == null) { return; } int length = xmlString.length(); char character; for (int i = 0; i < length; i++) { character = xmlString.charAt( i ); switch (character) { // we don't care about single quotes since axis will // use double quotes anyway case '&': writer.write(AMP); break; case '"': writer.write(QUOTE); break; case '<': writer.write(LESS); break; case '>': writer.write(GREATER); break; case '\n': writer.write(LF); break; case '\r': writer.write(CR); break; case '\t': writer.write(TAB); break; default: if (character < 0x20) { throw new IllegalArgumentException(Messages.getMessage( "invalidXmlCharacter00", Integer.toHexString(character), xmlString.substring(0, i))); // } else if (character > 0x7F) { // writer.write("&#x"); // writer.write(Integer.toHexString(character).toUpperCase()); // writer.write(";"); } else { writer.write(character); } break; } } } }
エンコーダーを別のもの指定するとか、なんかもうちょっと綺麗な対処方法がある気がするので、何かあればぜひご指摘くださいmm