Beispiel #1
0
  public final void onRequest() {
    RequestCycle requestCycle = getComponent().getRequestCycle();
    Request request = requestCycle.getRequest();
    IRequestParameters parameters = request.getRequestParameters();
    StringValue input = parameters.getParameterValue("term");

    final Iterable<T> choices = getChoices(input.toString(""));

    String jsonArray = createJson(choices);

    requestCycle.scheduleRequestHandlerAfterCurrent(
        new TextRequestHandler("application/json", "UTF-8", jsonArray));
  }
  @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 Session newSession(final Request request, final Response response) {
    if (!DEMO_MODE_USING_CREDENTIALS_AS_QUERYARGS) {
      return super.newSession(request, response);
    }

    // else demo mode
    final AuthenticatedWebSessionForIsis s =
        (AuthenticatedWebSessionForIsis) super.newSession(request, response);
    IRequestParameters requestParameters = request.getRequestParameters();
    final org.apache.wicket.util.string.StringValue user =
        requestParameters.getParameterValue("user");
    final org.apache.wicket.util.string.StringValue password =
        requestParameters.getParameterValue("pass");
    s.signIn(user.toString(), password.toString());
    return s;
  }