Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,25 @@

package com.predic8.membrane.balancer.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.util.Properties;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.predic8.membrane.core.exchange.*;
import com.predic8.membrane.core.http.*;
import com.predic8.membrane.core.interceptor.balancer.*;
import com.predic8.membrane.core.transport.http.*;
import org.apache.commons.cli.*;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.Request;
import com.predic8.membrane.core.http.Response;
import com.predic8.membrane.core.interceptor.balancer.Balancer;
import com.predic8.membrane.core.interceptor.balancer.Cluster;
import com.predic8.membrane.core.transport.http.HttpClient;
import com.predic8.membrane.core.util.MessageUtil;
import org.apache.commons.codec.binary.*;
import org.slf4j.*;

import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.*;

import static com.predic8.membrane.core.http.Request.*;
import static java.nio.charset.StandardCharsets.*;
import static javax.crypto.Cipher.ENCRYPT_MODE;
import static org.apache.commons.codec.binary.Base64.encodeBase64;
import static javax.crypto.Cipher.*;
import static org.apache.commons.codec.binary.Base64.*;

public class LBNotificationClient {

Expand Down Expand Up @@ -81,16 +74,15 @@ public void run(String[] args) throws Exception {
private Response notifiyClusterManager() throws Exception {
try (HttpClient client = new HttpClient()) {
Exchange exc = new Exchange(null);
Request r = MessageUtil.getPostRequest(getRequestURL());
r.setBodyContent(new byte[0]);
exc.setRequest(r);
exc.setRequest(post(getRequestURL()).build());
exc.getDestinations().add(getRequestURL());
return client.call(exc).getResponse();
}
}

private void parseArguments(CommandLine cl) throws Exception {
if (!new File(propertiesFile).exists()) log.warn("no properties file found at: "+ new File(propertiesFile).getAbsolutePath());
if (!new File(propertiesFile).exists())
log.warn("no properties file found at: {}",new File(propertiesFile).getAbsolutePath());

cmd = getArgument(cl, 0, '-', null, null,
"No command up, down or takeout specified!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,8 @@ public boolean shouldNotContainBody() {
if (methodsWithOptionalBody.contains(method)) {
if (header.hasContentLength())
return header.getContentLength() == 0;
if (header.getFirstValue(TRANSFER_ENCODING) != null)
return false;
return true;
}
return header.getFirstValue(TRANSFER_ENCODING) == null;
}

return false;
}
Expand Down Expand Up @@ -318,7 +316,7 @@ public Builder post(String url) throws URISyntaxException {
}

public Builder get(URIFactory uriFactory, String url) throws URISyntaxException {
return method(Request.METHOD_GET).url(uriFactory, url);
return method(METHOD_GET).url(uriFactory, url);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.predic8.membrane.core.exchange.*;
import com.predic8.membrane.core.http.*;
import com.predic8.membrane.core.transport.http.*;
import com.predic8.membrane.core.util.*;
import com.predic8.membrane.core.ws.relocator.*;
import org.jetbrains.annotations.*;
import org.slf4j.*;
Expand All @@ -28,6 +27,8 @@
import java.net.*;

import static com.predic8.membrane.core.Constants.*;
import static com.predic8.membrane.core.http.Header.HOST;
import static com.predic8.membrane.core.http.Request.*;
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.Set.*;
import static java.nio.charset.StandardCharsets.*;

Expand Down Expand Up @@ -113,14 +114,13 @@ private void callRegistry(String uri) {
}
}

private Exchange createExchange(String uri) throws MalformedURLException {
URL url = new URL(uri);
Request req = MessageUtil.getGetRequest(getCompletePath(url));
req.getHeader().setHost(url.getHost());
Exchange exc = new Exchange(null);
exc.setRequest(req);
exc.getDestinations().add(uri);
return exc;
private Exchange createExchange(String uri) throws MalformedURLException, URISyntaxException {
return get(getCompletePath(new URL(uri))).header(HOST, getHost(uri)).buildExchange();

}

private static String getHost(String uri) throws MalformedURLException {
return new URL(uri).getHost();
}

private String getCompletePath(URL url) {
Expand Down
44 changes: 9 additions & 35 deletions core/src/main/java/com/predic8/membrane/core/util/MessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import com.predic8.membrane.core.http.*;
import com.predic8.membrane.core.interceptor.schemavalidation.*;
import org.brotli.dec.BrotliInputStream;
import org.brotli.dec.*;
import org.xml.sax.*;

import javax.xml.parsers.*;
Expand All @@ -24,8 +24,7 @@
import java.io.*;
import java.util.zip.*;

import static com.predic8.membrane.core.Constants.*;
import static com.predic8.membrane.core.http.Request.*;
import static com.predic8.membrane.core.util.ByteUtil.getDecompressedData;

public class MessageUtil {

Expand All @@ -40,9 +39,11 @@ public class MessageUtil {
public static InputStream getContentAsStream(Message res) throws IOException {
if (res.isGzip()) {
return new GZIPInputStream(res.getBodyAsStream());
} else if (res.isDeflate()) {
return new ByteArrayInputStream(ByteUtil.getDecompressedData(res.getBody().getContent()));
} else if (res.isBrotli()) {
}
if (res.isDeflate()) {
return new ByteArrayInputStream(getDecompressedData(res.getBody().getContent()));
}
if (res.isBrotli()) {
return new BrotliInputStream(res.getBodyAsStream());
}
return res.getBodyAsStream();
Expand All @@ -56,7 +57,7 @@ public static byte[] getContent(Message res) throws Exception {
}
}
if (res.isDeflate()) {
return ByteUtil.getDecompressedData(res.getBody().getContent());
return getDecompressedData(res.getBody().getContent());
}
if (res.isBrotli()) {
try (InputStream lInputStream = res.getBodyAsStream();
Expand All @@ -74,31 +75,4 @@ public static Source getSOAPBody(InputStream stream) {
throw new RuntimeException("Error initializing SAXSource", e);
}
}

public static Request getGetRequest(String uri) {
Request req = getStandartRequest(METHOD_GET);
req.setUri(uri);
return req;
}

public static Request getPostRequest(String uri) {
Request req = getStandartRequest(METHOD_POST);
req.setUri(uri);
return req;
}

public static Request getDeleteRequest(String uri) {
Request req = getStandartRequest(METHOD_DELETE);
req.setUri(uri);
return req;
}

private static Request getStandartRequest(String method) {
Request request = new Request();
request.setMethod(method);
request.setVersion(HTTP_VERSION_11);

return request;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@

import com.predic8.membrane.core.*;
import com.predic8.membrane.core.exchange.*;
import com.predic8.membrane.core.http.*;
import com.predic8.membrane.core.proxies.*;
import com.predic8.membrane.core.util.*;
import org.jetbrains.annotations.*;
import org.junit.jupiter.api.*;

import java.net.*;

import static com.predic8.membrane.core.http.Request.*;
import static com.predic8.membrane.core.interceptor.Outcome.*;
import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -44,7 +43,7 @@ public void setUp() {

@Test
public void testServiceProxy() throws Exception {
exc.setRequest(MessageUtil.getGetRequest("/axis2/services/BLZService?wsdl"));
exc.setRequest(get("/axis2/services/BLZService?wsdl").build());
exc.setProxy(serviceProxy);

assertEquals(CONTINUE, dispatcher.handleRequest(exc));
Expand All @@ -57,7 +56,8 @@ public void testServiceProxy() throws Exception {

@Test
public void testProxyRuleHttp() throws Exception {
exc.setRequest(MessageUtil.getGetRequest("http://www.thomas-bayer.com:80/axis2/services/BLZService?wsdl"));
exc.setRequest(get("/dummy").build());
exc.getRequest().setUri("http://www.thomas-bayer.com:80/axis2/services/BLZService?wsdl");
exc.setProxy(getProxyRule());

assertEquals(CONTINUE, dispatcher.handleRequest(exc));
Expand All @@ -76,7 +76,7 @@ private ProxyRule getProxyRule() {
@Test
void getAddressFromTargetElementTargetWithHostAndPort() throws Exception {
exc.setProxy(serviceProxy);
exc.setRequest(new Request.Builder().get("/foo").build());
exc.setRequest(get("/foo").build());
assertEquals("http://thomas-bayer.com:80/foo", getGetAddressFromTargetElement());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,22 @@
limitations under the License. */
package com.predic8.membrane.core.interceptor;

import com.predic8.membrane.core.exchange.*;
import com.predic8.membrane.core.http.*;
import com.predic8.membrane.core.transport.http.*;
import org.junit.jupiter.api.*;
import org.xml.sax.*;

import javax.xml.namespace.*;
import javax.xml.xpath.*;
import java.io.*;
import java.net.*;
import java.util.*;

import static com.predic8.membrane.core.http.Request.*;
import static com.predic8.membrane.core.interceptor.Outcome.CONTINUE;
import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.xml.sax.InputSource;

import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.Response;
import com.predic8.membrane.core.transport.http.FakeHttpHandler;
import com.predic8.membrane.core.util.MessageUtil;

public class WADLInterceptorTest {

static final NamespaceContext nsCtx = new NamespaceContext() {
Expand Down Expand Up @@ -65,10 +62,10 @@ public static void setUp() throws Exception {
}

@Test
public void testDefaultSettings() throws Exception {
void testDefaultSettings() throws Exception {
Exchange exc = getExchange();

assertEquals(interceptor.handleResponse(exc), Outcome.CONTINUE);
assertEquals( CONTINUE, interceptor.handleResponse(exc));

assertAttribute(exc, "//wadl:resources/@base",
"http://thomas-bayer.com:3011/search/V1/");
Expand All @@ -91,7 +88,7 @@ public void testProtocolHostAndPort() throws Exception {
interceptor.setProtocol("https");
interceptor.setHost("abc.de");

assertEquals(interceptor.handleResponse(exc), Outcome.CONTINUE);
assertEquals( CONTINUE, interceptor.handleResponse(exc));

assertAttribute(exc, "//wadl:resources/@base",
"https://abc.de/search/V1/");
Expand All @@ -111,18 +108,15 @@ private void assertAttribute(Exchange exc, String xpathExpr, String expected)
.getResponse().getBodyAsStream())));
}

private Exchange getExchange() throws IOException {
private Exchange getExchange() throws URISyntaxException {
Exchange exc = new Exchange(new FakeHttpHandler(3011));
exc.setRequest(MessageUtil.getGetRequest("/search?wadl"));
exc.setRequest(get("/search?wadl").build());
InputStream resourceAsStream = this.getClass().getResourceAsStream("/wadls/search.wadl");
Response okResponse = Response.ok()
.contentType("text/xml; charset=utf-8")
.body(resourceAsStream, true)
.build();
exc.setResponse(okResponse);

exc.setResponse(Response.ok()
.contentType("text/xml; charset=utf-8")
.body(resourceAsStream, true)
.build());
exc.setOriginalHostHeader("thomas-bayer.com:80");

return exc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import com.predic8.membrane.core.exchange.*;
import com.predic8.membrane.core.transport.http.*;
import com.predic8.membrane.core.util.*;
import org.junit.jupiter.api.*;

import javax.xml.*;
Expand All @@ -26,8 +25,9 @@
import java.util.regex.*;

import static com.predic8.membrane.core.Constants.*;
import static com.predic8.membrane.core.http.Request.*;
import static com.predic8.membrane.core.http.Response.*;
import static com.predic8.membrane.core.interceptor.Outcome.CONTINUE;
import static com.predic8.membrane.core.interceptor.Outcome.*;
import static org.junit.jupiter.api.Assertions.*;

public class WSDLInterceptorTest {
Expand All @@ -39,8 +39,7 @@ public class WSDLInterceptorTest {
@BeforeEach
public void setUp() throws Exception {
exc = new Exchange(new FakeHttpHandler(3011));
exc.setRequest(MessageUtil
.getGetRequest("/axis2/services/BLZService?wsdl"));
exc.setRequest(get("/axis2/services/BLZService?wsdl").build());
exc.setResponse(ok()
.contentType("text/xml; charset=utf-8")
.body(WSDLInterceptorTest.class.getResourceAsStream("/blz-service.wsdl"), true)
Expand Down
Loading
Loading