com.gwtplatform.dispatch.rest.client.AbstractRestAction Maven / Gradle / Ivy
/**
* Copyright 2011 ArcBees Inc.
*
* 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.gwtplatform.dispatch.rest.client;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.google.common.annotations.VisibleForTesting;
import com.google.gwt.i18n.shared.DateTimeFormat;
import com.gwtplatform.dispatch.rest.shared.HttpMethod;
import com.gwtplatform.dispatch.rest.shared.RestAction;
import com.gwtplatform.dispatch.rest.shared.RestParameter;
/**
* Provides a basic implementation of {@link RestAction} to inheritors. This is used by GWTP code-generator to create
* the actions defined by the services.
*
* @param the result type
*/
public abstract class AbstractRestAction implements RestAction {
private String defaultDateFormat;
private HttpMethod httpMethod;
private String rawServicePath;
private List pathParams;
private List headerParams;
private List queryParams;
private List formParams;
private Object bodyParam;
protected AbstractRestAction() {
defaultDateFormat = DateFormat.DEFAULT;
pathParams = new ArrayList();
headerParams = new ArrayList();
queryParams = new ArrayList();
formParams = new ArrayList();
}
protected AbstractRestAction(
HttpMethod httpMethod,
String rawServicePath,
String defaultDateFormat) {
this();
this.httpMethod = httpMethod;
this.rawServicePath = rawServicePath;
this.defaultDateFormat = DateFormat.DEFAULT;
this.defaultDateFormat = defaultDateFormat;
}
@Override
public HttpMethod getHttpMethod() {
return httpMethod;
}
@Override
public String getPath() {
return rawServicePath;
}
@Override
public List getPathParams() {
return pathParams;
}
@Override
public List getQueryParams() {
return queryParams;
}
@Override
public List getFormParams() {
return formParams;
}
@Override
public List getHeaderParams() {
return headerParams;
}
@Override
public Object getBodyParam() {
return bodyParam;
}
@Override
public Boolean hasFormParams() {
return !formParams.isEmpty();
}
@Override
public Boolean hasBodyParam() {
return bodyParam != null;
}
protected void addPathParam(String name, Object value) {
addParam(pathParams, name, value);
}
protected void addPathParam(String name, Date date) {
addDateParam(pathParams, name, date);
}
protected void addPathParam(String name, Date date, String pattern) {
addDateParam(pathParams, name, date, pattern);
}
protected void addQueryParam(String name, Object value) {
addParam(queryParams, name, value);
}
protected void addQueryParam(String name, Date date) {
addDateParam(queryParams, name, date);
}
protected void addQueryParam(String name, Date date, String pattern) {
addDateParam(queryParams, name, date, pattern);
}
protected void addFormParam(String name, Object value) {
addParam(formParams, name, value);
}
protected void addFormParam(String name, Date date) {
addDateParam(formParams, name, date);
}
protected void addFormParam(String name, Date date, String pattern) {
addDateParam(formParams, name, date, pattern);
}
protected void addHeaderParam(String name, Object value) {
addParam(headerParams, name, value);
}
protected void addHeaderParam(String name, Date date) {
addDateParam(headerParams, name, date);
}
protected void addHeaderParam(String name, Date date, String pattern) {
addDateParam(headerParams, name, date, pattern);
}
protected void setBodyParam(Object value) {
bodyParam = value;
}
@VisibleForTesting
protected String formatDate(Date date, String pattern) {
if (date == null) {
return null;
} else {
return DateTimeFormat.getFormat(pattern).format(date);
}
}
private void addDateParam(List target, String name, Date date) {
String value = formatDate(date, defaultDateFormat);
addParam(target, name, value);
}
private void addDateParam(List target, String name, Date date, String pattern) {
String value = formatDate(date, pattern);
addParam(target, name, value);
}
private void addParam(List target, String name, Object value) {
if (value != null) {
target.add(new RestParameter(name, value));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy