I'm trying to figure out the best way to solve a design issue. I have to be able to clone (I'm cloning them to Apache HttpClient) different types of HttpServletRequest
(POST, GET...) and then send them. I want a super class with the common logic and then some subclasses for each method.
public abstract class ClonedRequest {
public ClonedRequest() {
// Common logic
...
}
public static ClonedRequest GET() {
return new ClonedGetRequest();
}
public static ClonedRequest POST() {
return new ClonedPostRequest();
}
public abstract ClonedRequest clone(HttpServletRequest request);
public abstract void send();
protected void send(HttpUriRequest clonedRequest) {
// Common logic
...
}
}
public class ClonedGetRequest extends ClonedRequest {
private HttpGet httpRequest;
public ClonedGetRequest() {
this.httpRequest = new HttpGet();
}
@Override
public ClonedRequest clone(HttpServletRequest request) {
...
}
@Override
public void send() {
super.send(httpRequest);
}
}
public class ClonedPostRequest extends ClonedRequest {
private HttpPost httpRequest;
public ClonedPostRequest() {
this.httpRequest = new HttpPost();
}
@Override
public ClonedRequest clone(HttpServletRequest request) {
...
cloneBody(request);
}
private void cloneBody(HttpServletRequest request) {
StringBuilder sb = new StringBuilder("");
BufferedReader br = request.getReader();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
httpRequest.setEntity(new StringEntity(sb.toString(), "UTF-8"));
}
@Override
public void send() {
super.send(httpRequest);
}
}
Both HttpGet
and HttpPost
inherit from HttpUriRequest
. Instead of having them as instance variables in subclasses, I could declare protected HttUriRequest request
in superclass so it wouldn't be necesary to pass the instances to the superclass method all the time for common logic (as in protected void send(HttpUriRequest clonedRequest)
). The problem is in ClonedPostRequest
. The line httpRequest.setEntity(new StringEntity(sb.toString(), "UTF-8"));
needs an instance of HttpPost
and HttpUriRequest
needs to be casted to it. It might be ok if it's only one time, but if you have this issue on serveral points, it might clutter the code with so much casting.
So my question was if is there a common pattern to approach this kind of design.