Java tutorial
/** * Copyright 2010 Guardian News And Media * * 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.gu.openplatform.contentapi; import com.gu.openplatform.contentapi.configuration.ApiClientConfiguration; import com.gu.openplatform.contentapi.configuration.ConfigurationProfile; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; import java.util.Set; @SuppressWarnings("unchecked") public abstract class ApiQuery<T extends ApiQuery> implements Cloneable { private static final Logger logger = LoggerFactory.getLogger(ApiQuery.class); private static final String formatString = "json"; protected final HttpClient httpClient; protected final JsonParser jsonParser; private String apiKey; private String queryString; protected ApiQuery(HttpClient httpClient, JsonParser jsonParser) { this.httpClient = httpClient; this.jsonParser = jsonParser; } public abstract ApiResponse execute(); public T apiKey(String apiKey) { this.apiKey = apiKey; return (T) this; } public T query(String queryString) { this.queryString = queryString; return (T) this; } public T zeroTermQuery() { this.queryString = null; return (T) this; } public T configure(ApiClientConfiguration apiClientConfiguration) { if (logger.isTraceEnabled()) logger.trace("Applying configuration: " + apiClientConfiguration.getClass().getSimpleName()); apiClientConfiguration.configure(this); return (T) this; } @Override public String toString() { return ReflectionToStringBuilder.reflectionToString(this); } T configure(ConfigurationProfile configurationProfile) { for (ApiClientConfiguration apiClientConfiguration : configurationProfile.getSearchConfigurations()) configure(apiClientConfiguration); return (T) this; } protected void addHttpParameter(Map<ApiParameter, Object> parameters, ApiParameter param, Set<? extends ApiUrlParameter> value) { if (value != null && !value.isEmpty()) parameters.put(param, value); } protected void addHttpParameter(Map<ApiParameter, Object> parameters, ApiParameter param, Object value) { if (value != null) parameters.put(param, value); } protected Map<ApiParameter, Object> generateParameterMap() { Map<ApiParameter, Object> parameters = new HashMap<ApiParameter, Object>(); addHttpParameter(parameters, ApiParameter.apiKey, getApiKey()); addHttpParameter(parameters, ApiParameter.format, getFormatString()); addHttpParameter(parameters, ApiParameter.q, getQueryString()); fillInApiParameters(parameters); return parameters; } protected abstract void fillInApiParameters(Map<ApiParameter, Object> parameters); protected JsonParser getJsonParser() { return jsonParser; } protected HttpClient getHttpClient() { return httpClient; } String getApiKey() { return apiKey; } String getQueryString() { return queryString; } String getFormatString() { return formatString; } }