├── README.md ├── emulator ├── src │ ├── main │ │ ├── resources │ │ │ ├── testFiles │ │ │ │ ├── clientRequest.txt │ │ │ │ ├── serverRequest.txt │ │ │ │ ├── test1clientRequest.txt │ │ │ │ ├── test1serverRequest.txt │ │ │ │ ├── test2serverResponse.txt │ │ │ │ ├── test3serverRequest.txt │ │ │ │ ├── test3serverResponse.txt │ │ │ │ ├── test4clientRequest.txt │ │ │ │ ├── test4serverRequest.txt │ │ │ │ ├── test4serverResponse.txt │ │ │ │ ├── clientResponse.txt │ │ │ │ └── serverResponse.txt │ │ │ ├── server.properties │ │ │ └── log4j.properties │ │ └── java │ │ │ └── org │ │ │ └── wso2 │ │ │ └── carbon │ │ │ └── protocol │ │ │ └── emulator │ │ │ ├── dsl │ │ │ ├── Protocol.java │ │ │ ├── EmulatorType.java │ │ │ ├── contexts │ │ │ │ ├── AbstractClientProcessorContext.java │ │ │ │ ├── AbstractRequestBuilderContext.java │ │ │ │ ├── AbstractResponseBuilderContext.java │ │ │ │ ├── AbstractOperationBuilderContext.java │ │ │ │ ├── AbstractConfigurationBuilderContext.java │ │ │ │ ├── AbstractGivenBuilderContext.java │ │ │ │ ├── AbstractServerOperationBuilderContext.java │ │ │ │ ├── AbstractThenBuilderContext.java │ │ │ │ ├── AbstractWhenBuilderContext.java │ │ │ │ ├── AbstractEmulatorBuilderContext.java │ │ │ │ └── AbstractClientOperationBuilderContext.java │ │ │ ├── Emulator.java │ │ │ └── AbstractProtocolEmulator.java │ │ │ ├── tcp │ │ │ ├── client │ │ │ │ ├── TcpClientInitializer.java │ │ │ │ └── context │ │ │ │ │ ├── TcpClientEmulatorBuilderContext.java │ │ │ │ │ └── TcpClientConfigBuilderContext.java │ │ │ └── TCPProtocolEmulator.java │ │ │ ├── http │ │ │ ├── params │ │ │ │ ├── CookieOperation.java │ │ │ │ ├── HeaderOperation.java │ │ │ │ ├── QueryParameterOperation.java │ │ │ │ ├── Cookie.java │ │ │ │ ├── Header.java │ │ │ │ └── QueryParameter.java │ │ │ ├── client │ │ │ │ ├── processors │ │ │ │ │ ├── AbstractClientProcessor.java │ │ │ │ │ ├── HttpResponseInformationProcessor.java │ │ │ │ │ └── HttpResponseAssertProcessor.java │ │ │ │ ├── contexts │ │ │ │ │ ├── HttpClientProcessorContext.java │ │ │ │ │ ├── HttpClientResponseProcessorContext.java │ │ │ │ │ ├── HttpClientRequestProcessorContext.java │ │ │ │ │ ├── RequestResponseCorrelation.java │ │ │ │ │ ├── HttpClientGivenBuilderContext.java │ │ │ │ │ ├── HttpClientThenBuilderContext.java │ │ │ │ │ ├── HttpClientWhenBuilderContext.java │ │ │ │ │ ├── HttpClientOperationBuilderContext.java │ │ │ │ │ ├── HttpResponseContext.java │ │ │ │ │ ├── HttpClientInformationContext.java │ │ │ │ │ ├── HttpClientResponseBuilderContext.java │ │ │ │ │ └── HttpClientConfigBuilderContext.java │ │ │ │ ├── ConnectionDroppingWriter.java │ │ │ │ └── handler │ │ │ │ │ └── HttpClientHandler.java │ │ │ ├── server │ │ │ │ ├── contexts │ │ │ │ │ ├── UtilityContext.java │ │ │ │ │ ├── HttpServerThenBuilderContext.java │ │ │ │ │ ├── HttpServerGivenBuilderContext.java │ │ │ │ │ ├── HttpServerOperationBuilderContext.java │ │ │ │ │ ├── HttpServerInformationContext.java │ │ │ │ │ ├── HttpServerWhenBuilderContext.java │ │ │ │ │ ├── MockServerThread.java │ │ │ │ │ ├── DelayedElement.java │ │ │ │ │ ├── HttpServerProcessorContext.java │ │ │ │ │ ├── HttpRequestContext.java │ │ │ │ │ └── HttpServerResponseBuilderContext.java │ │ │ │ ├── processors │ │ │ │ │ ├── HttpResponseCustomProcessor.java │ │ │ │ │ ├── HttpRequestCustomProcessor.java │ │ │ │ │ ├── AbstractServerProcessor.java │ │ │ │ │ ├── HttpRequestResponseMatchingProcessor.java │ │ │ │ │ └── HttpRequestInformationProcessor.java │ │ │ │ ├── handler │ │ │ │ │ ├── HttpChunkedSupportHandler.java │ │ │ │ │ ├── HttpChunkedWriteHandler.java │ │ │ │ │ └── HttpVersionHandler.java │ │ │ │ └── HttpServerInitializer.java │ │ │ ├── common │ │ │ │ └── handler │ │ │ │ │ ├── SlowReadingHandler.java │ │ │ │ │ └── SlowByteWriterHandler.java │ │ │ ├── SSLConfig.java │ │ │ ├── HTTPProtocolEmulator.java │ │ │ └── SSLHandlerFactory.java │ │ │ ├── util │ │ │ ├── FileReaderUtil.java │ │ │ ├── WireLogHandler.java │ │ │ └── ValidationUtil.java │ │ │ └── example │ │ │ └── http │ │ │ ├── SimpleEmulatorServer.java │ │ │ └── SimpleEmulatorClient.java │ └── test │ │ ├── java │ │ └── org │ │ │ └── wso2 │ │ │ └── carbon │ │ │ └── protocol │ │ │ └── emulator │ │ │ └── http │ │ │ ├── custom │ │ │ └── processor │ │ │ │ └── validation │ │ │ │ ├── CustomRequestProcessor.java │ │ │ │ ├── CustomResponseProcessor.java │ │ │ │ ├── CustomProcessorValidationTestCase2.java │ │ │ │ └── CustomProcessorValidationTestCase.java │ │ │ ├── LogicDelayTestCase.java │ │ │ ├── FastBackEndTestCase.java │ │ │ ├── ReadingDelayTestCase.java │ │ │ └── WritingDelayTestCase.java │ │ └── resources │ │ └── testng.xml └── pom.xml ├── .gitignore ├── issue_template.md └── pull_request_template.md /README.md: -------------------------------------------------------------------------------- 1 | # wso2-protocol-emulator -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/clientRequest.txt: -------------------------------------------------------------------------------- 1 | TestRequest1 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/serverRequest.txt: -------------------------------------------------------------------------------- 1 | TestRequest1 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/test1clientRequest.txt: -------------------------------------------------------------------------------- 1 | User1 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/test1serverRequest.txt: -------------------------------------------------------------------------------- 1 | User1 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/test2serverResponse.txt: -------------------------------------------------------------------------------- 1 | User2 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/test3serverRequest.txt: -------------------------------------------------------------------------------- 1 | User3 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/test3serverResponse.txt: -------------------------------------------------------------------------------- 1 | User3 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/test4clientRequest.txt: -------------------------------------------------------------------------------- 1 | User4 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/test4serverRequest.txt: -------------------------------------------------------------------------------- 1 | User4 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/test4serverResponse.txt: -------------------------------------------------------------------------------- 1 | User4 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/clientResponse.txt: -------------------------------------------------------------------------------- 1 | TestResponse1 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/testFiles/serverResponse.txt: -------------------------------------------------------------------------------- 1 | TestResponse1 2 | -------------------------------------------------------------------------------- /emulator/src/main/resources/server.properties: -------------------------------------------------------------------------------- 1 | worker_count=8 2 | boss_count=8 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | -------------------------------------------------------------------------------- /emulator/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | ### direct log messages to stdout ### 2 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 | log4j.appender.stdout.Target=System.out 4 | log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout 5 | log4j.logger.org.apache.synapse.transport.http.wire=DEBUG 6 | log4j.rootLogger=INFO, stdout 7 | 8 | 9 | 10 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 11 | 12 | log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n 13 | 14 | 15 | 16 | log4j.logger.httpclient.wire=DEBUG 17 | 18 | log4j.logger.org.apache.commons.httpclient=DEBUG 19 | -------------------------------------------------------------------------------- /issue_template.md: -------------------------------------------------------------------------------- 1 | **Description:** 2 | 3 | 4 | **Suggested Labels:** 5 | 6 | 7 | **Suggested Assignees:** 8 | 9 | 10 | **Affected Product Version:** 11 | 12 | **OS, DB, other environment details and versions:** 13 | 14 | **Steps to reproduce:** 15 | 16 | 17 | **Related Issues:** 18 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/Protocol.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl; 20 | 21 | /** 22 | * Protocol types. 23 | */ 24 | public enum Protocol { 25 | HTTP, HTTPS 26 | } 27 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/tcp/client/TcpClientInitializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.tcp.client; 20 | 21 | /** 22 | * Class for Tcp Client Initializer. 23 | */ 24 | public class TcpClientInitializer { 25 | } 26 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/params/CookieOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.params; 20 | 21 | /** 22 | * Class for define Cookie Operations. 23 | */ 24 | public enum CookieOperation { 25 | OR, AND 26 | } 27 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/params/HeaderOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.params; 20 | 21 | /** 22 | * Class for define Header Operations. 23 | */ 24 | public enum HeaderOperation { 25 | OR, AND 26 | } 27 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/EmulatorType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl; 20 | 21 | /** 22 | * Types of emulator. 23 | */ 24 | public enum EmulatorType { 25 | HTTP_SERVER, HTTP_CLIENT, TCP_SERVER, TCP_CLIENT 26 | } 27 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/params/QueryParameterOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.params; 20 | 21 | /** 22 | * Class for define Query parameter operations. 23 | */ 24 | public enum QueryParameterOperation { 25 | OR, AND 26 | } 27 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractClientProcessorContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | /** 22 | * Abstract class for ClientProcessorContext. 23 | */ 24 | public abstract class AbstractClientProcessorContext { 25 | } 26 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractRequestBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | /** 22 | * Abstract class for RequestBuilderContext. 23 | */ 24 | public abstract class AbstractRequestBuilderContext { 25 | 26 | } 27 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractResponseBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | /** 22 | * Abstract class for ResponseBuilderContext. 23 | */ 24 | public abstract class AbstractResponseBuilderContext { 25 | } 26 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractOperationBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | /** 22 | * Abstract class for OperationBuilderContext. 23 | */ 24 | public abstract class AbstractOperationBuilderContext { 25 | 26 | } 27 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/tcp/client/context/TcpClientEmulatorBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.tcp.client.context; 20 | 21 | /** 22 | * Class for TCP Client Emulator Builder Context. 23 | */ 24 | public class TcpClientEmulatorBuilderContext { 25 | } 26 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractConfigurationBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | /** 22 | * Abstract class for ConfigurationBuilderContext. 23 | */ 24 | public abstract class AbstractConfigurationBuilderContext { 25 | 26 | public abstract AbstractConfigurationBuilderContext host(String host); 27 | 28 | public abstract AbstractConfigurationBuilderContext port(int port); 29 | } 30 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractGivenBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | /** 22 | * Abstract class for a given BuilderContext. 23 | * 24 | * @param given builder context type 25 | */ 26 | public abstract class AbstractGivenBuilderContext { 27 | 28 | public abstract AbstractWhenBuilderContext given(T configurationContext); 29 | } 30 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractServerOperationBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | /** 22 | * Abstract class for ServerOperationBuilderContext. 23 | */ 24 | public abstract class AbstractServerOperationBuilderContext extends AbstractOperationBuilderContext { 25 | 26 | public abstract AbstractServerOperationBuilderContext start(); 27 | 28 | public abstract AbstractServerOperationBuilderContext stop(); 29 | } 30 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/processors/AbstractClientProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.processors; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientProcessorContext; 22 | 23 | /** 24 | * Abstract class for ClientProcessor. 25 | * @param Client Processor type 26 | */ 27 | public abstract class AbstractClientProcessor { 28 | 29 | public abstract void process(T processorContext); 30 | } 31 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/Emulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.HTTPProtocolEmulator; 22 | 23 | /** 24 | * Initialize the Emulator instance. 25 | */ 26 | public class Emulator { 27 | 28 | /** 29 | * Return emulator instance for HTTP protocol. 30 | * @return HTTPProtocolEmulator 31 | */ 32 | public static HTTPProtocolEmulator getHttpEmulator() { 33 | return new HTTPProtocolEmulator(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/UtilityContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import java.util.regex.Pattern; 22 | 23 | /** 24 | * Class for utility context. 25 | */ 26 | public class UtilityContext { 27 | private Pattern pattern; 28 | 29 | public UtilityContext() { 30 | pattern = Pattern.compile("(\\@\\{[a-zA-Z]*.[a-zA-Z]*[0-9]*\\})"); 31 | } 32 | 33 | public Pattern getPattern() { 34 | return pattern; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/processors/HttpResponseCustomProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.processors; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerProcessorContext; 22 | 23 | /** 24 | * Class to Custom processor for HttpResponse. 25 | */ 26 | public class HttpResponseCustomProcessor { 27 | public HttpServerProcessorContext process(HttpServerProcessorContext responseContext) { 28 | return responseContext; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/processors/HttpRequestCustomProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.processors; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerProcessorContext; 22 | 23 | /** 24 | * Class to create custom HttpRequest Processor. 25 | */ 26 | public class HttpRequestCustomProcessor { 27 | 28 | public HttpServerProcessorContext process(HttpServerProcessorContext requestContext) { 29 | return requestContext; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/AbstractProtocolEmulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractGivenBuilderContext; 22 | 23 | /** 24 | * Abstract class for ProtocolEmulator. 25 | */ 26 | public abstract class AbstractProtocolEmulator { 27 | 28 | /** 29 | * Create server instance. 30 | */ 31 | public abstract AbstractGivenBuilderContext server(); 32 | 33 | /** 34 | * Create client instance. 35 | */ 36 | public abstract AbstractGivenBuilderContext client(); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/processors/AbstractServerProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.processors; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerProcessorContext; 22 | 23 | /** 24 | * Abstract class for Server Processor. 25 | */ 26 | public abstract class AbstractServerProcessor { 27 | 28 | /** 29 | * Process the given context according to type of the processor. 30 | * @param processorContext 31 | */ 32 | public abstract void process(HttpServerProcessorContext processorContext); 33 | } 34 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractThenBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | /** 22 | * Abstract class for ThenBuilderContext 23 | * 24 | * @param response builder context type 25 | */ 26 | public abstract class AbstractThenBuilderContext { 27 | 28 | /** 29 | * Evaluate then statement according to a response context. 30 | * @param responseBuilderContext 31 | * @return whenBuilderContext 32 | */ 33 | public abstract AbstractWhenBuilderContext then(T responseBuilderContext); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/tcp/TCPProtocolEmulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.tcp; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.Emulator; 22 | 23 | /** 24 | * Class for TCP Protocol Emulator. 25 | */ 26 | public class TCPProtocolEmulator /*extends AbstractProtocolEmulator */ { 27 | 28 | public TCPProtocolEmulator(Emulator emulator) { 29 | //super(emulator); 30 | 31 | } 32 | 33 | /* @Override 34 | public AbstractEmulatorContext server() { 35 | return null; 36 | } 37 | 38 | @Override 39 | public AbstractEmulatorContext client() { 40 | return null; 41 | }*/ 42 | } 43 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractWhenBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | /** 22 | * Abstract class for WhenBuilderContext. 23 | * 24 | * @param request builder context type 25 | */ 26 | public abstract class AbstractWhenBuilderContext { 27 | 28 | /** 29 | * Evaluate when statement according to a request context. 30 | * @param requestContextBuilder 31 | * @return thenBuilderContext 32 | */ 33 | public abstract AbstractThenBuilderContext when(T requestContextBuilder); 34 | 35 | public abstract AbstractOperationBuilderContext operation(); 36 | } 37 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/params/Cookie.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.params; 20 | 21 | /** 22 | * Class for Cookie creation. 23 | */ 24 | public class Cookie { 25 | private String name; 26 | private String value; 27 | 28 | public Cookie(String name, String value) { 29 | this.name = name; 30 | this.value = value; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public String getValue() { 42 | return value; 43 | } 44 | 45 | public void setValue(String value) { 46 | this.value = value; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/params/Header.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.params; 20 | 21 | /** 22 | * Class for Header creation. 23 | */ 24 | public class Header { 25 | private String name; 26 | private String value; 27 | 28 | public Header(String name, String value) { 29 | this.name = name; 30 | this.value = value; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public String getValue() { 42 | return value; 43 | } 44 | 45 | public void setValue(String value) { 46 | this.value = value; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /emulator/src/test/java/org/wso2/carbon/protocol/emulator/http/custom/processor/validation/CustomRequestProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.custom.processor.validation; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerProcessorContext; 22 | import org.wso2.carbon.protocol.emulator.http.server.processors.HttpRequestCustomProcessor; 23 | 24 | /** 25 | * CustomRequestProcessor 26 | */ 27 | public class CustomRequestProcessor extends HttpRequestCustomProcessor { 28 | 29 | @Override 30 | public HttpServerProcessorContext process(HttpServerProcessorContext requestContext) { 31 | requestContext.getHttpRequestContext().setUri("/users/user2"); 32 | return requestContext; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractEmulatorBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | /** 22 | * Abstract class for EmulatorBuilderContext. 23 | */ 24 | public class AbstractEmulatorBuilderContext { 25 | 26 | private String host; 27 | private Integer port; 28 | 29 | public AbstractEmulatorBuilderContext host(String host) { 30 | this.host = host; 31 | return this; 32 | } 33 | 34 | public AbstractEmulatorBuilderContext port(int port) { 35 | this.port = port; 36 | return this; 37 | } 38 | 39 | public String getHost() { 40 | return host; 41 | } 42 | 43 | public Integer getPort() { 44 | return port; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /emulator/src/test/java/org/wso2/carbon/protocol/emulator/http/custom/processor/validation/CustomResponseProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.custom.processor.validation; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerProcessorContext; 22 | import org.wso2.carbon.protocol.emulator.http.server.processors.HttpResponseCustomProcessor; 23 | 24 | /** 25 | * CustomResponseProcessor 26 | */ 27 | public class CustomResponseProcessor extends HttpResponseCustomProcessor { 28 | 29 | @Override 30 | public HttpServerProcessorContext process(HttpServerProcessorContext responseContext) { 31 | 32 | responseContext.getFinalResponse().headers().add("Header2", "value2"); 33 | return responseContext; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/params/QueryParameter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.params; 20 | 21 | /** 22 | * Class for Query parameter creation. 23 | */ 24 | public class QueryParameter { 25 | private String name; 26 | private String value; 27 | 28 | public QueryParameter(String name, String value) { 29 | this.name = name; 30 | this.value = value; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public String getValue() { 42 | return value; 43 | } 44 | 45 | public void setValue(String value) { 46 | this.value = value; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/dsl/contexts/AbstractClientOperationBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.dsl.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientOperationBuilderContext; 22 | import org.wso2.carbon.protocol.emulator.http.client.contexts.RequestResponseCorrelation; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * Abstract class for ClientOperationBuilderContext. 28 | */ 29 | public abstract class AbstractClientOperationBuilderContext extends AbstractOperationBuilderContext { 30 | 31 | public abstract AbstractClientProcessorContext send(); 32 | 33 | public abstract HttpClientOperationBuilderContext sendAsync(); 34 | 35 | public abstract List shutdown(); 36 | } 37 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/common/handler/SlowReadingHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.common.handler; 20 | 21 | import io.netty.channel.ChannelHandlerContext; 22 | import io.netty.channel.ChannelInboundHandlerAdapter; 23 | 24 | import java.util.concurrent.TimeUnit; 25 | 26 | 27 | /** 28 | * This handler imposes a reading delay on the message. 29 | */ 30 | public class SlowReadingHandler extends ChannelInboundHandlerAdapter { 31 | private int delay; 32 | 33 | public SlowReadingHandler(int delay) { 34 | this.delay = delay; 35 | } 36 | 37 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 38 | TimeUnit.MILLISECONDS.sleep(delay); 39 | ctx.fireChannelRead(msg); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpClientProcessorContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractClientProcessorContext; 22 | 23 | /** 24 | * Class to set and get HTTP client information context. 25 | */ 26 | public class HttpClientProcessorContext extends AbstractClientProcessorContext { 27 | private HttpClientInformationContext clientInformationContext; 28 | 29 | public HttpClientInformationContext getClientInformationContext() { 30 | return clientInformationContext; 31 | } 32 | 33 | public void setClientInformationContext(HttpClientInformationContext clientInformationContext) { 34 | this.clientInformationContext = clientInformationContext; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /emulator/src/test/resources/testng.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/common/handler/SlowByteWriterHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.common.handler; 20 | 21 | import io.netty.channel.ChannelHandlerContext; 22 | import io.netty.channel.ChannelOutboundHandlerAdapter; 23 | import io.netty.channel.ChannelPromise; 24 | 25 | import java.util.concurrent.TimeUnit; 26 | 27 | /** 28 | * Delay the writing by configured time 29 | */ 30 | public class SlowByteWriterHandler extends ChannelOutboundHandlerAdapter { 31 | private final int writingDelay; 32 | 33 | public SlowByteWriterHandler(int writingDelay) { 34 | this.writingDelay = writingDelay; 35 | } 36 | 37 | @Override 38 | public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { 39 | 40 | TimeUnit.MILLISECONDS.sleep(writingDelay); 41 | 42 | ctx.write(msg, promise); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/ConnectionDroppingWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client; 20 | 21 | import io.netty.buffer.ByteBuf; 22 | import io.netty.channel.ChannelFutureListener; 23 | import io.netty.channel.ChannelHandlerContext; 24 | import io.netty.channel.ChannelOutboundHandlerAdapter; 25 | import io.netty.channel.ChannelPromise; 26 | 27 | /** 28 | * Drop the connection after writing half of the bytes 29 | */ 30 | public class ConnectionDroppingWriter extends ChannelOutboundHandlerAdapter { 31 | 32 | @Override 33 | public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { 34 | if (msg instanceof ByteBuf) { 35 | ByteBuf buffer = (ByteBuf) msg; 36 | 37 | ByteBuf halfBuf = buffer.slice(buffer.readerIndex(), buffer.readableBytes() / 2); 38 | ctx.writeAndFlush(halfBuf, promise).addListener(ChannelFutureListener.CLOSE); 39 | } else { 40 | ctx.write(msg, promise); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpClientResponseProcessorContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import io.netty.handler.codec.http.HttpResponse; 22 | 23 | /** 24 | * Class for get and set Response of server. 25 | */ 26 | public class HttpClientResponseProcessorContext extends HttpClientProcessorContext { 27 | private HttpResponseContext receivedResponseContext; 28 | private HttpResponse receivedResponse; 29 | 30 | public HttpResponseContext getReceivedResponseContext() { 31 | return receivedResponseContext; 32 | } 33 | 34 | public void setReceivedResponseContext(HttpResponseContext receivedResponseContext) { 35 | this.receivedResponseContext = receivedResponseContext; 36 | } 37 | 38 | public HttpResponse getReceivedResponse() { 39 | return receivedResponse; 40 | } 41 | 42 | public void setReceivedResponse(HttpResponse receivedResponse) { 43 | this.receivedResponse = receivedResponse; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/SSLConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http; 20 | 21 | import java.io.File; 22 | 23 | /** 24 | * Class for SSL Configuration. 25 | */ 26 | public class SSLConfig { 27 | private File keyStore; 28 | private String keyStorePass; 29 | private String certPass; 30 | private File trustStore; 31 | private String trustStorePass; 32 | 33 | public SSLConfig(File keyStore, String keyStorePass, String certPass, File trustStore, String trustStorePass) { 34 | this.keyStore = keyStore; 35 | this.keyStorePass = keyStorePass; 36 | this.certPass = certPass; 37 | this.trustStore = trustStore; 38 | this.trustStorePass = trustStorePass; 39 | } 40 | 41 | public String getCertPass() { 42 | return certPass; 43 | } 44 | 45 | public File getTrustStore() { 46 | return trustStore; 47 | } 48 | 49 | public String getTrustStorePass() { 50 | return trustStorePass; 51 | } 52 | 53 | public File getKeyStore() { 54 | return keyStore; 55 | } 56 | 57 | public String getKeyStorePass() { 58 | return keyStorePass; 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpClientRequestProcessorContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import io.netty.channel.Channel; 22 | import io.netty.handler.codec.http.HttpRequest; 23 | 24 | /** 25 | * Class to set and get HTTP client request. 26 | */ 27 | public class HttpClientRequestProcessorContext extends HttpClientProcessorContext { 28 | private HttpClientRequestBuilderContext requestBuilderContext; 29 | private HttpRequest request; 30 | private Channel channel; 31 | 32 | public HttpClientRequestBuilderContext getRequestBuilderContext() { 33 | return requestBuilderContext; 34 | } 35 | 36 | public void setRequestBuilderContext(HttpClientRequestBuilderContext requestBuilderContext) { 37 | this.requestBuilderContext = requestBuilderContext; 38 | } 39 | 40 | public HttpRequest getRequest() { 41 | return request; 42 | } 43 | 44 | public void setRequest(HttpRequest request) { 45 | this.request = request; 46 | } 47 | 48 | public void setChannel(Channel channel) { 49 | this.channel = channel; 50 | } 51 | 52 | public Channel getChannel() { 53 | return channel; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/util/FileReaderUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.util; 20 | 21 | import java.io.File; 22 | import java.io.FileInputStream; 23 | import java.io.IOException; 24 | 25 | /** 26 | * Util class for read file operations. 27 | */ 28 | public class FileReaderUtil { 29 | 30 | /** 31 | * Reads a file for a given file path. 32 | * @param filePath 33 | * @return String which contains the content of the file 34 | * @throws IOException 35 | */ 36 | public static String getFileBody(File filePath) throws IOException { 37 | 38 | FileInputStream fileInputStream = null; 39 | try { 40 | fileInputStream = new FileInputStream(filePath); 41 | int c; 42 | StringBuilder stringBuilder = new StringBuilder(); 43 | while ((c = fileInputStream.read()) != -1) { 44 | stringBuilder.append(c); 45 | } 46 | String content = stringBuilder.toString(); 47 | content = content.replace("\n", "").replace("\r", ""); 48 | 49 | return content; 50 | } finally { 51 | if (fileInputStream != null) { 52 | fileInputStream.close(); 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/example/http/SimpleEmulatorServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package org.wso2.carbon.protocol.emulator.example.http; 19 | 20 | import io.netty.handler.codec.http.HttpMethod; 21 | import io.netty.handler.codec.http.HttpResponseStatus; 22 | import org.wso2.carbon.protocol.emulator.dsl.Emulator; 23 | 24 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerConfigBuilderContext.configure; 25 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerRequestBuilderContext.request; 26 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerResponseBuilderContext.response; 27 | 28 | /** 29 | * Example class for simple emulator server. 30 | */ 31 | public class SimpleEmulatorServer { 32 | 33 | public static void main(String args[]) { 34 | 35 | Emulator.getHttpEmulator().server().given(configure().host("127.0.0.1").port(6065).context("/users")) 36 | .when(request().withMethod(HttpMethod.GET).withPath("user1/")) 37 | .then(response().withBody("User1").withStatusCode(HttpResponseStatus.OK) 38 | .withHeader("Header1", "value1")).when(request().withMethod(HttpMethod.GET).withPath("user2/")) 39 | .then(response().withStatusCode(HttpResponseStatus.NOT_FOUND)).operation().start(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/util/WireLogHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.util; 20 | 21 | import io.netty.channel.ChannelDuplexHandler; 22 | import org.apache.log4j.Logger; 23 | 24 | /** 25 | * Util class for wire logs. 26 | **/ 27 | public class WireLogHandler extends ChannelDuplexHandler { 28 | 29 | private static boolean wireLog = false; 30 | 31 | private static final Logger log = Logger.getLogger(WireLogHandler.class); 32 | 33 | public static void requestWireLog(Object msg) { 34 | String[] array = msg.toString().split("\\r?\\n"); 35 | for (int i = 0; i < array.length; i++) { 36 | log.info("wire << " + array[i]); 37 | } 38 | wireLog = true; 39 | } 40 | 41 | public static void responseWireLog(Object msg) { 42 | 43 | if (wireLog) { 44 | String[] array = msg.toString().split("\\r?\\n"); 45 | for (int i = 0; i < array.length; i++) { 46 | log.info("wire >> " + array[i]); 47 | } 48 | } 49 | } 50 | 51 | public static void logRequestBody(String body) { 52 | if (wireLog) { 53 | log.info("wire << " + body); 54 | } 55 | } 56 | 57 | public static void logResponseBody(String body) { 58 | if (wireLog) { 59 | log.info("wire >> " + body); 60 | } 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/RequestResponseCorrelation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | /** 22 | * Hold the contexts for request, expected response and actual response 23 | */ 24 | public class RequestResponseCorrelation { 25 | private HttpClientRequestBuilderContext requestBuilderContext; 26 | private HttpClientResponseBuilderContext expectedResponse; 27 | private HttpClientResponseProcessorContext receivedResponse; 28 | 29 | RequestResponseCorrelation(HttpClientRequestBuilderContext httpClientRequestBuilderContext, 30 | HttpClientResponseBuilderContext httpClientResponseBuilderContext) { 31 | requestBuilderContext = httpClientRequestBuilderContext; 32 | expectedResponse = httpClientResponseBuilderContext; 33 | } 34 | 35 | /** 36 | * Getter for requestBuilderContext 37 | */ 38 | public HttpClientRequestBuilderContext getRequestBuilderContext() { 39 | return requestBuilderContext; 40 | } 41 | 42 | 43 | public HttpClientResponseBuilderContext getExpectedResponse() { 44 | return expectedResponse; 45 | } 46 | 47 | public void setReceivedResponse(HttpClientResponseProcessorContext receivedResponse) { 48 | this.receivedResponse = receivedResponse; 49 | } 50 | 51 | public HttpClientResponseProcessorContext getReceivedResponse() { 52 | return receivedResponse; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpClientGivenBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractGivenBuilderContext; 22 | 23 | import java.util.ArrayList; 24 | 25 | /** 26 | * Class for HTTP Client's Configuration for a given Builder Context. 27 | */ 28 | public class HttpClientGivenBuilderContext extends AbstractGivenBuilderContext { 29 | private final ArrayList whenBuilderContextList; 30 | private HttpClientInformationContext httpClientInformationContext; 31 | private HttpClientConfigBuilderContext configurationContext; 32 | private HttpClientWhenBuilderContext whenBuilderContext; 33 | 34 | public HttpClientGivenBuilderContext(HttpClientInformationContext httpClientInformationContext) { 35 | whenBuilderContextList = new ArrayList(); 36 | this.httpClientInformationContext = httpClientInformationContext; 37 | } 38 | 39 | @Override 40 | public HttpClientWhenBuilderContext given(HttpClientConfigBuilderContext configurationContext) { 41 | this.configurationContext = configurationContext; 42 | httpClientInformationContext.setClientConfigBuilderContext(this.configurationContext); 43 | whenBuilderContext = new HttpClientWhenBuilderContext(whenBuilderContextList, httpClientInformationContext); 44 | return whenBuilderContext; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/handler/HttpChunkedSupportHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.handler; 20 | 21 | import io.netty.channel.ChannelHandlerContext; 22 | import io.netty.channel.ChannelInboundHandlerAdapter; 23 | import io.netty.handler.codec.http.HttpContent; 24 | import io.netty.handler.codec.http.HttpHeaders; 25 | import io.netty.handler.codec.http.HttpRequest; 26 | import io.netty.handler.codec.http.LastHttpContent; 27 | 28 | /** 29 | * This handler checks the http version to see if it can be handled. 30 | */ 31 | public class HttpChunkedSupportHandler extends ChannelInboundHandlerAdapter { 32 | 33 | private boolean connectionClosed = false; 34 | 35 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 36 | if (msg instanceof HttpRequest && HttpHeaders.Values.CHUNKED.equals( 37 | ((HttpRequest) msg).headers().get(HttpHeaders.Names.TRANSFER_ENCODING))) { 38 | ctx.close(); 39 | connectionClosed = true; 40 | } else if (msg instanceof HttpRequest) { 41 | ctx.fireChannelRead(msg); 42 | } else if (msg instanceof HttpContent && !connectionClosed) { 43 | ctx.fireChannelRead(msg); 44 | } else if (msg instanceof LastHttpContent && !connectionClosed) { 45 | ctx.fireChannelRead(msg); 46 | } else if (msg instanceof LastHttpContent && connectionClosed) { 47 | connectionClosed = false; 48 | } 49 | } 50 | } 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/example/http/SimpleEmulatorClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | package org.wso2.carbon.protocol.emulator.example.http; 19 | 20 | import io.netty.handler.codec.http.HttpMethod; 21 | import org.apache.log4j.Logger; 22 | import org.wso2.carbon.protocol.emulator.dsl.Emulator; 23 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientConfigBuilderContext; 24 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientRequestBuilderContext; 25 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseBuilderContext; 26 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseProcessorContext; 27 | 28 | /** 29 | * Example class for simple emulator client. 30 | */ 31 | public class SimpleEmulatorClient { 32 | 33 | private static final Logger log = Logger.getLogger(SimpleEmulatorClient.class); 34 | 35 | public static void main(String args[]) { 36 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 37 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 38 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user1").withMethod(HttpMethod.GET)) 39 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 40 | 41 | log.info("Response content =" + response.getReceivedResponseContext().getResponseBody()); 42 | log.info("Response status code =" + response.getReceivedResponseContext().getResponseStatus()); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/processors/HttpRequestResponseMatchingProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.processors; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpRequestContext; 22 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerProcessorContext; 23 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerRequestBuilderContext; 24 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerResponseBuilderContext; 25 | 26 | import java.util.Map; 27 | 28 | /** 29 | * Class for Http Request Response Matching Processor. 30 | */ 31 | public class HttpRequestResponseMatchingProcessor extends AbstractServerProcessor { 32 | 33 | @Override 34 | public void process(HttpServerProcessorContext processorContext) { 35 | Map requestResponseCorrelation 36 | = processorContext 37 | .getServerInformationContext().getRequestResponseCorrelation(); 38 | 39 | HttpRequestContext httpRequestContext = processorContext.getHttpRequestContext(); 40 | for (Map.Entry entry 41 | : requestResponseCorrelation 42 | .entrySet()) { 43 | if (entry.getKey().isMatch(httpRequestContext)) { 44 | processorContext.setSelectedResponseContext(entry.getValue()); 45 | break; 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/HttpServerThenBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractThenBuilderContext; 22 | 23 | import java.util.List; 24 | 25 | /** 26 | * Class for Http server Then builder context. 27 | */ 28 | public class HttpServerThenBuilderContext extends AbstractThenBuilderContext { 29 | private final HttpServerRequestBuilderContext requestContext; 30 | private final HttpServerInformationContext httpServerInformationContext; 31 | private HttpServerWhenBuilderContext whenBuilderContext; 32 | private List whenBuilderContextList; 33 | 34 | public HttpServerThenBuilderContext(List whenBuilderContextList, 35 | HttpServerRequestBuilderContext requestContext, 36 | HttpServerInformationContext httpServerInformationContext) { 37 | this.requestContext = requestContext; 38 | this.httpServerInformationContext = httpServerInformationContext; 39 | this.whenBuilderContextList = whenBuilderContextList; 40 | } 41 | 42 | @Override 43 | public HttpServerWhenBuilderContext then(HttpServerResponseBuilderContext responseContext) { 44 | whenBuilderContext = new HttpServerWhenBuilderContext(whenBuilderContextList, httpServerInformationContext); 45 | this.httpServerInformationContext.addCorrelation(requestContext, responseContext); 46 | return whenBuilderContext; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpClientThenBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractThenBuilderContext; 22 | 23 | import java.util.List; 24 | 25 | /** 26 | * Class for Http client Then builder context. 27 | */ 28 | public class HttpClientThenBuilderContext extends AbstractThenBuilderContext { 29 | private final HttpClientRequestBuilderContext requestContext; 30 | private final HttpClientInformationContext httpClientInformationContext; 31 | private final List whenBuilderContextList; 32 | private HttpClientWhenBuilderContext whenBuilderContext; 33 | 34 | public HttpClientThenBuilderContext(List whenBuilderContextList, 35 | HttpClientRequestBuilderContext requestContext, 36 | HttpClientInformationContext httpClientInformationContext) { 37 | this.requestContext = requestContext; 38 | this.httpClientInformationContext = httpClientInformationContext; 39 | this.whenBuilderContextList = whenBuilderContextList; 40 | } 41 | 42 | @Override 43 | public HttpClientWhenBuilderContext then(HttpClientResponseBuilderContext responseContext) { 44 | whenBuilderContext = new HttpClientWhenBuilderContext(whenBuilderContextList, httpClientInformationContext); 45 | this.httpClientInformationContext.addCorrelation(requestContext, responseContext); 46 | return whenBuilderContext; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpClientWhenBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractWhenBuilderContext; 22 | 23 | import java.util.List; 24 | 25 | /** 26 | * Class for Http client When builder context. 27 | */ 28 | public class HttpClientWhenBuilderContext extends AbstractWhenBuilderContext { 29 | private List whenBuilderContextList; 30 | private HttpClientInformationContext httpClientInformationContext; 31 | private HttpClientThenBuilderContext thenBuilderContext; 32 | 33 | public HttpClientWhenBuilderContext(List whenBuilderContextList, 34 | HttpClientInformationContext httpClientInformationContext) { 35 | this.httpClientInformationContext = httpClientInformationContext; 36 | this.whenBuilderContextList = whenBuilderContextList; 37 | this.whenBuilderContextList.add(this); 38 | } 39 | 40 | @Override 41 | public HttpClientThenBuilderContext when(HttpClientRequestBuilderContext requestContext) { 42 | httpClientInformationContext.setRequestContext(requestContext); 43 | thenBuilderContext = new HttpClientThenBuilderContext(whenBuilderContextList, requestContext, 44 | httpClientInformationContext); 45 | return thenBuilderContext; 46 | } 47 | 48 | @Override 49 | public HttpClientOperationBuilderContext operation() { 50 | return new HttpClientOperationBuilderContext(httpClientInformationContext); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/HttpServerGivenBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractGivenBuilderContext; 22 | 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | /** 27 | * Class for HTTP Server's Configuration for a given Builder Context. 28 | */ 29 | public class HttpServerGivenBuilderContext extends AbstractGivenBuilderContext { 30 | private final HttpServerInformationContext httpServerInformationContext; 31 | private HttpServerConfigBuilderContext configurationContext; 32 | private HttpServerWhenBuilderContext whenBuilderContext; 33 | private List whenBuilderContextList; 34 | 35 | public HttpServerGivenBuilderContext(HttpServerInformationContext httpServerInformationContext) { 36 | whenBuilderContextList = new ArrayList(); 37 | this.httpServerInformationContext = httpServerInformationContext; 38 | } 39 | 40 | @Override 41 | public HttpServerWhenBuilderContext given(HttpServerConfigBuilderContext configurationContext) { 42 | this.configurationContext = configurationContext; 43 | httpServerInformationContext.setServerConfigBuilderContext(this.configurationContext); 44 | whenBuilderContext = new HttpServerWhenBuilderContext(whenBuilderContextList, httpServerInformationContext); 45 | return whenBuilderContext; 46 | } 47 | 48 | public HttpServerConfigBuilderContext getConfigurationContext() { 49 | return configurationContext; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/util/ValidationUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.util; 20 | 21 | import org.apache.log4j.Logger; 22 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientConfigBuilderContext; 23 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerConfigBuilderContext; 24 | 25 | /** 26 | * Util class for Validation tasks. 27 | */ 28 | public class ValidationUtil { 29 | private static final Logger log = Logger.getLogger(ValidationUtil.class); 30 | 31 | public static void validateMandatoryParameters(HttpClientConfigBuilderContext clientConfigBuilderContext) { 32 | if (!isHostAvailable(clientConfigBuilderContext.getHost())) { 33 | log.error("Host is not given"); 34 | } 35 | 36 | if (!isPortAvailable(clientConfigBuilderContext.getPort())) { 37 | log.error("Host is not given"); 38 | } 39 | } 40 | 41 | public static void validateMandatoryParameters(HttpServerConfigBuilderContext serverConfigBuilderContext) { 42 | if (!isHostAvailable(serverConfigBuilderContext.getHost())) { 43 | log.error("Host is not given"); 44 | } 45 | 46 | if (!isPortAvailable(serverConfigBuilderContext.getPort())) { 47 | log.error("Host is not given"); 48 | } 49 | } 50 | 51 | private static boolean isHostAvailable(String host) { 52 | if (host == null) { 53 | return false; 54 | } 55 | return true; 56 | } 57 | 58 | private static boolean isPortAvailable(int port) { 59 | if (port == 0) { 60 | return false; 61 | } 62 | return true; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/HttpServerOperationBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import org.apache.log4j.Logger; 22 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractServerOperationBuilderContext; 23 | import org.wso2.carbon.protocol.emulator.util.ValidationUtil; 24 | 25 | /** 26 | * Class for Http Server Operation BuilderContext. 27 | */ 28 | public class HttpServerOperationBuilderContext extends AbstractServerOperationBuilderContext { 29 | private static final Logger log = Logger.getLogger(HttpServerOperationBuilderContext.class); 30 | private HttpServerInformationContext httpServerInformationContext; 31 | 32 | public HttpServerOperationBuilderContext(HttpServerInformationContext httpServerInformationContext) { 33 | this.httpServerInformationContext = httpServerInformationContext; 34 | } 35 | 36 | @Override 37 | public HttpServerOperationBuilderContext start() { 38 | ValidationUtil.validateMandatoryParameters(httpServerInformationContext.getServerConfigBuilderContext()); 39 | 40 | try { 41 | httpServerInformationContext.getHttpServerInitializer().start(); 42 | } catch (Exception e) { 43 | log.error("Exception occurred while starting the Emulator server", e); 44 | } 45 | return this; 46 | } 47 | 48 | @Override 49 | public HttpServerOperationBuilderContext stop() { 50 | try { 51 | httpServerInformationContext.getHttpServerInitializer().shutdown(); 52 | } catch (Exception e) { 53 | log.error("Exception occurred while stopping the Emulator server", e); 54 | } 55 | return this; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/HTTPProtocolEmulator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.AbstractProtocolEmulator; 22 | import org.wso2.carbon.protocol.emulator.http.client.HttpClientInitializer; 23 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientGivenBuilderContext; 24 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientInformationContext; 25 | import org.wso2.carbon.protocol.emulator.http.server.HttpServerInitializer; 26 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerGivenBuilderContext; 27 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerInformationContext; 28 | 29 | /** 30 | * Class for HTTP Protocol Emulator. 31 | */ 32 | public class HTTPProtocolEmulator extends AbstractProtocolEmulator { 33 | 34 | @Override 35 | public HttpServerGivenBuilderContext server() { 36 | HttpServerInformationContext serverInformationContext = new HttpServerInformationContext(); 37 | HttpServerInitializer serverInitializer = new HttpServerInitializer(serverInformationContext); 38 | serverInformationContext.setHttpServerInitializer(serverInitializer); 39 | return new HttpServerGivenBuilderContext(serverInformationContext); 40 | } 41 | 42 | @Override 43 | public HttpClientGivenBuilderContext client() { 44 | HttpClientInformationContext clientInformationContext = new HttpClientInformationContext(); 45 | HttpClientInitializer clientInitializer = new HttpClientInitializer(clientInformationContext); 46 | clientInformationContext.setClientInitializer(clientInitializer); 47 | return new HttpClientGivenBuilderContext(clientInformationContext); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/tcp/client/context/TcpClientConfigBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.tcp.client.context; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractConfigurationBuilderContext; 22 | 23 | /** 24 | * Class for ClientConfigBuilderContext of TCP Protocol. 25 | */ 26 | public class TcpClientConfigBuilderContext extends AbstractConfigurationBuilderContext { 27 | 28 | private static TcpClientConfigBuilderContext clientConfigBuilderContext; 29 | private String host; 30 | private int port; 31 | private int readingDelay; 32 | 33 | private static TcpClientConfigBuilderContext getInstance() { 34 | clientConfigBuilderContext = new TcpClientConfigBuilderContext(); 35 | return clientConfigBuilderContext; 36 | } 37 | 38 | public static TcpClientConfigBuilderContext configure() { 39 | return getInstance(); 40 | } 41 | 42 | @Override 43 | public TcpClientConfigBuilderContext host(String host) { 44 | this.host = host; 45 | return this; 46 | } 47 | 48 | @Override 49 | public TcpClientConfigBuilderContext port(int port) { 50 | this.port = port; 51 | return this; 52 | } 53 | 54 | public TcpClientConfigBuilderContext readingDelay(int readingDelay) { 55 | this.readingDelay = readingDelay; 56 | return this; 57 | } 58 | 59 | public String getHost() { 60 | return host; 61 | } 62 | 63 | public void setHost(String host) { 64 | this.host = host; 65 | } 66 | 67 | public int getPort() { 68 | return port; 69 | } 70 | 71 | public void setPort(int port) { 72 | this.port = port; 73 | } 74 | 75 | public int getReadingDelay() { 76 | return readingDelay; 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## Purpose 2 | > Describe the problems, issues, or needs driving this feature/fix and include links to related issues in the following format: Resolves issue1, issue2, etc. 3 | 4 | ## Goals 5 | > Describe the solutions that this feature/fix will introduce to resolve the problems described above 6 | 7 | ## Approach 8 | > Describe how you are implementing the solutions. Include an animated GIF or screenshot if the change affects the UI (email documentation@wso2.com to review all UI text). Include a link to a Markdown file or Google doc if the feature write-up is too long to paste here. 9 | 10 | ## User stories 11 | > Summary of user stories addressed by this change> 12 | 13 | ## Release note 14 | > Brief description of the new feature or bug fix as it will appear in the release notes 15 | 16 | ## Documentation 17 | > Link(s) to product documentation that addresses the changes of this PR. If no doc impact, enter “N/A” plus brief explanation of why there’s no doc impact 18 | 19 | ## Training 20 | > Link to the PR for changes to the training content in https://github.com/wso2/WSO2-Training, if applicable 21 | 22 | ## Certification 23 | > Type “Sent” when you have provided new/updated certification questions, plus four answers for each question (correct answer highlighted in bold), based on this change. Certification questions/answers should be sent to certification@wso2.com and NOT pasted in this PR. If there is no impact on certification exams, type “N/A” and explain why. 24 | 25 | ## Marketing 26 | > Link to drafts of marketing content that will describe and promote this feature, including product page changes, technical articles, blog posts, videos, etc., if applicable 27 | 28 | ## Automation tests 29 | - Unit tests 30 | > Code coverage information 31 | - Integration tests 32 | > Details about the test cases and coverage 33 | 34 | ## Security checks 35 | - Followed secure coding standards in http://wso2.com/technical-reports/wso2-secure-engineering-guidelines? yes/no 36 | - Ran FindSecurityBugs plugin and verified report? yes/no 37 | - Confirmed that this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets? yes/no 38 | 39 | ## Samples 40 | > Provide high-level details about the samples related to this feature 41 | 42 | ## Related PRs 43 | > List any other related PRs 44 | 45 | ## Migrations (if applicable) 46 | > Describe migration steps and platforms on which migration has been tested 47 | 48 | ## Test environment 49 | > List all JDK versions, operating systems, databases, and browser/versions on which this feature/fix was tested 50 | 51 | ## Learning 52 | > Describe the research phase and any blog posts, patterns, libraries, or add-ons you used to solve the problem. -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/HttpServerInformationContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.server.HttpServerInitializer; 22 | 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | /** 27 | * Class for HTTP server request response information context. 28 | */ 29 | public class HttpServerInformationContext { 30 | private HttpServerConfigBuilderContext serverConfigBuilderContext; 31 | private Map correlation; 32 | private HttpServerInitializer httpServerInitializer; 33 | 34 | public UtilityContext getUtilityContext() { 35 | return new UtilityContext(); 36 | } 37 | 38 | public HttpServerConfigBuilderContext getServerConfigBuilderContext() { 39 | return serverConfigBuilderContext; 40 | } 41 | 42 | public void setServerConfigBuilderContext(HttpServerConfigBuilderContext serverConfigBuilderContext) { 43 | this.serverConfigBuilderContext = serverConfigBuilderContext; 44 | } 45 | 46 | public Map getRequestResponseCorrelation() { 47 | return correlation; 48 | } 49 | 50 | public void addCorrelation(HttpServerRequestBuilderContext httpServerRequestBuilderContext, 51 | HttpServerResponseBuilderContext httpServerResponseBuilderContext) { 52 | if (correlation == null) { 53 | this.correlation = new HashMap(); 54 | } 55 | correlation.put(httpServerRequestBuilderContext, httpServerResponseBuilderContext); 56 | } 57 | 58 | public HttpServerInitializer getHttpServerInitializer() { 59 | return httpServerInitializer; 60 | } 61 | 62 | public void setHttpServerInitializer(HttpServerInitializer httpServerInitializer) { 63 | this.httpServerInitializer = httpServerInitializer; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/HttpServerWhenBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractWhenBuilderContext; 22 | 23 | import java.util.List; 24 | 25 | /** 26 | * Class for Http server When builder context. 27 | */ 28 | public class HttpServerWhenBuilderContext extends AbstractWhenBuilderContext { 29 | private final HttpServerInformationContext httpServerInformationContext; 30 | private HttpServerThenBuilderContext thenBuilderContext; 31 | private List whenBuilderContextList; 32 | private HttpServerRequestBuilderContext requestContext; 33 | private HttpServerOperationBuilderContext httpServerOperationBuilderContext; 34 | 35 | public HttpServerWhenBuilderContext(List whenBuilderContextList, 36 | HttpServerInformationContext httpServerInformationContext) { 37 | this.httpServerInformationContext = httpServerInformationContext; 38 | this.whenBuilderContextList = whenBuilderContextList; 39 | this.whenBuilderContextList.add(this); 40 | } 41 | 42 | @Override 43 | public HttpServerThenBuilderContext when(HttpServerRequestBuilderContext requestContext) { 44 | this.requestContext = requestContext; 45 | this.requestContext.buildPathRegex(httpServerInformationContext.getServerConfigBuilderContext().getContext()); 46 | thenBuilderContext = new HttpServerThenBuilderContext(whenBuilderContextList, requestContext, 47 | httpServerInformationContext); 48 | return thenBuilderContext; 49 | } 50 | 51 | @Override 52 | public HttpServerOperationBuilderContext operation() { 53 | this.httpServerOperationBuilderContext = new HttpServerOperationBuilderContext(httpServerInformationContext); 54 | return httpServerOperationBuilderContext; 55 | } 56 | 57 | public HttpServerRequestBuilderContext getRequestContext() { 58 | return requestContext; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpClientOperationBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import org.apache.log4j.Logger; 22 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractClientOperationBuilderContext; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * Class for request sending operation of HTTP client. 28 | */ 29 | public class HttpClientOperationBuilderContext extends AbstractClientOperationBuilderContext { 30 | private static final Logger log = Logger.getLogger(HttpClientOperationBuilderContext.class); 31 | private HttpClientInformationContext httpClientInformationContext; 32 | 33 | public HttpClientOperationBuilderContext(HttpClientInformationContext httpClientInformationContext) { 34 | this.httpClientInformationContext = httpClientInformationContext; 35 | } 36 | 37 | @Override 38 | public HttpClientResponseProcessorContext send() { //recommanded for a one request only 39 | try { 40 | this.httpClientInformationContext.getClientInitializer().initialize(); 41 | } catch (Exception e) { 42 | log.error("Exception occurred while sending message" + e); 43 | } 44 | return httpClientInformationContext.getLastReceivedResponseProcessContext(); 45 | } 46 | 47 | @Override 48 | public HttpClientOperationBuilderContext sendAsync() { //recommanded for a one request only 49 | try { 50 | this.httpClientInformationContext.getClientInitializer().initializeAsync(); 51 | } catch (Exception e) { 52 | log.error("Exception occurred while sending message", e); 53 | } 54 | return this; 55 | } 56 | 57 | @Override 58 | public List shutdown() { 59 | try { 60 | this.httpClientInformationContext.getClientInitializer().shutdownClients(); 61 | } catch (InterruptedException e) { 62 | log.error("Exception occurred while shutting down clients", e); 63 | } 64 | return httpClientInformationContext.getRequestResponseCorrelation(); 65 | } 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/MockServerThread.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import io.netty.channel.ChannelFutureListener; 22 | import io.netty.channel.ChannelHandlerContext; 23 | import io.netty.handler.codec.http.FullHttpResponse; 24 | import io.netty.handler.codec.http.HttpHeaders; 25 | import io.netty.handler.codec.http.HttpRequest; 26 | import org.apache.log4j.Logger; 27 | 28 | import java.util.concurrent.DelayQueue; 29 | 30 | /** 31 | * Class for create a mock server thread. 32 | */ 33 | public class MockServerThread extends Thread { 34 | private static final Logger log = Logger.getLogger(MockServerThread.class); 35 | 36 | private final DelayQueue queue = new DelayQueue(); 37 | 38 | public void run() { 39 | DelayedElement elem; 40 | 41 | while (true) { 42 | try { 43 | elem = queue.take(); 44 | beginResponse(elem.getContext(), elem.getProcessorContext()); 45 | } catch (InterruptedException e) { 46 | log.error(e); 47 | } 48 | } 49 | } 50 | 51 | public void delayEvent(ChannelHandlerContext ctx, HttpServerProcessorContext processorContext, int delay, int id) { 52 | DelayedElement delayedElement = new DelayedElement(ctx, processorContext, System.currentTimeMillis(), delay); 53 | queue.add(delayedElement); 54 | } 55 | 56 | private void beginResponse(ChannelHandlerContext ctx, HttpServerProcessorContext context) { 57 | writeResponse(ctx, context); 58 | } 59 | 60 | private void writeResponse(ChannelHandlerContext ctx, HttpServerProcessorContext context) { 61 | // Decide whether to close the connection or not. 62 | HttpRequest request = context.getHttpRequest(); 63 | 64 | boolean keepAlive = HttpHeaders.isKeepAlive(request); 65 | // Build the response object. 66 | FullHttpResponse response = context.getFinalResponse(); 67 | if (keepAlive) { 68 | ctx.write(response); 69 | } else { 70 | ctx.write(response).addListener(ChannelFutureListener.CLOSE); 71 | } 72 | ctx.channel().flush(); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpResponseContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import io.netty.handler.codec.http.HttpResponseStatus; 22 | 23 | import java.util.ArrayList; 24 | import java.util.HashMap; 25 | import java.util.List; 26 | import java.util.Map; 27 | 28 | /** 29 | * Class for response information of the server. 30 | */ 31 | public class HttpResponseContext { 32 | private Map> headerParameters; 33 | private Map> cookieParameters; 34 | private StringBuffer responseBody; 35 | private HttpResponseStatus responseStatus; 36 | 37 | public void addHeaderParameter(String key, String value) { 38 | if (headerParameters == null) { 39 | this.headerParameters = new HashMap>(); 40 | } 41 | 42 | List headerValues = this.headerParameters.get(key); 43 | if (headerValues == null) { 44 | headerValues = new ArrayList(); 45 | } 46 | headerValues.add(value); 47 | this.headerParameters.put(key, headerValues); 48 | } 49 | 50 | public void addCookieParameter(String key, String value) { 51 | if (cookieParameters == null) { 52 | this.cookieParameters = new HashMap>(); 53 | } 54 | List cookieValues = this.cookieParameters.get(key); 55 | if (cookieValues == null) { 56 | cookieValues = new ArrayList(); 57 | } 58 | cookieValues.add(value); 59 | this.cookieParameters.put(key, cookieValues); 60 | } 61 | 62 | public Map> getHeaderParameters() { 63 | return headerParameters; 64 | } 65 | 66 | public Map> getCookieParameters() { 67 | return cookieParameters; 68 | } 69 | 70 | public void appendResponseContent(Object content) { 71 | if (responseBody == null) { 72 | this.responseBody = new StringBuffer(); 73 | } 74 | this.responseBody.append(content); 75 | } 76 | 77 | public String getResponseBody() { 78 | if (responseBody == null) { 79 | return null; 80 | } 81 | return responseBody.toString(); 82 | } 83 | 84 | public HttpResponseStatus getResponseStatus() { 85 | return responseStatus; 86 | } 87 | 88 | public void setResponseStatus(HttpResponseStatus responseStatus) { 89 | this.responseStatus = responseStatus; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpClientInformationContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.http.client.HttpClientInitializer; 22 | 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | /** 27 | * Class for HTTP client request response information context. 28 | */ 29 | public class HttpClientInformationContext { 30 | private HttpClientConfigBuilderContext clientConfigBuilderContext; 31 | private List correlation; 32 | private HttpClientInitializer clientInitializer; 33 | private HttpClientRequestBuilderContext requestContext; 34 | private HttpClientResponseProcessorContext receivedProcessContext; 35 | 36 | public HttpClientConfigBuilderContext getClientConfigBuilderContext() { 37 | return clientConfigBuilderContext; 38 | } 39 | 40 | public void setClientConfigBuilderContext(HttpClientConfigBuilderContext clientConfigBuilderContext) { 41 | this.clientConfigBuilderContext = clientConfigBuilderContext; 42 | } 43 | 44 | public List getRequestResponseCorrelation() { 45 | return correlation; 46 | } 47 | 48 | public void addCorrelation(HttpClientRequestBuilderContext httpClientRequestBuilderContext, 49 | HttpClientResponseBuilderContext expectedResponseContext) { 50 | if (correlation == null) { 51 | this.correlation = new ArrayList<>(); 52 | } 53 | correlation.add(new RequestResponseCorrelation(httpClientRequestBuilderContext, expectedResponseContext)); 54 | } 55 | 56 | public HttpClientInitializer getClientInitializer() { 57 | return clientInitializer; 58 | } 59 | 60 | public void setClientInitializer(HttpClientInitializer clientInitializer) { 61 | this.clientInitializer = clientInitializer; 62 | } 63 | 64 | public HttpClientRequestBuilderContext getRequestContext() { 65 | return requestContext; 66 | } 67 | 68 | public void setRequestContext(HttpClientRequestBuilderContext requestContext) { 69 | this.requestContext = requestContext; 70 | } 71 | 72 | public HttpClientResponseProcessorContext getLastReceivedResponseProcessContext() { 73 | return receivedProcessContext; 74 | } 75 | 76 | public void setReceivedResponseProcessContext(HttpClientResponseProcessorContext receivedProcessContext) { 77 | this.receivedProcessContext = receivedProcessContext; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/DelayedElement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import io.netty.channel.ChannelHandlerContext; 22 | 23 | import java.util.concurrent.Delayed; 24 | import java.util.concurrent.TimeUnit; 25 | 26 | /** 27 | * Class for Delay element of the fast backend. 28 | */ 29 | public class DelayedElement implements Delayed { 30 | protected long timestamp; 31 | private long delay; 32 | private ChannelHandlerContext ctx; 33 | private HttpServerProcessorContext context; 34 | 35 | public DelayedElement(ChannelHandlerContext ctx, HttpServerProcessorContext context, long receivedTime, int delay) { 36 | this.delay = delay; 37 | this.ctx = ctx; 38 | this.context = context; 39 | this.timestamp = receivedTime; 40 | } 41 | 42 | @Override 43 | public long getDelay(TimeUnit unit) { 44 | return delay - (System.currentTimeMillis() - this.timestamp); 45 | } 46 | 47 | @Override 48 | public int compareTo(Delayed other) { 49 | long comparison = ((DelayedElement) other).timestamp - this.timestamp; 50 | if (comparison > 0) { 51 | return -1; 52 | } else if (comparison < 0) { 53 | return 1; 54 | } else { 55 | return 0; 56 | } 57 | } 58 | 59 | @Override 60 | public boolean equals(Object o) { 61 | if (this == o) { 62 | return true; 63 | } 64 | if (o == null || getClass() != o.getClass()) { 65 | return false; 66 | } 67 | 68 | DelayedElement that = (DelayedElement) o; 69 | if (delay != that.delay) { 70 | return false; 71 | } 72 | if (timestamp != that.timestamp) { 73 | return false; 74 | } 75 | if (ctx != null ? !ctx.equals(that.ctx) : that.ctx != null) { 76 | return false; 77 | } 78 | return !(context != null ? !context.equals(that.context) : that.context != null); 79 | } 80 | 81 | @Override 82 | public int hashCode() { 83 | int result = (int) (delay ^ (delay >>> 32)); 84 | result = 31 * result + (ctx != null ? ctx.hashCode() : 0); 85 | result = 31 * result + (context != null ? context.hashCode() : 0); 86 | result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)); 87 | return result; 88 | } 89 | 90 | public ChannelHandlerContext getContext() { 91 | return ctx; 92 | } 93 | 94 | public HttpServerProcessorContext getProcessorContext() { 95 | return this.context; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/HttpServerProcessorContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import io.netty.handler.codec.http.FullHttpResponse; 22 | import io.netty.handler.codec.http.HttpContent; 23 | import io.netty.handler.codec.http.HttpRequest; 24 | 25 | /** 26 | * Class for Http Server Processor Context. 27 | */ 28 | public class HttpServerProcessorContext { 29 | private HttpServerInformationContext serverInformationContext; 30 | private HttpRequestContext httpRequestContext; 31 | private HttpRequest httpRequest; 32 | private HttpContent httpContent; 33 | private HttpServerResponseBuilderContext selectedResponseContext; 34 | private FullHttpResponse finalResponse; 35 | private HttpServerRequestBuilderContext requestBuilderContext; 36 | 37 | public HttpServerRequestBuilderContext getRequestBuilderContext() { 38 | return requestBuilderContext; 39 | } 40 | 41 | public void setRequestBuilderContext(HttpServerRequestBuilderContext requestBuilderContext) { 42 | this.requestBuilderContext = requestBuilderContext; 43 | } 44 | 45 | public HttpServerInformationContext getServerInformationContext() { 46 | return serverInformationContext; 47 | } 48 | 49 | public void setServerInformationContext(HttpServerInformationContext serverInformationContext) { 50 | this.serverInformationContext = serverInformationContext; 51 | } 52 | 53 | public HttpRequestContext getHttpRequestContext() { 54 | return httpRequestContext; 55 | } 56 | 57 | public void setHttpRequestContext(HttpRequestContext httpRequestContext) { 58 | this.httpRequestContext = httpRequestContext; 59 | } 60 | 61 | public HttpRequest getHttpRequest() { 62 | return httpRequest; 63 | } 64 | 65 | public void setHttpRequest(HttpRequest httpRequest) { 66 | this.httpRequest = httpRequest; 67 | } 68 | 69 | public HttpContent getHttpContent() { 70 | return httpContent; 71 | } 72 | 73 | public void setHttpContent(HttpContent httpContent) { 74 | this.httpContent = httpContent; 75 | } 76 | 77 | public HttpServerResponseBuilderContext getSelectedResponseContext() { 78 | return selectedResponseContext; 79 | } 80 | 81 | public void setSelectedResponseContext(HttpServerResponseBuilderContext selectedResponseContext) { 82 | this.selectedResponseContext = selectedResponseContext; 83 | } 84 | 85 | public FullHttpResponse getFinalResponse() { 86 | return finalResponse; 87 | } 88 | 89 | public void setFinalResponse(FullHttpResponse finalResponse) { 90 | this.finalResponse = finalResponse; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/handler/HttpChunkedWriteHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.handler; 20 | 21 | import io.netty.channel.ChannelHandlerContext; 22 | import io.netty.channel.ChannelPromise; 23 | import io.netty.handler.stream.ChunkedWriteHandler; 24 | import org.apache.log4j.Logger; 25 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerInformationContext; 26 | 27 | import java.util.concurrent.Callable; 28 | import java.util.concurrent.Executors; 29 | import java.util.concurrent.ScheduledExecutorService; 30 | 31 | /** 32 | * Class to handle chuncked HTTP writing. 33 | */ 34 | public class HttpChunkedWriteHandler extends ChunkedWriteHandler { 35 | private static final Logger log = Logger.getLogger(HttpChunkedWriteHandler.class); 36 | static Callable callable = new Callable() { 37 | public Object call() throws Exception { 38 | return "Writing"; 39 | } 40 | }; 41 | private final HttpServerInformationContext serverInformationContext; 42 | private final ScheduledExecutorService scheduledWritingExecutorService; 43 | private final int corePoolSize = 10; 44 | 45 | public HttpChunkedWriteHandler(HttpServerInformationContext serverInformationContext) { 46 | this.serverInformationContext = serverInformationContext; 47 | scheduledWritingExecutorService = Executors.newScheduledThreadPool(corePoolSize); 48 | } 49 | 50 | @Override 51 | public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { 52 | 53 | Boolean writingConnectionDrop = serverInformationContext.getServerConfigBuilderContext() 54 | .getWritingConnectionDrop(); 55 | if (writingConnectionDrop != null && writingConnectionDrop == true) { 56 | ctx.channel().close(); 57 | log.info("101505--Connection dropped/closed while writing data to channel"); 58 | } 59 | 60 | int writeTimeOut = serverInformationContext.getServerConfigBuilderContext().getWriteTimeOut(); 61 | 62 | if (writeTimeOut == 0) { 63 | writeTimeOut = Integer.MAX_VALUE; 64 | } 65 | 66 | Thread thread = new Thread(() -> { 67 | try { 68 | super.write(ctx, msg, promise); 69 | } catch (Exception e) { 70 | log.error("Error while writing data", e); 71 | } 72 | }); 73 | thread.start(); 74 | long endTimeMillis = System.currentTimeMillis() + writeTimeOut; 75 | while (thread.isAlive()) { 76 | if (System.currentTimeMillis() > endTimeMillis) { 77 | ctx.channel().close(); 78 | log.info("101504--Connection timeout occurred while writing data to the Channel"); 79 | break; 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/HttpRequestContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import io.netty.handler.codec.http.HttpMethod; 22 | import io.netty.handler.codec.http.HttpVersion; 23 | 24 | import java.util.ArrayList; 25 | import java.util.HashMap; 26 | import java.util.List; 27 | import java.util.Map; 28 | 29 | /** 30 | * Class for HttpRequestContext. 31 | */ 32 | public class HttpRequestContext { 33 | private Map> headerParameters; 34 | private Map> queryParameters; 35 | private StringBuffer requestBody; 36 | private String uri; 37 | private HttpMethod httpMethod; 38 | private HttpVersion httpVersion; 39 | private boolean isKeepAlive; 40 | 41 | 42 | /** 43 | * Add Header Parameters. 44 | * @param key 45 | * @param value 46 | */ 47 | public void addHeaderParameter(String key, String value) { 48 | if (headerParameters == null) { 49 | this.headerParameters = new HashMap>(); 50 | } 51 | 52 | List headerValues = this.headerParameters.get(key); 53 | if (headerValues == null) { 54 | headerValues = new ArrayList(); 55 | } 56 | headerValues.add(value); 57 | this.headerParameters.put(key, headerValues); 58 | } 59 | 60 | public Map> getQueryParameters() { 61 | return queryParameters; 62 | } 63 | 64 | public void setQueryParameters(Map> queryParameters) { 65 | this.queryParameters = queryParameters; 66 | } 67 | 68 | public Map> getHeaderParameters() { 69 | return headerParameters; 70 | } 71 | 72 | public void appendResponseContent(Object content) { 73 | if (requestBody == null) { 74 | this.requestBody = new StringBuffer(); 75 | } 76 | this.requestBody.append(content); 77 | } 78 | 79 | public String getUri() { 80 | return uri; 81 | } 82 | 83 | public void setUri(String uri) { 84 | this.uri = uri; 85 | } 86 | 87 | public HttpMethod getHttpMethod() { 88 | return httpMethod; 89 | } 90 | 91 | public void setHttpMethod(HttpMethod httpMethod) { 92 | this.httpMethod = httpMethod; 93 | } 94 | 95 | public HttpVersion getHttpVersion() { 96 | return httpVersion; 97 | } 98 | 99 | public void setHttpVersion(HttpVersion httpVersion) { 100 | this.httpVersion = httpVersion; 101 | } 102 | 103 | public boolean isKeepAlive() { 104 | return isKeepAlive; 105 | } 106 | 107 | public void setKeepAlive(boolean isKeepAlive) { 108 | this.isKeepAlive = isKeepAlive; 109 | } 110 | 111 | public String getRequestBody() { 112 | if (requestBody == null) { 113 | return null; 114 | } 115 | return requestBody.toString(); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/processors/HttpResponseInformationProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.processors; 20 | 21 | import io.netty.buffer.ByteBuf; 22 | import io.netty.handler.codec.DecoderResult; 23 | import io.netty.handler.codec.http.HttpHeaders; 24 | import io.netty.handler.codec.http.HttpObject; 25 | import io.netty.util.CharsetUtil; 26 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseProcessorContext; 27 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpResponseContext; 28 | 29 | import java.util.Map; 30 | 31 | /** 32 | * Class to process the received HTTP response. 33 | */ 34 | public class HttpResponseInformationProcessor extends AbstractClientProcessor { 35 | 36 | @Override 37 | public void process(HttpClientResponseProcessorContext processorContext) { 38 | 39 | HttpResponseContext httpResponseContext = new HttpResponseContext(); 40 | processorContext.setReceivedResponseContext(httpResponseContext); 41 | processorContext.setReceivedResponseContext(httpResponseContext); 42 | populateResponseHeaders(processorContext); 43 | populateResponseCookies(processorContext); 44 | populateResponseStatusCode(processorContext); 45 | } 46 | 47 | private void populateResponseHeaders(HttpClientResponseProcessorContext processorContext) { 48 | HttpHeaders headers = processorContext.getReceivedResponse().headers(); 49 | if (!headers.isEmpty()) { 50 | for (Map.Entry entry : headers.entries()) { 51 | processorContext.getReceivedResponseContext().addHeaderParameter(entry.getKey(), entry.getValue()); 52 | } 53 | } 54 | } 55 | 56 | private void populateResponseCookies(HttpClientResponseProcessorContext processorContext) { 57 | 58 | /*Cookie cookie = processorContext.getReceivedResponse(). 59 | 60 | Map> cookieParameters = 61 | processorContext.getReceivedResponseContext().getCookieParameters(); 62 | 63 | if (!cookieParameters.isEmpty()){ 64 | for (Map.Entry> entry : cookieParameters.entrySet()){ 65 | processorContext.getReceivedResponseContext().addCookieParameter(entry.getKey(),entry.getValue()); 66 | } 67 | }*/ 68 | } 69 | 70 | private void populateResponseStatusCode(HttpClientResponseProcessorContext processorContext) { 71 | processorContext.getReceivedResponseContext() 72 | .setResponseStatus(processorContext.getReceivedResponse().getStatus()); 73 | } 74 | 75 | public void appendDecoderResult(HttpResponseContext responseContext, HttpObject httpObject, ByteBuf content) { 76 | responseContext.appendResponseContent(content.toString(CharsetUtil.UTF_8)); 77 | DecoderResult result = httpObject.getDecoderResult(); 78 | if (result.isSuccess()) { 79 | return; 80 | } 81 | responseContext.appendResponseContent(result.cause()); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/handler/HttpVersionHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.handler; 20 | 21 | import io.netty.buffer.Unpooled; 22 | import io.netty.channel.ChannelHandlerContext; 23 | import io.netty.channel.ChannelInboundHandlerAdapter; 24 | import io.netty.handler.codec.http.DefaultFullHttpResponse; 25 | import io.netty.handler.codec.http.FullHttpResponse; 26 | import io.netty.handler.codec.http.HttpContent; 27 | import io.netty.handler.codec.http.HttpHeaders; 28 | import io.netty.handler.codec.http.HttpRequest; 29 | import io.netty.handler.codec.http.HttpVersion; 30 | import io.netty.handler.codec.http.LastHttpContent; 31 | import io.netty.util.CharsetUtil; 32 | 33 | import static io.netty.handler.codec.http.HttpResponseStatus.HTTP_VERSION_NOT_SUPPORTED; 34 | 35 | /** 36 | * This handler checks the http version to see if it can be handled. 37 | */ 38 | public class HttpVersionHandler extends ChannelInboundHandlerAdapter { 39 | private HttpVersion httpVersion; 40 | private boolean responded = false; 41 | 42 | public HttpVersionHandler(HttpVersion httpVersion) { 43 | this.httpVersion = httpVersion; 44 | } 45 | 46 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 47 | versionCheck(ctx, msg); 48 | } 49 | 50 | 51 | private void versionCheck(ChannelHandlerContext ctx, Object msg) { 52 | if (msg instanceof HttpRequest && !httpVersion.equals(((HttpRequest) msg).getProtocolVersion())) { 53 | FullHttpResponse response = populate505VersionError(httpVersion, ((HttpRequest) msg).getProtocolVersion()); 54 | ctx.writeAndFlush(response); 55 | responded = true; 56 | } else if (msg instanceof HttpRequest) { 57 | ctx.fireChannelRead(msg); 58 | } else if (msg instanceof HttpContent && !responded) { 59 | ctx.fireChannelRead(msg); 60 | } else if (msg instanceof LastHttpContent && !responded) { 61 | ctx.fireChannelRead(msg); 62 | } else if (msg instanceof LastHttpContent && responded) { 63 | responded = false; 64 | } 65 | } 66 | 67 | private FullHttpResponse populate505VersionError(HttpVersion httpVersion, HttpVersion protocolVersion) { 68 | FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, HTTP_VERSION_NOT_SUPPORTED, 69 | Unpooled.copiedBuffer("The " + protocolVersion + 70 | " is not supported " + 71 | "because of the " + 72 | "configurations\n", 73 | CharsetUtil.UTF_8) 74 | ); 75 | response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); 76 | response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); 77 | return response; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/SSLHandlerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http; 20 | 21 | import io.netty.handler.ssl.SslHandler; 22 | 23 | import java.io.File; 24 | import java.io.FileInputStream; 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | import java.security.KeyManagementException; 28 | import java.security.KeyStore; 29 | import java.security.KeyStoreException; 30 | import java.security.NoSuchAlgorithmException; 31 | import java.security.Security; 32 | import java.security.UnrecoverableKeyException; 33 | import java.security.cert.CertificateException; 34 | import javax.net.ssl.KeyManager; 35 | import javax.net.ssl.KeyManagerFactory; 36 | import javax.net.ssl.SSLContext; 37 | import javax.net.ssl.SSLEngine; 38 | import javax.net.ssl.TrustManager; 39 | import javax.net.ssl.TrustManagerFactory; 40 | 41 | 42 | /** 43 | * Class for SSL Handler Factory. 44 | */ 45 | public class SSLHandlerFactory { 46 | private static final String protocol = "TLS"; 47 | private final SSLContext serverContext; 48 | private boolean needClientAuth; 49 | 50 | public SSLHandlerFactory(SSLConfig sslConfig) { 51 | String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm"); 52 | if (algorithm == null) { 53 | algorithm = "sunX509"; 54 | } 55 | try { 56 | KeyStore ks = getKeyStore(sslConfig.getKeyStore(), sslConfig.getKeyStorePass()); 57 | // Set up key manager factory to use our key store 58 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm); 59 | kmf.init(ks, sslConfig.getCertPass() != null ? 60 | sslConfig.getCertPass().toCharArray() : 61 | sslConfig.getKeyStorePass().toCharArray()); 62 | KeyManager[] keyManagers = kmf.getKeyManagers(); 63 | TrustManager[] trustManagers = null; 64 | if (sslConfig.getTrustStore() != null) { 65 | this.needClientAuth = true; 66 | KeyStore tks = getKeyStore(sslConfig.getTrustStore(), sslConfig.getTrustStorePass()); 67 | TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm); 68 | tmf.init(tks); 69 | trustManagers = tmf.getTrustManagers(); 70 | } 71 | serverContext = SSLContext.getInstance(protocol); 72 | serverContext.init(keyManagers, trustManagers, null); 73 | } catch (UnrecoverableKeyException | KeyManagementException | 74 | NoSuchAlgorithmException | KeyStoreException | IOException e) { 75 | throw new IllegalArgumentException("Failed to initialize the server-side SSLContext", e); 76 | } 77 | } 78 | 79 | private static KeyStore getKeyStore(File keyStore, String keyStorePassword) throws IOException { 80 | KeyStore ks; 81 | try (InputStream is = new FileInputStream(keyStore)) { 82 | ks = KeyStore.getInstance("JKS"); 83 | ks.load(is, keyStorePassword.toCharArray()); 84 | 85 | } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException e) { 86 | throw new IOException(e); 87 | } 88 | return ks; 89 | } 90 | 91 | /** 92 | * @return instance of {@code SslHandler} 93 | */ 94 | public SslHandler create() { 95 | SSLEngine engine = serverContext.createSSLEngine(); 96 | engine.setNeedClientAuth(needClientAuth); 97 | engine.setUseClientMode(false); 98 | return new SslHandler(engine); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/processors/HttpRequestInformationProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.processors; 20 | 21 | import io.netty.handler.codec.DecoderResult; 22 | import io.netty.handler.codec.http.HttpContent; 23 | import io.netty.handler.codec.http.HttpHeaders; 24 | import io.netty.handler.codec.http.HttpRequest; 25 | import io.netty.handler.codec.http.QueryStringDecoder; 26 | import io.netty.util.CharsetUtil; 27 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpRequestContext; 28 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerProcessorContext; 29 | 30 | import java.util.List; 31 | import java.util.Map; 32 | 33 | /** 34 | * Class for Http Request Information Processor. 35 | */ 36 | public class HttpRequestInformationProcessor extends AbstractServerProcessor { 37 | 38 | @Override 39 | public void process(HttpServerProcessorContext processorContext) { 40 | HttpRequest request = processorContext.getHttpRequest(); 41 | HttpRequestContext requestContext = processorContext.getHttpRequestContext(); 42 | populateRequestHeaders(request, requestContext); 43 | populateQueryParameters(request, requestContext); 44 | populateRequestContext(request, requestContext); 45 | populateHttpMethod(request, requestContext); 46 | populateHttpVersion(request, requestContext); 47 | populateConnectionKeepAlive(request, requestContext); 48 | 49 | if (processorContext.getHttpContent() != null) { 50 | appendDecoderResult(processorContext.getHttpContent(), requestContext); 51 | } 52 | } 53 | 54 | private void populateRequestHeaders(HttpRequest request, HttpRequestContext requestContext) { 55 | HttpHeaders headers = request.headers(); 56 | if (!headers.isEmpty()) { 57 | for (Map.Entry entry : headers.entries()) { 58 | requestContext.addHeaderParameter(entry.getKey(), entry.getValue()); 59 | } 60 | } 61 | } 62 | 63 | private void appendDecoderResult(HttpContent httpContent, HttpRequestContext requestContext) { 64 | try { 65 | requestContext.appendResponseContent(httpContent.content().toString(CharsetUtil.UTF_8)); 66 | DecoderResult result = httpContent.getDecoderResult(); 67 | if (result.isSuccess()) { 68 | return; 69 | } 70 | requestContext.appendResponseContent(result.cause()); 71 | } finally { 72 | httpContent.release(); 73 | } 74 | } 75 | 76 | private void populateQueryParameters(HttpRequest request, HttpRequestContext requestContext) { 77 | QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri()); 78 | Map> params = queryStringDecoder.parameters(); 79 | requestContext.setQueryParameters(params); 80 | } 81 | 82 | private void populateRequestContext(HttpRequest request, HttpRequestContext requestContext) { 83 | requestContext.setUri(request.getUri()); 84 | } 85 | 86 | private void populateHttpMethod(HttpRequest request, HttpRequestContext requestContext) { 87 | requestContext.setHttpMethod(request.getMethod()); 88 | } 89 | 90 | private void populateHttpVersion(HttpRequest request, HttpRequestContext requestContext) { 91 | requestContext.setHttpVersion(request.getProtocolVersion()); 92 | } 93 | 94 | private void populateConnectionKeepAlive(HttpRequest request, HttpRequestContext requestContext) { 95 | requestContext.setKeepAlive(HttpHeaders.isKeepAlive(request)); 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /emulator/pom.xml: -------------------------------------------------------------------------------- 1 | 18 | 19 | org.wso2.carbon.protocol.emulator 20 | org.wso2.carbon.protocol.emulator.parent 21 | 1.0.1-SNAPSHOT 22 | ../pom.xml 23 | 24 | 4.0.0 25 | emulator 26 | bundle 27 | 1.0.1-SNAPSHOT 28 | WSO2 Carbon - Emulator 29 | http://wso2.org 30 | 31 | 32 | 33 | org.apache.maven.plugins 34 | maven-compiler-plugin 35 | 36 | 1.8 37 | 1.8 38 | 39 | 40 | 41 | org.apache.maven.plugins 42 | maven-surefire-plugin 43 | 44 | 45 | 46 | ${basedir}/src/test/resources/ 47 | 48 | 49 | 50 | src/test/resources/testng.xml 51 | 52 | ${argLine} 53 | 54 | 55 | 56 | org.jacoco 57 | jacoco-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | org.testng 64 | testng 65 | 66 | 67 | io.netty 68 | netty-common 69 | 70 | 71 | io.netty 72 | netty-buffer 73 | 74 | 75 | io.netty 76 | netty-transport 77 | 78 | 79 | io.netty 80 | netty-handler 81 | 82 | 83 | io.netty 84 | netty-codec 85 | 86 | 87 | io.netty 88 | netty-codec-http 89 | 90 | 91 | org.eclipse.jetty.alpn 92 | alpn-api 93 | 94 | 95 | org.slf4j 96 | slf4j-api 97 | 98 | 99 | org.slf4j 100 | slf4j-log4j12 101 | 102 | 103 | 104 | 105 | org.wso2.carbon.protocol.emulator.*;version=${emulator.version} 106 | 107 | * 108 | 109 | 110 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpClientResponseBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import io.netty.handler.codec.http.HttpResponseStatus; 22 | import org.apache.log4j.Logger; 23 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractResponseBuilderContext; 24 | import org.wso2.carbon.protocol.emulator.http.params.Cookie; 25 | import org.wso2.carbon.protocol.emulator.http.params.Header; 26 | import org.wso2.carbon.protocol.emulator.http.params.HeaderOperation; 27 | import org.wso2.carbon.protocol.emulator.util.FileReaderUtil; 28 | 29 | import java.io.File; 30 | import java.io.IOException; 31 | import java.util.ArrayList; 32 | import java.util.Arrays; 33 | import java.util.List; 34 | 35 | /** 36 | * Class for expected HTTP client response details. 37 | */ 38 | public class HttpClientResponseBuilderContext extends AbstractResponseBuilderContext { 39 | private static final Logger log = Logger.getLogger(HttpClientResponseBuilderContext.class); 40 | private static HttpClientResponseBuilderContext clientResponseBuilderContext; 41 | private HttpResponseStatus statusCode; 42 | private List
headers; 43 | private List cookies; 44 | private String body; 45 | private boolean isIgnored; 46 | private HeaderOperation operations; 47 | 48 | private static HttpClientResponseBuilderContext getInstance() { 49 | clientResponseBuilderContext = new HttpClientResponseBuilderContext(); 50 | return clientResponseBuilderContext; 51 | } 52 | 53 | public static HttpClientResponseBuilderContext response() { 54 | return getInstance(); 55 | } 56 | 57 | public HttpClientResponseBuilderContext withStatusCode(HttpResponseStatus statusCode) { 58 | this.statusCode = statusCode; 59 | return this; 60 | } 61 | 62 | public HttpClientResponseBuilderContext withHeader(String name, String value) { 63 | if (headers == null) { 64 | this.headers = new ArrayList
(); 65 | } 66 | headers.add(new Header(name, value)); 67 | return this; 68 | } 69 | 70 | public HttpClientResponseBuilderContext withHeaders(HeaderOperation operation, Header... headers) { 71 | this.operations = operation; 72 | if (this.headers == null) { 73 | this.headers = new ArrayList
(); 74 | } 75 | 76 | if (headers != null && headers.length > 0) { 77 | this.headers.addAll(Arrays.asList(headers)); 78 | } 79 | return this; 80 | } 81 | 82 | public HttpClientResponseBuilderContext withCookie(String name, String value) { 83 | if (cookies == null) { 84 | this.cookies = new ArrayList(); 85 | } 86 | cookies.add(new Cookie(name, value)); 87 | return this; 88 | } 89 | 90 | public HttpClientResponseBuilderContext withCookies(Cookie... cookies) { 91 | if (this.cookies == null) { 92 | this.cookies = new ArrayList(); 93 | } 94 | 95 | if (cookies != null && cookies.length > 0) { 96 | this.cookies.addAll(Arrays.asList(cookies)); 97 | } 98 | return this; 99 | } 100 | 101 | public HttpClientResponseBuilderContext withBody(String body) { 102 | this.body = body; 103 | return this; 104 | } 105 | 106 | public HttpClientResponseBuilderContext withBody(File filePath) { 107 | try { 108 | this.body = FileReaderUtil.getFileBody(filePath); 109 | } catch (IOException e) { 110 | log.error(e); 111 | } 112 | return this; 113 | } 114 | 115 | public boolean getAssertionStatus() { 116 | return isIgnored; 117 | } 118 | 119 | public HttpClientResponseBuilderContext assertionIgnore() { 120 | this.isIgnored = true; 121 | return this; 122 | } 123 | 124 | public HttpResponseStatus getStatusCode() { 125 | return statusCode; 126 | } 127 | 128 | public List
getHeaders() { 129 | return headers; 130 | } 131 | 132 | public List getCookies() { 133 | return cookies; 134 | } 135 | 136 | public String getBody() { 137 | return body; 138 | } 139 | 140 | public HeaderOperation getOperations() { 141 | return operations; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/contexts/HttpServerResponseBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server.contexts; 20 | 21 | import io.netty.handler.codec.http.HttpResponseStatus; 22 | import org.apache.log4j.Logger; 23 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractResponseBuilderContext; 24 | import org.wso2.carbon.protocol.emulator.http.params.Cookie; 25 | import org.wso2.carbon.protocol.emulator.http.params.Header; 26 | import org.wso2.carbon.protocol.emulator.util.FileReaderUtil; 27 | 28 | import java.io.File; 29 | import java.io.IOException; 30 | import java.util.ArrayList; 31 | import java.util.Arrays; 32 | import java.util.List; 33 | 34 | /** 35 | * Class for Http Server Response Builder Context. 36 | */ 37 | public class HttpServerResponseBuilderContext extends AbstractResponseBuilderContext { 38 | private static final Logger log = Logger.getLogger(HttpServerResponseBuilderContext.class); 39 | private static HttpServerResponseBuilderContext serverResponse; 40 | private HttpResponseStatus statusCode = HttpResponseStatus.OK; 41 | private List cookies; 42 | private List
headers; 43 | private List copyHeaders; 44 | private String body; 45 | 46 | private static HttpServerResponseBuilderContext getInstance() { 47 | serverResponse = new HttpServerResponseBuilderContext(); 48 | return serverResponse; 49 | } 50 | 51 | public static HttpServerResponseBuilderContext response() { 52 | return getInstance(); 53 | } 54 | 55 | public HttpServerResponseBuilderContext withStatusCode(HttpResponseStatus statusCode) { 56 | this.statusCode = statusCode; 57 | return this; 58 | } 59 | 60 | public HttpServerResponseBuilderContext withCookie(String name, String value) { 61 | if (cookies == null) { 62 | this.cookies = new ArrayList(); 63 | } 64 | cookies.add(new Cookie(name, value)); 65 | return this; 66 | } 67 | 68 | public HttpServerResponseBuilderContext withCookies(Cookie... cookies) { 69 | if (this.cookies == null) { 70 | this.cookies = new ArrayList(); 71 | } 72 | if (cookies != null && cookies.length > 0) { 73 | this.cookies.addAll(Arrays.asList(cookies)); 74 | } 75 | return this; 76 | } 77 | 78 | public HttpServerResponseBuilderContext withHeader(String name, String value) { 79 | if (headers == null) { 80 | this.headers = new ArrayList
(); 81 | } 82 | headers.add(new Header(name, value)); 83 | return this; 84 | } 85 | 86 | public HttpServerResponseBuilderContext withHeaders(Header... headers) { 87 | if (this.headers == null) { 88 | this.headers = new ArrayList
(); 89 | } 90 | 91 | if (headers != null && headers.length > 0) { 92 | this.headers.addAll(Arrays.asList(headers)); 93 | } 94 | return this; 95 | } 96 | 97 | public HttpServerResponseBuilderContext withCustomProcessor(String customRequestProcessor) { 98 | return this; 99 | } 100 | 101 | public HttpServerResponseBuilderContext withBody(String body) { 102 | this.body = body; 103 | return this; 104 | } 105 | 106 | public HttpServerResponseBuilderContext withBody(File filePath) { 107 | try { 108 | this.body = FileReaderUtil.getFileBody(filePath); 109 | } catch (IOException e) { 110 | log.error("Exception occurred while reading file", e); 111 | } 112 | return this; 113 | } 114 | 115 | public HttpServerResponseBuilderContext withCopyHeader(String name) { 116 | if (copyHeaders == null) { 117 | this.copyHeaders = new ArrayList(); 118 | } 119 | copyHeaders.add(name); 120 | return this; 121 | } 122 | 123 | public HttpServerResponseBuilderContext withEmptyBody() { 124 | return this; 125 | } 126 | 127 | public HttpResponseStatus getStatusCode() { 128 | return statusCode; 129 | } 130 | 131 | public List getCookies() { 132 | return cookies; 133 | } 134 | 135 | public List
getHeaders() { 136 | return headers; 137 | } 138 | 139 | public List getCopyHeaders() { 140 | return copyHeaders; 141 | } 142 | 143 | public String getBody() { 144 | return body; 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /emulator/src/test/java/org/wso2/carbon/protocol/emulator/http/custom/processor/validation/CustomProcessorValidationTestCase2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.custom.processor.validation; 20 | 21 | import io.netty.handler.codec.http.HttpMethod; 22 | import io.netty.handler.codec.http.HttpResponseStatus; 23 | import org.testng.Assert; 24 | import org.testng.annotations.AfterClass; 25 | import org.testng.annotations.BeforeClass; 26 | import org.testng.annotations.Test; 27 | import org.wso2.carbon.protocol.emulator.dsl.Emulator; 28 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientConfigBuilderContext; 29 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientRequestBuilderContext; 30 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseBuilderContext; 31 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseProcessorContext; 32 | import org.wso2.carbon.protocol.emulator.http.params.Header; 33 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerOperationBuilderContext; 34 | 35 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerConfigBuilderContext.configure; 36 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerRequestBuilderContext.request; 37 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerResponseBuilderContext.response; 38 | 39 | /** 40 | * CustomProcessorValidationTestCase2 41 | */ 42 | public class CustomProcessorValidationTestCase2 { 43 | 44 | private HttpServerOperationBuilderContext emulator; 45 | 46 | @BeforeClass 47 | public void setEnvironment() throws Exception { 48 | this.emulator = startHttpEmulator(); 49 | Thread.sleep(1000); 50 | } 51 | 52 | @Test 53 | public void testServerRequestCustomProcessor() { 54 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 55 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 56 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user1").withMethod(HttpMethod.GET)) 57 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 58 | 59 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 60 | "Expected response status code not found"); 61 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User1", 62 | "Expected response content not found"); 63 | Assert.assertEquals(response.getReceivedResponseContext().getHeaderParameters().get("Header2").get(0), 64 | "value2", "Expected response header not found"); 65 | } 66 | 67 | @Test 68 | public void testServerRequestResponseCustomProcessorsErrorScenario() { 69 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 70 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 71 | 72 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user5").withMethod(HttpMethod.GET)) 73 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 74 | 75 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 76 | "Expected response status code not found"); 77 | } 78 | 79 | @AfterClass 80 | public void cleanup() { 81 | this.emulator.stop(); 82 | } 83 | 84 | private HttpServerOperationBuilderContext startHttpEmulator() { 85 | return Emulator.getHttpEmulator().server().given(configure().host("127.0.0.1").port(6065).context("/users") 86 | .withCustomResponseProcessor(new CustomResponseProcessor()) 87 | .withEnableWireLog()) 88 | 89 | .when(request().withMethod(HttpMethod.GET).withPath("/user1")) 90 | .then(response().withBody("User1").withStatusCode(HttpResponseStatus.OK) 91 | .withHeader("Header1", "value1")) 92 | 93 | .when(request().withMethod(HttpMethod.GET).withPath("/user2")) 94 | .then(response().withBody("ChangedBody").withStatusCode(HttpResponseStatus.OK) 95 | .withHeader("Header2", "ChangedHeaderValue2")) 96 | 97 | .when(request().withMethod(HttpMethod.GET).withPath("user4")) 98 | .then(response().withBody("User4").withStatusCode(HttpResponseStatus.OK) 99 | .withHeaders(new Header("Header4", "value4"))).operation().start(); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/handler/HttpClientHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.handler; 20 | 21 | import io.netty.buffer.ByteBuf; 22 | import io.netty.channel.ChannelHandlerContext; 23 | import io.netty.channel.ChannelInboundHandlerAdapter; 24 | import io.netty.handler.codec.http.HttpContent; 25 | import io.netty.handler.codec.http.HttpResponse; 26 | import io.netty.handler.codec.http.LastHttpContent; 27 | import org.apache.log4j.Logger; 28 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientConfigBuilderContext; 29 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientInformationContext; 30 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseProcessorContext; 31 | import org.wso2.carbon.protocol.emulator.http.client.contexts.RequestResponseCorrelation; 32 | import org.wso2.carbon.protocol.emulator.http.client.processors.HttpResponseAssertProcessor; 33 | import org.wso2.carbon.protocol.emulator.http.client.processors.HttpResponseInformationProcessor; 34 | import org.wso2.carbon.protocol.emulator.util.WireLogHandler; 35 | 36 | import java.util.concurrent.Executors; 37 | import java.util.concurrent.ScheduledExecutorService; 38 | 39 | /** 40 | * Class for Http client handler. 41 | */ 42 | public class HttpClientHandler extends ChannelInboundHandlerAdapter { 43 | private static final Logger log = Logger.getLogger(HttpClientHandler.class); 44 | private final RequestResponseCorrelation requestResponseCorrelation; 45 | private HttpResponseInformationProcessor responseInformationProcessor; 46 | private HttpClientResponseProcessorContext processorContext; 47 | private HttpClientInformationContext clientInformationContext; 48 | private ScheduledExecutorService scheduledReadingExecutorService; 49 | private int corePoolSize = 10; 50 | 51 | public HttpClientHandler(HttpClientInformationContext clientInformationContext, 52 | RequestResponseCorrelation requestResponseCorrelation) { 53 | this.clientInformationContext = clientInformationContext; 54 | this.requestResponseCorrelation = requestResponseCorrelation; 55 | scheduledReadingExecutorService = Executors.newScheduledThreadPool(corePoolSize); 56 | } 57 | 58 | @Override 59 | public void channelRead(ChannelHandlerContext ctx, Object msg) { 60 | HttpClientConfigBuilderContext configBuilderContext 61 | = clientInformationContext.getClientConfigBuilderContext(); 62 | 63 | WireLogHandler.responseWireLog(msg); 64 | 65 | Boolean connectionDrop = configBuilderContext.getReadingConnectionDrop(); 66 | if (connectionDrop != null && connectionDrop && ctx.channel().isOpen()) { 67 | ctx.close(); 68 | log.info("Client Connection dropped while Reading data from the channel"); 69 | } 70 | 71 | if (msg instanceof HttpResponse) { 72 | this.processorContext = new HttpClientResponseProcessorContext(); 73 | this.processorContext.setClientInformationContext(clientInformationContext); 74 | this.responseInformationProcessor = new HttpResponseInformationProcessor(); 75 | HttpResponse response = (HttpResponse) msg; 76 | processorContext.setReceivedResponse(response); 77 | responseInformationProcessor.process(processorContext); 78 | } 79 | 80 | if (msg instanceof HttpContent) { 81 | HttpContent httpContent = (HttpContent) msg; 82 | try { 83 | ByteBuf content = httpContent.content(); 84 | 85 | if (content.isReadable()) { 86 | this.responseInformationProcessor 87 | .appendDecoderResult(processorContext.getReceivedResponseContext(), httpContent, content); 88 | } 89 | } finally { 90 | httpContent.release(); 91 | } 92 | } 93 | if (msg instanceof LastHttpContent) { 94 | requestResponseCorrelation.setReceivedResponse(processorContext); 95 | HttpResponseAssertProcessor.process(processorContext, requestResponseCorrelation.getExpectedResponse()); 96 | this.clientInformationContext.setReceivedResponseProcessContext(processorContext); 97 | 98 | String responseBody = processorContext.getReceivedResponseContext().getResponseBody(); 99 | if (responseBody != null) { 100 | WireLogHandler.logResponseBody(responseBody); 101 | } 102 | ctx.close(); 103 | } 104 | } 105 | 106 | @Override 107 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 108 | log.error(cause); 109 | ctx.close(); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/processors/HttpResponseAssertProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.processors; 20 | 21 | import org.apache.log4j.Logger; 22 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseBuilderContext; 23 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseProcessorContext; 24 | import org.wso2.carbon.protocol.emulator.http.params.Header; 25 | import org.wso2.carbon.protocol.emulator.http.params.HeaderOperation; 26 | 27 | import java.util.List; 28 | import java.util.Map; 29 | 30 | /** 31 | * Class to assert the received HTTP response with expected HTTP response. 32 | */ 33 | public class HttpResponseAssertProcessor { 34 | 35 | private static final Logger log = Logger.getLogger(HttpResponseAssertProcessor.class); 36 | 37 | public static void process(HttpClientResponseProcessorContext processorContext, 38 | HttpClientResponseBuilderContext expectedResponseContext) { 39 | 40 | if (!expectedResponseContext.getAssertionStatus()) { 41 | assertResponseContent(processorContext, expectedResponseContext); 42 | assertHeaderParameters(processorContext, expectedResponseContext); 43 | } 44 | } 45 | 46 | private static void assertResponseContent(HttpClientResponseProcessorContext processorContext, 47 | HttpClientResponseBuilderContext expectedResponseContext) { 48 | 49 | if (expectedResponseContext.getBody() 50 | .equalsIgnoreCase(processorContext.getReceivedResponseContext().getResponseBody())) { 51 | log.info("Equal content"); 52 | } else { 53 | log.info("Wrong content"); 54 | } 55 | } 56 | 57 | private static void assertHeaderParameters(HttpClientResponseProcessorContext processorContext, 58 | HttpClientResponseBuilderContext expectedResponseContext) { 59 | if (expectedResponseContext.getHeaders() == null || expectedResponseContext.getHeaders().isEmpty()) { 60 | return; 61 | } 62 | Map> receivedHeaders = processorContext.getReceivedResponseContext().getHeaderParameters(); 63 | HeaderOperation operation = expectedResponseContext.getOperations(); 64 | 65 | boolean value = false; 66 | if (operation == HeaderOperation.AND) { 67 | 68 | for (Map.Entry> entry : processorContext.getReceivedResponseContext() 69 | .getHeaderParameters().entrySet()) { 70 | entry.getKey(); 71 | } 72 | 73 | for (Header header : expectedResponseContext.getHeaders()) { 74 | List receivedHeaderValues = receivedHeaders.get(header.getName()); 75 | 76 | if (receivedHeaderValues == null || receivedHeaderValues.isEmpty() || !receivedHeaderValues 77 | .contains(header.getValue())) { 78 | log.info("Header not present"); 79 | break; 80 | } else { 81 | log.info("Headers are present"); 82 | } 83 | } 84 | } else if (operation == operation.OR) { 85 | for (Header header : expectedResponseContext.getHeaders()) { 86 | List receivedHeaderValues = receivedHeaders.get(header.getName()); 87 | 88 | if (receivedHeaderValues == null || receivedHeaderValues.isEmpty() || !receivedHeaderValues 89 | .contains(header.getValue())) { 90 | ; 91 | } else { 92 | value = true; 93 | } 94 | } 95 | if (value) { 96 | log.info("Headers are present"); 97 | } else { 98 | log.info("Non of the Headers present"); 99 | } 100 | } else { 101 | boolean match = false; 102 | for (Map.Entry> header : receivedHeaders.entrySet()) { 103 | 104 | List value1 = header.getValue(); 105 | for (String val : value1) { 106 | Header header1 = new Header(header.getKey(), val); 107 | if (expectedResponseContext.getHeaders().get(0).getValue() 108 | .equals(header1.getValue()) && expectedResponseContext.getHeaders() 109 | .get(0).getName().equals(header1.getName())) { 110 | match = true; 111 | } 112 | } 113 | } 114 | if (match) { 115 | log.info("Header Present"); 116 | } else { 117 | log.info("Header is not present"); 118 | } 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/client/contexts/HttpClientConfigBuilderContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.client.contexts; 20 | 21 | import org.wso2.carbon.protocol.emulator.dsl.Protocol; 22 | import org.wso2.carbon.protocol.emulator.dsl.contexts.AbstractConfigurationBuilderContext; 23 | 24 | import java.io.File; 25 | 26 | /** 27 | * Class for HTTP Client's Configuration. 28 | */ 29 | public class HttpClientConfigBuilderContext extends AbstractConfigurationBuilderContext { 30 | private static HttpClientConfigBuilderContext clientConfigBuilderContext; 31 | private String host; 32 | private int port; 33 | private int readingDelay; 34 | private Protocol protocol; 35 | private File keyStore; 36 | private String keyStorePass; 37 | private String certPass; 38 | private File trustStore; 39 | private String trustStorePass; 40 | private boolean readingConnectionDrop; 41 | private boolean partialWriteConnectionDrop; 42 | private int writingDelay = 0; 43 | private boolean keepAlive = false; 44 | 45 | private static HttpClientConfigBuilderContext getInstance() { 46 | clientConfigBuilderContext = new HttpClientConfigBuilderContext(); 47 | return clientConfigBuilderContext; 48 | } 49 | 50 | public static HttpClientConfigBuilderContext configure() { 51 | return getInstance(); 52 | } 53 | 54 | public HttpClientConfigBuilderContext keyStore(File keyStore) { 55 | this.keyStore = keyStore; 56 | return this; 57 | } 58 | 59 | public HttpClientConfigBuilderContext keyStorePass(String keyStorePass) { 60 | this.keyStorePass = keyStorePass; 61 | return this; 62 | } 63 | 64 | public HttpClientConfigBuilderContext certPass(String certPass) { 65 | this.certPass = certPass; 66 | return this; 67 | } 68 | 69 | public HttpClientConfigBuilderContext trustStore(File trustStore) { 70 | this.trustStore = trustStore; 71 | return this; 72 | 73 | } 74 | 75 | public HttpClientConfigBuilderContext trustStorePass(String trustStorePass) { 76 | this.trustStorePass = trustStorePass; 77 | return this; 78 | } 79 | 80 | public HttpClientConfigBuilderContext withEnableReadingConnectionDrop() { 81 | this.readingConnectionDrop = true; 82 | return this; 83 | } 84 | 85 | public HttpClientConfigBuilderContext withPartialWriteConnectionDrop() { 86 | this.partialWriteConnectionDrop = true; 87 | return this; 88 | } 89 | 90 | public HttpClientConfigBuilderContext withKeepAlive(boolean enable) { 91 | this.keepAlive = enable; 92 | return this; 93 | } 94 | 95 | public String getCertPass() { 96 | return certPass; 97 | } 98 | 99 | public File getKeyStore() { 100 | return keyStore; 101 | } 102 | 103 | public String getKeyStorePass() { 104 | return keyStorePass; 105 | } 106 | 107 | public File getTrustStore() { 108 | return trustStore; 109 | } 110 | 111 | public String getTrustStorePass() { 112 | return trustStorePass; 113 | } 114 | 115 | @Override 116 | public HttpClientConfigBuilderContext host(String host) { 117 | this.host = host; 118 | return this; 119 | } 120 | 121 | @Override 122 | public HttpClientConfigBuilderContext port(int port) { 123 | this.port = port; 124 | return this; 125 | } 126 | 127 | public Protocol getProtocol() { 128 | return protocol; 129 | } 130 | 131 | public HttpClientConfigBuilderContext withReadingDelay(int readingDelay) { 132 | this.readingDelay = readingDelay; 133 | return this; 134 | } 135 | 136 | public HttpClientConfigBuilderContext withWritingDelay(int writingDelay) { 137 | this.writingDelay = writingDelay; 138 | return this; 139 | } 140 | 141 | public HttpClientConfigBuilderContext withProtocol(Protocol protocol) { 142 | this.protocol = protocol; 143 | return this; 144 | } 145 | 146 | public String getHost() { 147 | return host; 148 | } 149 | 150 | public void setHost(String host) { 151 | this.host = host; 152 | } 153 | 154 | public int getPort() { 155 | return port; 156 | } 157 | 158 | public void setPort(int port) { 159 | this.port = port; 160 | } 161 | 162 | public int getReadingDelay() { 163 | return readingDelay; 164 | } 165 | 166 | public int getWritingDelay() { 167 | return writingDelay; 168 | } 169 | 170 | public Boolean getReadingConnectionDrop() { 171 | return readingConnectionDrop; 172 | } 173 | 174 | public Boolean getPartialWriteConnectionDrop() { 175 | return partialWriteConnectionDrop; 176 | } 177 | 178 | public boolean isKeepAlive() { 179 | return keepAlive; 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /emulator/src/test/java/org/wso2/carbon/protocol/emulator/http/custom/processor/validation/CustomProcessorValidationTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.custom.processor.validation; 20 | 21 | import io.netty.handler.codec.http.HttpMethod; 22 | import io.netty.handler.codec.http.HttpResponseStatus; 23 | import org.testng.Assert; 24 | import org.testng.annotations.AfterClass; 25 | import org.testng.annotations.BeforeClass; 26 | import org.testng.annotations.Test; 27 | import org.wso2.carbon.protocol.emulator.dsl.Emulator; 28 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientConfigBuilderContext; 29 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientRequestBuilderContext; 30 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseBuilderContext; 31 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseProcessorContext; 32 | import org.wso2.carbon.protocol.emulator.http.params.Header; 33 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerConfigBuilderContext; 34 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerOperationBuilderContext; 35 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerRequestBuilderContext; 36 | 37 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerResponseBuilderContext.response; 38 | 39 | /** 40 | * CustomProcessorValidationTestCase 41 | */ 42 | public class CustomProcessorValidationTestCase { 43 | 44 | private HttpServerOperationBuilderContext emulator; 45 | 46 | @BeforeClass 47 | public void setEnvironment() throws Exception { 48 | this.emulator = startHttpEmulator(); 49 | Thread.sleep(1000); 50 | } 51 | 52 | @Test 53 | public void testServerRequestCustomProcessor() { 54 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 55 | .given(HttpClientConfigBuilderContext.configure().host("10.100.4.37").port(8280)) 56 | .when(HttpClientRequestBuilderContext.request().withPath("/Users") 57 | .withHeader("Content-Type1", "application/xml").withBody("sddfff") 58 | .withMethod(HttpMethod.PUT)).then(HttpClientResponseBuilderContext.response().assertionIgnore()) 59 | .operation().send(); 60 | 61 | /*Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 62 | "Expected response status code not found"); 63 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "ChangedBody", 64 | "Expected response content not found"); 65 | Assert.assertEquals(response.getReceivedResponseContext().getHeaderParameters().get("Header2").get(0), 66 | "ChangedHeaderValue2", "Expected response header not found");*/ 67 | } 68 | 69 | @Test 70 | public void testServerRequestResponseCustomProcessorsErrorScenario() { 71 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 72 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 73 | 74 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user5").withMethod(HttpMethod.GET)) 75 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 76 | 77 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 78 | "Expected response status code not found"); 79 | } 80 | 81 | @AfterClass 82 | public void cleanup() { 83 | this.emulator.stop(); 84 | } 85 | 86 | private HttpServerOperationBuilderContext startHttpEmulator() { 87 | return Emulator.getHttpEmulator().server().given(HttpServerConfigBuilderContext.configure().host("127.0.0.1") 88 | .port(6065).context("/users") 89 | .withCustomRequestProcessor(new CustomRequestProcessor()).withEnableWireLog()) 90 | 91 | .when(HttpServerRequestBuilderContext.request().withMethod(HttpMethod.GET).withPath("/user1")) 92 | .then(response().withBody("User1").withStatusCode(HttpResponseStatus.OK) 93 | .withHeader("Header1", "value1")) 94 | 95 | .when(HttpServerRequestBuilderContext.request().withMethod(HttpMethod.GET).withPath("/user2")) 96 | .then(response().withBody("ChangedBody").withStatusCode(HttpResponseStatus.OK) 97 | .withHeader("Header2", "ChangedHeaderValue2")) 98 | 99 | .when(HttpServerRequestBuilderContext.request().withMethod(HttpMethod.GET).withPath("user4")) 100 | .then(response().withBody("User4").withStatusCode(HttpResponseStatus.OK) 101 | .withHeaders(new Header("Header4", "value4"))).operation().start(); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /emulator/src/test/java/org/wso2/carbon/protocol/emulator/http/LogicDelayTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http; 20 | 21 | import io.netty.handler.codec.http.HttpMethod; 22 | import io.netty.handler.codec.http.HttpResponseStatus; 23 | import org.testng.Assert; 24 | import org.testng.annotations.AfterClass; 25 | import org.testng.annotations.BeforeClass; 26 | import org.testng.annotations.Test; 27 | import org.wso2.carbon.protocol.emulator.dsl.Emulator; 28 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientConfigBuilderContext; 29 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientRequestBuilderContext; 30 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseBuilderContext; 31 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseProcessorContext; 32 | import org.wso2.carbon.protocol.emulator.http.params.Header; 33 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerOperationBuilderContext; 34 | 35 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerConfigBuilderContext.configure; 36 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerRequestBuilderContext.request; 37 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerResponseBuilderContext.response; 38 | 39 | /** 40 | * LogicDelayTestCase 41 | */ 42 | public class LogicDelayTestCase { 43 | 44 | private HttpServerOperationBuilderContext emulator; 45 | 46 | @BeforeClass 47 | public void setEnvironment() throws InterruptedException { 48 | this.emulator = startHttpEmulator(); 49 | Thread.sleep(1000); 50 | } 51 | 52 | @Test 53 | public void testServerWithReadingDelayGET() { 54 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 55 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 56 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user1").withMethod(HttpMethod.GET)) 57 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 58 | 59 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 60 | "Expected response status code not found"); 61 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User1", 62 | "Expected response content not found"); 63 | } 64 | 65 | @Test 66 | public void testServerWithReadingDelayPUT() { 67 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 68 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 69 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user2").withMethod(HttpMethod.PUT)) 70 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 71 | 72 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 73 | "Expected response status code not found"); 74 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User2", 75 | "Expected response content not found"); 76 | } 77 | 78 | @Test 79 | public void testServerWithReadingDelayPOST() { 80 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 81 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 82 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user3").withMethod(HttpMethod.POST)) 83 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 84 | 85 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 86 | "Expected response status code not found"); 87 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User3", 88 | "Expected response content not found"); 89 | } 90 | 91 | @AfterClass 92 | public void cleanup() { 93 | this.emulator.stop(); 94 | } 95 | 96 | private HttpServerOperationBuilderContext startHttpEmulator() { 97 | return Emulator.getHttpEmulator().server() 98 | .given(configure().host("127.0.0.1").port(6065).context("/users").withLogicDelay(1000)) 99 | 100 | .when(request().withMethod(HttpMethod.GET).withPath("/user1")) 101 | .then(response().withBody("User1").withStatusCode(HttpResponseStatus.OK) 102 | .withHeader("Header1", "value1")) 103 | 104 | .when(request().withMethod(HttpMethod.PUT).withPath("/user2")) 105 | .then(response().withBody("User2").withStatusCode(HttpResponseStatus.OK) 106 | .withHeader("Header2", "value2")) 107 | 108 | .when(request().withMethod(HttpMethod.POST).withPath("user3")) 109 | .then(response().withBody("User3").withStatusCode(HttpResponseStatus.OK) 110 | .withHeaders(new Header("Header3", "value3"))).operation().start(); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /emulator/src/test/java/org/wso2/carbon/protocol/emulator/http/FastBackEndTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http; 20 | 21 | import io.netty.handler.codec.http.HttpMethod; 22 | import io.netty.handler.codec.http.HttpResponseStatus; 23 | import org.testng.Assert; 24 | import org.testng.annotations.AfterClass; 25 | import org.testng.annotations.BeforeClass; 26 | import org.testng.annotations.Test; 27 | import org.wso2.carbon.protocol.emulator.dsl.Emulator; 28 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientConfigBuilderContext; 29 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientRequestBuilderContext; 30 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseBuilderContext; 31 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseProcessorContext; 32 | import org.wso2.carbon.protocol.emulator.http.params.Header; 33 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerOperationBuilderContext; 34 | 35 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerConfigBuilderContext.configure; 36 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerRequestBuilderContext.request; 37 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerResponseBuilderContext.response; 38 | 39 | /** 40 | * FastBackEndTestCase 41 | */ 42 | public class FastBackEndTestCase { 43 | 44 | private HttpServerOperationBuilderContext emulator; 45 | 46 | @BeforeClass 47 | public void setEnvironment() throws InterruptedException { 48 | this.emulator = startHttpEmulator(); 49 | Thread.sleep(1000); 50 | } 51 | 52 | @Test 53 | public void testServerWithReadingDelayGET() { 54 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 55 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 56 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user1").withMethod(HttpMethod.GET)) 57 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 58 | 59 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 60 | "Expected response status code not found"); 61 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User1", 62 | "Expected response content not found"); 63 | } 64 | 65 | @Test 66 | public void testServerWithReadingDelayPUT() { 67 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 68 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 69 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user2").withMethod(HttpMethod.PUT)) 70 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 71 | 72 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 73 | "Expected response status code not found"); 74 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User2", 75 | "Expected response content not found"); 76 | } 77 | 78 | @Test 79 | public void testServerWithReadingDelayPOST() { 80 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 81 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 82 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user3").withMethod(HttpMethod.POST)) 83 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 84 | 85 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 86 | "Expected response status code not found"); 87 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User3", 88 | "Expected response content not found"); 89 | } 90 | 91 | @AfterClass 92 | public void cleanup() { 93 | this.emulator.stop(); 94 | } 95 | 96 | private HttpServerOperationBuilderContext startHttpEmulator() { 97 | return Emulator.getHttpEmulator().server() 98 | .given(configure().host("127.0.0.1").port(6065).context("/users").withFastBackend(3, 2000)) 99 | 100 | .when(request().withMethod(HttpMethod.GET).withPath("/user1")) 101 | .then(response().withBody("User1").withStatusCode(HttpResponseStatus.OK) 102 | .withHeader("Header1", "value1")) 103 | 104 | .when(request().withMethod(HttpMethod.PUT).withPath("/user2")) 105 | .then(response().withBody("User2").withStatusCode(HttpResponseStatus.OK) 106 | .withHeader("Header2", "value2")) 107 | 108 | .when(request().withMethod(HttpMethod.POST).withPath("user3")) 109 | .then(response().withBody("User3").withStatusCode(HttpResponseStatus.OK) 110 | .withHeaders(new Header("Header3", "value3"))).operation().start(); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /emulator/src/test/java/org/wso2/carbon/protocol/emulator/http/ReadingDelayTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http; 20 | 21 | import io.netty.handler.codec.http.HttpMethod; 22 | import io.netty.handler.codec.http.HttpResponseStatus; 23 | import org.testng.Assert; 24 | import org.testng.annotations.AfterClass; 25 | import org.testng.annotations.BeforeClass; 26 | import org.testng.annotations.Test; 27 | import org.wso2.carbon.protocol.emulator.dsl.Emulator; 28 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientConfigBuilderContext; 29 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientRequestBuilderContext; 30 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseBuilderContext; 31 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseProcessorContext; 32 | import org.wso2.carbon.protocol.emulator.http.params.Header; 33 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerConfigBuilderContext; 34 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerOperationBuilderContext; 35 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerRequestBuilderContext; 36 | 37 | import static org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerResponseBuilderContext.response; 38 | 39 | /** 40 | * ReadingDelayTestCase 41 | */ 42 | public class ReadingDelayTestCase { 43 | 44 | private HttpServerOperationBuilderContext emulator; 45 | 46 | @BeforeClass 47 | public void setEnvironment() throws InterruptedException { 48 | this.emulator = startHttpEmulator(); 49 | Thread.sleep(1000); 50 | } 51 | 52 | @Test 53 | public void testServerWithReadingDelayGET() { 54 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 55 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 56 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user1").withMethod(HttpMethod.GET)) 57 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 58 | 59 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 60 | "Expected response status code not found"); 61 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User1", 62 | "Expected response content not found"); 63 | } 64 | 65 | @Test 66 | public void testServerWithReadingDelayPUT() { 67 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 68 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 69 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user2").withMethod(HttpMethod.PUT)) 70 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 71 | 72 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 73 | "Expected response status code not found"); 74 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User2", 75 | "Expected response content not found"); 76 | } 77 | 78 | @Test 79 | public void testServerWithReadingDelayPOST() { 80 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 81 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 82 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user3").withMethod(HttpMethod.POST)) 83 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 84 | 85 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 86 | "Expected response status code not found"); 87 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User3", 88 | "Expected response content not found"); 89 | } 90 | 91 | @AfterClass 92 | public void cleanup() { 93 | this.emulator.stop(); 94 | } 95 | 96 | private HttpServerOperationBuilderContext startHttpEmulator() { 97 | return Emulator.getHttpEmulator().server() 98 | .given(HttpServerConfigBuilderContext.configure().host("127.0.0.1").port(6065).context("/users") 99 | .withReadingDelay(1000)) 100 | 101 | .when(HttpServerRequestBuilderContext.request().withMethod(HttpMethod.GET).withPath("/user1")) 102 | .then(response().withBody("User1").withStatusCode(HttpResponseStatus.OK) 103 | .withHeader("Header1", "value1")) 104 | 105 | .when(HttpServerRequestBuilderContext.request().withMethod(HttpMethod.PUT).withPath("/user2")) 106 | .then(response().withBody("User2").withStatusCode(HttpResponseStatus.OK) 107 | .withHeader("Header2", "value2")) 108 | 109 | .when(HttpServerRequestBuilderContext.request().withMethod(HttpMethod.POST).withPath("user3")) 110 | .then(response().withBody("User3").withStatusCode(HttpResponseStatus.OK) 111 | .withHeaders(new Header("Header3", "value3"))).operation().start(); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /emulator/src/test/java/org/wso2/carbon/protocol/emulator/http/WritingDelayTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http; 20 | 21 | import io.netty.handler.codec.http.HttpMethod; 22 | import io.netty.handler.codec.http.HttpResponseStatus; 23 | import org.testng.Assert; 24 | import org.testng.annotations.AfterClass; 25 | import org.testng.annotations.BeforeClass; 26 | import org.testng.annotations.Test; 27 | import org.wso2.carbon.protocol.emulator.dsl.Emulator; 28 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientConfigBuilderContext; 29 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientRequestBuilderContext; 30 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseBuilderContext; 31 | import org.wso2.carbon.protocol.emulator.http.client.contexts.HttpClientResponseProcessorContext; 32 | import org.wso2.carbon.protocol.emulator.http.params.Header; 33 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerConfigBuilderContext; 34 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerOperationBuilderContext; 35 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerRequestBuilderContext; 36 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerResponseBuilderContext; 37 | 38 | /** 39 | * WritingDelayTestCase 40 | */ 41 | public class WritingDelayTestCase { 42 | 43 | private HttpServerOperationBuilderContext emulator; 44 | 45 | @BeforeClass 46 | public void setEnvironment() throws InterruptedException { 47 | this.emulator = startHttpEmulator(); 48 | Thread.sleep(1000); 49 | } 50 | 51 | @Test 52 | public void testServerWithReadingDelayGET() { 53 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 54 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 55 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user1").withMethod(HttpMethod.GET)) 56 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 57 | 58 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 59 | "Expected response status code not found"); 60 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User1", 61 | "Expected response content not found"); 62 | } 63 | 64 | @Test 65 | public void testServerWithReadingDelayPUT() { 66 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 67 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 68 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user2").withMethod(HttpMethod.PUT)) 69 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 70 | 71 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 72 | "Expected response status code not found"); 73 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User2", 74 | "Expected response content not found"); 75 | } 76 | 77 | @Test 78 | public void testServerWithReadingDelayPOST() { 79 | HttpClientResponseProcessorContext response = Emulator.getHttpEmulator().client() 80 | .given(HttpClientConfigBuilderContext.configure().host("127.0.0.1").port(6065)) 81 | .when(HttpClientRequestBuilderContext.request().withPath("/users/user3").withMethod(HttpMethod.POST)) 82 | .then(HttpClientResponseBuilderContext.response().assertionIgnore()).operation().send(); 83 | 84 | Assert.assertEquals(response.getReceivedResponseContext().getResponseStatus(), HttpResponseStatus.OK, 85 | "Expected response status code not found"); 86 | Assert.assertEquals(response.getReceivedResponseContext().getResponseBody(), "User3", 87 | "Expected response content not found"); 88 | } 89 | 90 | @AfterClass 91 | public void cleanup() { 92 | this.emulator.stop(); 93 | } 94 | 95 | private HttpServerOperationBuilderContext startHttpEmulator() { 96 | return Emulator.getHttpEmulator().server() 97 | .given(HttpServerConfigBuilderContext.configure().host("127.0.0.1") 98 | .port(6065).context("/users").withWritingDelay(1000)) 99 | 100 | .when(HttpServerRequestBuilderContext.request().withMethod(HttpMethod.GET).withPath("/user1")) 101 | .then(HttpServerResponseBuilderContext.response().withBody("User1") 102 | .withStatusCode(HttpResponseStatus.OK).withHeader("Header1", "value1")) 103 | 104 | .when(HttpServerRequestBuilderContext.request().withMethod(HttpMethod.PUT).withPath("/user2")) 105 | .then(HttpServerResponseBuilderContext.response().withBody("User2") 106 | .withStatusCode(HttpResponseStatus.OK).withHeader("Header2", "value2")) 107 | 108 | .when(HttpServerRequestBuilderContext.request().withMethod(HttpMethod.POST).withPath("user3")) 109 | .then(HttpServerResponseBuilderContext.response().withBody("User3") 110 | .withStatusCode(HttpResponseStatus.OK) 111 | .withHeaders(new Header("Header3", "value3"))).operation().start(); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /emulator/src/main/java/org/wso2/carbon/protocol/emulator/http/server/HttpServerInitializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * WSO2 Inc. licenses this file to you under the Apache License, 5 | * Version 2.0 (the "License"); you may not use this file except 6 | * in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, 12 | * software distributed under the License is distributed on an 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | * KIND, either express or implied. See the License for the 15 | * specific language governing permissions and limitations 16 | * under the License. 17 | */ 18 | 19 | package org.wso2.carbon.protocol.emulator.http.server; 20 | 21 | import io.netty.bootstrap.ServerBootstrap; 22 | import io.netty.channel.ChannelFuture; 23 | import io.netty.channel.ChannelOption; 24 | import io.netty.channel.EventLoopGroup; 25 | import io.netty.channel.nio.NioEventLoopGroup; 26 | import io.netty.channel.socket.nio.NioServerSocketChannel; 27 | import io.netty.handler.logging.LogLevel; 28 | import io.netty.handler.logging.LoggingHandler; 29 | import org.apache.log4j.Logger; 30 | import org.wso2.carbon.protocol.emulator.dsl.EmulatorType; 31 | import org.wso2.carbon.protocol.emulator.http.ChannelPipelineInitializer; 32 | import org.wso2.carbon.protocol.emulator.http.server.contexts.HttpServerInformationContext; 33 | import org.wso2.carbon.protocol.emulator.http.server.contexts.MockServerThread; 34 | import org.wso2.carbon.protocol.emulator.util.ValidationUtil; 35 | 36 | import java.io.IOException; 37 | import java.io.InputStream; 38 | import java.util.Properties; 39 | 40 | /** 41 | * Class to initialize the Http Server. 42 | */ 43 | public class HttpServerInitializer extends Thread { 44 | private static final Logger log = Logger.getLogger(HttpServerInitializer.class); 45 | private EventLoopGroup bossGroup; 46 | private EventLoopGroup workerGroup; 47 | private HttpServerInformationContext serverInformationContext; 48 | private int bossCount; 49 | private int workerCount; 50 | private Properties prop; 51 | private InputStream inputStream; 52 | private int queues; 53 | 54 | /** 55 | * Initialize the Http Server instance. 56 | * @param serverInformationContext 57 | */ 58 | public HttpServerInitializer(HttpServerInformationContext serverInformationContext) { 59 | this.serverInformationContext = serverInformationContext; 60 | prop = new Properties(); 61 | String propFileName = "server.properties"; 62 | inputStream = getClass().getClassLoader().getResourceAsStream(propFileName); 63 | if (inputStream != null) { 64 | try { 65 | prop.load(inputStream); 66 | } catch (IOException e) { 67 | log.error("Exception occurred while loading properties", e); 68 | } 69 | } 70 | setBossCount(); 71 | setWorkerCount(); 72 | } 73 | 74 | public void run() { 75 | queues = serverInformationContext.getServerConfigBuilderContext().getQueues(); 76 | final MockServerThread[] handlers = new MockServerThread[queues]; 77 | if (queues > 0) { 78 | 79 | for (int i = 0; i < queues; i++) { 80 | MockServerThread handler = new MockServerThread(); 81 | handlers[i] = handler; 82 | handler.start(); 83 | } 84 | } else { 85 | queues = 0; 86 | } 87 | 88 | /*SslContext sslCtx = null; 89 | if (protocol == Protocol.HTTPS) { 90 | SelfSignedCertificate ssc = null; 91 | try { 92 | ssc = new SelfSignedCertificate(); 93 | sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); 94 | } catch (CertificateException e) { 95 | log.error 96 | log.error(e); 97 | } catch (SSLException e) { 98 | log.error(e); 99 | } 100 | 101 | } else { 102 | sslCtx = null; 103 | }*/ 104 | // Configure the server. 105 | 106 | bossGroup = new NioEventLoopGroup(bossCount); 107 | workerGroup = new NioEventLoopGroup(workerCount); 108 | ValidationUtil.validateMandatoryParameters(serverInformationContext.getServerConfigBuilderContext()); 109 | 110 | try { 111 | ServerBootstrap serverBootstrap = new ServerBootstrap(); 112 | int connectTimeOut = serverInformationContext.getServerConfigBuilderContext().getConnectTimeOut(); 113 | 114 | if (connectTimeOut != 0) { 115 | try { 116 | Thread.sleep(connectTimeOut); 117 | } catch (InterruptedException e) { 118 | log.error(e); 119 | } 120 | } 121 | ChannelPipelineInitializer channelPipelineInitializer = new ChannelPipelineInitializer( 122 | EmulatorType.HTTP_SERVER, handlers); 123 | channelPipelineInitializer.setServerInformationContext(serverInformationContext); 124 | serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) 125 | .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) 126 | .childHandler(channelPipelineInitializer); 127 | ChannelFuture f = serverBootstrap.bind(serverInformationContext.getServerConfigBuilderContext().getHost(), 128 | serverInformationContext.getServerConfigBuilderContext().getPort()).sync(); 129 | f.channel().closeFuture().sync(); 130 | 131 | } catch (Exception e) { 132 | log.error("Exception occurred while initializing Emulator server", e); 133 | } finally { 134 | bossGroup.shutdownGracefully(); 135 | workerGroup.shutdownGracefully(); 136 | } 137 | } 138 | 139 | public void shutdown() { 140 | bossGroup.shutdownGracefully(); 141 | workerGroup.shutdownGracefully(); 142 | } 143 | 144 | public void setBossCount() { 145 | this.bossCount = Integer.parseInt(prop.getProperty("boss_count")); 146 | } 147 | 148 | public void setWorkerCount() { 149 | this.workerCount = Integer.parseInt(prop.getProperty("worker_count")); 150 | } 151 | } 152 | --------------------------------------------------------------------------------