fixing todo in code, using HttpUrlConnectionHelper instead. Adding content type as optional argument into helper method.

bug 5221
This commit is contained in:
Georg Herdt
2020-08-18 16:24:03 +02:00
parent 6f475c19fd
commit 0af6b2139b
2 changed files with 32 additions and 16 deletions
@@ -9,7 +9,9 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Logger;
import com.sap.sse.common.Duration;
import com.sap.sse.common.WithID;
import com.sap.sse.util.HttpUrlConnectionHelper;
public interface OperationsToMasterSender<S, O extends OperationWithResult<S, ?>> extends OperationsToMasterSendingQueue, WithID {
final Logger logger = Logger.getLogger(OperationsToMasterSender.class.getName());
@@ -45,15 +47,9 @@ public interface OperationsToMasterSender<S, O extends OperationWithResult<S, ?>
addOperationSentToMasterForReplication(operationWithResultWithIdWrapper);
URL url = masterDescriptor.getSendReplicaInitiatedOperationToMasterURL(this.getId().toString());
// TODO shouldn't this also use the HttpUrlConnectionHelper?
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/octet-stream");
String optionalBearerToken = masterDescriptor.getBearerToken();
if (optionalBearerToken != null && !optionalBearerToken.isEmpty()) {
connection.setRequestProperty("Authorization", "Bearer " + optionalBearerToken);
}
connection.setDoOutput(true); // we want to post the serialized operation
HttpURLConnection connection = (HttpURLConnection) HttpUrlConnectionHelper.redirectConnectionWithBearerToken(
url, Duration.ONE_MINUTE.times(10), "POST", masterDescriptor.getBearerToken(), "application/octet-stream");
logger.info("Sending operation "+operation+" to master "+masterDescriptor+"'s replicable with ID "+this+" for initial execution and replication");
connection.connect();
OutputStream outputStream = connection.getOutputStream();
DataOutputStream dos = new DataOutputStream(outputStream);
dos.writeUTF(getId().toString());
@@ -25,7 +25,7 @@ public class HttpUrlConnectionHelper {
* Redirects the connection using the <code>Location</code> header.
*/
public static URLConnection redirectConnectionWithBearerToken(URL url, String optionalBearerToken) throws MalformedURLException, IOException {
return redirectConnectionWithBearerToken(url, Duration.ONE_MINUTE.times(10), /* default HTTP method */ null, optionalBearerToken);
return redirectConnectionWithBearerToken(url, Duration.ONE_MINUTE.times(10), /* default HTTP method */ null, optionalBearerToken, null);
}
/**
@@ -33,29 +33,49 @@ public class HttpUrlConnectionHelper {
*/
public static URLConnection redirectConnectionWithBearerToken(URL url, String optionalRequestMethod,
String optionalBearerToken) throws MalformedURLException, IOException {
return redirectConnectionWithBearerToken(url, Duration.ONE_MINUTE.times(10), optionalRequestMethod, optionalBearerToken);
return redirectConnectionWithBearerToken(url, Duration.ONE_MINUTE.times(10), optionalRequestMethod, optionalBearerToken, null);
}
/**
* Redirects the connection using the <code>Location</code> header. Make sure to set
* the timeout if you expect the response to take longer.
* Create a URLConnection with the given parameters. If HTTP redirects are returned it will follow them.
*
* @param url the url to connect to
* @param timeout the read timeout
* @param optionalRequestMethod the request type, ignored when null
* @param optionalBearerToken bearer token for auth, ignored when null
* @param optionalContentType ttp content type, ignored when null
* @return the URLConnection already in open state
* @throws MalformedURLException when URL is malformed
* @throws IOException general io exception, e.g. connect is failing
*/
public static URLConnection redirectConnectionWithBearerToken(URL url, Duration timeout, String optionalRequestMethod, String optionalBearerToken)
public static URLConnection redirectConnectionWithBearerToken(URL url, Duration timeout, String optionalRequestMethod, String optionalBearerToken, String optionalContentType)
throws MalformedURLException, IOException {
return redirectConnection(url, timeout, optionalRequestMethod, t -> {
if (optionalBearerToken != null && !optionalBearerToken.isEmpty()) {
t.setRequestProperty("Authorization", "Bearer " + optionalBearerToken);
}
if (optionalContentType != null && !optionalContentType.isEmpty()) {
t.setRequestProperty("Content-Type", optionalContentType);
}
});
}
/**
* Redirects the connection using the <code>Location</code> header. Make sure to set
* the timeout if you expect the response to take longer.
*/
public static URLConnection redirectConnectionWithBearerToken(URL url, Duration timeout,
String optionalRequestMethod, String optionalBearerToken) throws MalformedURLException, IOException {
return redirectConnectionWithBearerToken(url, timeout, optionalRequestMethod, optionalBearerToken, null);
}
/**
* Redirects the connection using the <code>Location</code> header. Make sure to set
* the timeout if you expect the response to take longer.
*/
public static URLConnection redirectConnectionWithBearerToken(URL url, Duration timeout, String optionalBearerToken)
throws MalformedURLException, IOException {
return redirectConnectionWithBearerToken(url, timeout, /* request method */ null, optionalBearerToken);
return redirectConnectionWithBearerToken(url, timeout, /* request method */ null, optionalBearerToken, null);
}
/**