Java tutorial
/** * * Copyright (c) 2014 Kerby Martino and others. All rights reserved. * 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 com.textquo.dreamcode.client.publicstores; import com.google.gwt.http.client.RequestBuilder; import com.google.gwt.http.client.RequestCallback; import com.google.gwt.http.client.RequestException; import com.textquo.dreamcode.client.DreamcodeCallback; import com.textquo.dreamcode.client.Routes; import org.restlet.client.Request; import org.restlet.client.Response; import org.restlet.client.Uniform; import org.restlet.client.data.MediaType; import org.restlet.client.data.Status; import org.restlet.client.resource.ClientResource; public class GlobalStore { /** * Add new object. * Server stores object with id generated by a sharded counter (if not given) * * @param callback */ public void add(String type, String id, String jsonObject, final DreamcodeCallback callback) { String url = Routes.DREAMCODE + Routes.COLLECTIONS + "?type=" + type + "&id=" + id; RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url); try { builder.setHeader("content-type", "application/json"); builder.sendRequest(jsonObject, new RequestCallback() { public void onResponseReceived(com.google.gwt.http.client.Request request, com.google.gwt.http.client.Response response) { String jsonResponse = response.getText(); if (response.getStatusCode() == com.google.gwt.http.client.Response.SC_OK) { callback.success(jsonResponse); } else { callback.failure(new Throwable("Error: " + response.getStatusCode())); } } public void onError(com.google.gwt.http.client.Request request, Throwable throwable) { callback.failure(throwable); } }); } catch (RequestException e) { callback.failure(new Throwable(e.getMessage())); } // TODO: Make this code below work: // ClientResource resource = new ClientResource(Routes.DREAMCODE + Routes.COLLECTIONS); // resource.setOnResponse(new Uniform() { // public void handle(Request request, Response response) { // try { // Status status = response.getStatus(); // if (!Status.isError(status.getCode())) { // String jsonResponse = response.getEntity().getText(); // callback.success(jsonResponse); // } else { // callback.failure(new Throwable("Error: " + status.getCode())); // } // } catch (Exception e) { // callback.failure(new Throwable(e.getMessage())); // } // } // }); // JsniHelper.consoleLog("Adding object id=" + id + " type=" + type + " data=" + jsonObject); // if(JsonHelper.isValid(jsonObject)){ // resource.getReference().addQueryParameter("type", type); // resource.getReference().addQueryParameter("id",id); // resource.post(jsonObject, MediaType.APPLICATION_JSON); // } else { // callback.failure(new Throwable("Invalid JSON object")); // } } /** * * @param type * @param id * @param callback */ public void find(String type, String id, final DreamcodeCallback callback) { final ClientResource resource = new ClientResource(Routes.DREAMCODE + Routes.COLLECTIONS); resource.setOnResponse(new Uniform() { public void handle(Request request, Response response) { try { Status status = response.getStatus(); if (!Status.isError(status.getCode())) { String jsonResponse = response.getEntity().getText(); callback.success(jsonResponse); } else { callback.failure(new Throwable("Error: " + status.getCode())); } } catch (Exception e) { callback.failure(new Throwable(e.getMessage())); } } }); resource.getReference().addQueryParameter("type", type); resource.getReference().addQueryParameter("id", id); resource.get(MediaType.APPLICATION_JSON); } public void findAll(String type, final DreamcodeCallback callback) { final ClientResource resource = new ClientResource(Routes.DREAMCODE + Routes.COLLECTIONS); resource.setOnResponse(new Uniform() { public void handle(Request request, Response response) { try { String jsonResponse = response.getEntity().getText(); callback.success(jsonResponse); } catch (Exception e) { e.printStackTrace(); callback.failure(new Throwable(e.getMessage())); } } }); resource.getReference().addQueryParameter("type", type); resource.get(MediaType.APPLICATION_JSON); } /** * Update existing object */ public void update() { } }