All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jpattern.gwt.client.command.impl.GetTextResourceCommand Maven / Gradle / Ivy

package com.jpattern.gwt.client.command.impl;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.jpattern.gwt.client.command.ACommand;
import com.jpattern.gwt.client.logger.ILogger;
import com.jpattern.shared.result.ErrorMessage;
import com.jpattern.shared.result.IResult;

/**
 * 
 * @author Francesco Cina'
 *
 * 10/mag/2011
 */
public class GetTextResourceCommand extends ACommand {

	private final StringBuffer url;
	private final StringBuffer fileContent;

	public GetTextResourceCommand(StringBuffer url, StringBuffer fileContent) {
		this.url = url;
		this.fileContent = fileContent;
	}

	@Override
	protected void exec(final IResult commandResult) {
		final ILogger logger = getProvider().getLoggerService().getLogger(this.getClass());
		try {
			new RequestBuilder(RequestBuilder.GET, url.toString()).sendRequest("", new RequestCallback() {
				
				@Override
				public void onResponseReceived(Request request, Response response) {
					fileContent.append(response.getText());					
					callback(commandResult);
				}
				
				@Override
				public void onError(Request request, Throwable exception) {
					logger.error("RequestCallback error for resource " + url.toString(), exception);
					commandResult.getErrorMessages().add(new ErrorMessage("GetTextResourceCommand", "error.getting.file", new String[]{url.toString()}));
					callback(commandResult);
				}
			});
		} catch (RequestException e) {
			logger.error("Request Resource error for resource: " + url.toString(), e);
			commandResult.getErrorMessages().add(new ErrorMessage("GetTextResourceCommand", "error.getting.file", new String[]{url.toString()}));
			callback(commandResult);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy