/**
* Descriptions DoSomethingAPIサーブレット
*
* @version xxxx-xx-xx
* @author xxxx
* @since JDK1.6
*
*/
public class DoSomethingAPI extends HttpServlet {
private static final long serialVersionUID = -6628892825606366902L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// リクエストパラメータ取得
String uid = request.getParameter( "userid" );
// ユーザーステータスマップ取得
Map<String, String> userStatusMap = new HashMap<String, String>();
try {
// 応答用データ取得
userStatusMap = UserHelper.getUserStatus( uid );
} catch ( Exception e ) {
e.printStackTrace();
response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
return;
}
// 応答XML生成
String responseString = createResponseXmlString( userStatusMap );
if ( responseString == null ) {
response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
return;
}
// 応答返却
response.setContentType( "text/xml; charset=UTF-8" );
PrintWriter printWriter = response.getWriter();
printWriter.print( responseString );
}
/*-------------------------------------------------------------------------
/ メソッド名 : createResponseXmlString
/------------------------------------------------------------------------*/
/**
* アップロード通知応答用のXMLを生成し、文字列として返却します。
* @return 変換後XML文字列
* @param responseMap 応答情報Map
*/
public String createResponseXmlString( Map<String, String> responseMap ) {
String retString = null;
// XMLデータ生成
try {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element responseElement = document.createElement( "result" );
String nickname = responseMap.get( "nickname" );
// nickname生成
Element nicknameElement = document.createElement( "nickname" );
nicknameElement.appendChild( document.createTextNode( nickname ) );
responseElement.appendChild( nicknameElement );
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
transformer.setOutputProperty("version","1.0");
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
DOMSource source = new DOMSource( responseElement );
StringWriter strReturn = new StringWriter();
transformer.transform( source, new StreamResult( strReturn ) );
retString = strReturn.toString();
} catch ( Exception e ) {
e.printStackTrace();
}
return retString;
}
}