Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2015 Guillaume Hillairet.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Guillaume Hillairet - initial API and implementation
*
*/
package org.emfjson.gwt.handlers;
import com.google.gwt.http.client.*;
import org.eclipse.emf.common.util.Callback;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.URIHandler;
import org.eclipse.emf.ecore.resource.impl.URIHandlerImpl;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* URIHandler implementation that uses RequestBuilder to communicate with
* an HTTP server in JSON format.
*
* @author ghillairet
* @since 0.6.0
*/
public class HttpHandler extends URIHandlerImpl implements URIHandler {
public static void create(final ResourceSet resourceSet, URI createService, final Callback callback) {
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(createService.toString()));
builder.setHeader("Content-Type", "application/json");
builder.setCallback(new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
if (response.getStatusCode() == 201) {
String location = response.getHeader("Location");
Resource resource = resourceSet.createResource(URI.createURI(location));
callback.onSuccess(resource);
} else {
callback.onFailure(new Exception("Resource has not been created"));
}
}
@Override
public void onError(Request request, Throwable exception) {
callback.onFailure(exception);
}
});
try {
builder.send();
} catch (RequestException e) {
callback.onFailure(e);
}
}
@Override
public void createInputStream(final URI uri, Map, ?> options, final Callback