/** * Method invoked to serialize the result of the invoked method and write this value to the * response. * * @param response The current response object. * @param result The object to write to response. * @param restMimeFormats */ private void serializeObjectToResponse(WebResponse response, Object result, String mimeType) { try { response.setContentType(mimeType); objSerialDeserial.objectToResponse(result, response, mimeType); } catch (Exception e) { throw new RuntimeException("Error writing object to response.", e); } }
@Override public void onResourceRequested() { // this is the callback that retrieves matching choices used to populate the dropdown Request request = getRequestCycle().getRequest(); IRequestParameters params = request.getRequestParameters(); // retrieve choices matching the search term String term = params.getParameterValue("term").toOptionalString(); int page = params.getParameterValue("page").toInt(1); // select2 uses 1-based paging, but in wicket world we are used to // 0-based page -= 1; Response<T> response = new Response<T>(); provider.query(term, page, response); // jsonize and write out the choices to the response WebResponse webResponse = (WebResponse) getRequestCycle().getResponse(); webResponse.setContentType("application/json"); OutputStreamWriter out = new OutputStreamWriter(webResponse.getOutputStream(), getRequest().getCharset()); JSONWriter json = new JSONWriter(out); try { json.object(); json.key("results").array(); for (T item : response) { json.object(); provider.toJson(item, json); json.endObject(); } json.endArray(); json.key("more").value(response.getHasMore()).endObject(); } catch (JSONException e) { throw new RuntimeException("Could not write Json response", e); } try { out.flush(); } catch (IOException e) { throw new RuntimeException("Could not write Json to servlet response", e); } }
@Override public void setContentType(WebResponse response, String encoding) { response.setContentType("text/xml; charset=" + encoding); }