/* * Copyright 2016 the original author or authors. * * 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. */ // START SNIPPET sample // The following types can appear anywhere in the code // but say nothing about API or implementation usage import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.*; import org.apache.commons.lang3.exception.ExceptionUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; public class HttpClientWrapper { private final HttpClient client; // private member: implementation details // HttpClient is used as a parameter of a public method // so "leaks" into the public API of this component public HttpClientWrapper(HttpClient client) { this.client = client; } // public methods belongs to your API public byte[] doRawGet(String url) { GetMethod method = new GetMethod(url); try { int statusCode = doGet(method); return method.getResponseBody(); } catch (Exception e) { ExceptionUtils.rethrow(e); // this dependency is internal only } finally { method.releaseConnection(); } return null; } // GetMethod is used in a private method, so doesn't belong to the API private int doGet(GetMethod method) throws Exception { int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } return statusCode; } } // END SNIPPET sample