├── .gitignore ├── components ├── src │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── wso2 │ │ │ └── carbon │ │ │ └── messaging │ │ │ ├── exceptions │ │ │ ├── NelException.java │ │ │ ├── EndPointTimeOut.java │ │ │ ├── ClientConnectorException.java │ │ │ ├── ServerConnectorException.java │ │ │ └── MessagingException.java │ │ │ ├── FaultHandler.java │ │ │ ├── CarbonCallback.java │ │ │ ├── StreamingCarbonMessage.java │ │ │ ├── MessageProcessorException.java │ │ │ ├── Writer.java │ │ │ ├── MessageDataSource.java │ │ │ ├── TransportSender.java │ │ │ ├── Header.java │ │ │ ├── CarbonTransportInitializer.java │ │ │ ├── DefaultCarbonMessage.java │ │ │ ├── CarbonMessageProcessor.java │ │ │ ├── ServerConnectorErrorHandler.java │ │ │ ├── AbstractCarbonCallback.java │ │ │ ├── StatusCarbonMessage.java │ │ │ ├── BinaryCarbonMessage.java │ │ │ ├── TextCarbonMessage.java │ │ │ ├── MapCarbonMessage.java │ │ │ ├── Constants.java │ │ │ ├── ServerConnectorProvider.java │ │ │ ├── BufferFactory.java │ │ │ ├── SerializableCarbonMessage.java │ │ │ ├── ControlCarbonMessage.java │ │ │ ├── ClientConnector.java │ │ │ ├── handler │ │ │ ├── MessagingHandler.java │ │ │ └── HandlerExecutor.java │ │ │ ├── Headers.java │ │ │ ├── MessageUtil.java │ │ │ ├── ServerConnector.java │ │ │ └── CarbonMessage.java │ └── test │ │ └── java │ │ └── org │ │ └── wso2 │ │ └── carbon │ │ └── messaging │ │ └── HeadersTestCase.java └── pom.xml ├── README.md ├── issue_template.md ├── pull_request_template.md ├── features ├── pom.xml └── etc │ └── feature.properties ├── pom.xml └── LICENSE /.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 | 14 | # Ignore everything in this directory 15 | target 16 | .classpath 17 | .settings 18 | .project 19 | *.i?? 20 | .idea 21 | .m2 22 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/exceptions/NelException.java: -------------------------------------------------------------------------------- 1 | package org.wso2.carbon.messaging.exceptions; 2 | 3 | /** 4 | * This is to represent NEL Specific exceptions. 5 | * @deprecated This class will be replaced by {@link MessagingException } 6 | */ 7 | @Deprecated 8 | public class NelException extends Exception { 9 | // This class here only for type casting. 10 | } 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to WSO2 Carbon Messaging 2 | This is the Carbon Messaging component which provides an abstraction over the protocol handling layer 3 | 4 | # Introduction 5 | Carbon transports are the protocol handling implementation of the next generation integration platform. The messages received through these different protocols needs to be unified into a common context so that the message processing layer works independently from the protocol. 6 | 7 | # Features 8 | 9 | - Defines the structure of the common message context (carbon message) which carries data through different layers of integration runtime 10 | - Provides the interface for message processing engine 11 | - Provides the abstractions for transport listeners and senders 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/FaultHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.wso2.carbon.messaging; 17 | 18 | /** 19 | * A interface responsible for implement fault handling logic. 20 | */ 21 | public interface FaultHandler { 22 | 23 | void handleFault(String statusCode, Throwable throwable, CarbonMessage carbonMessage, 24 | CarbonCallback carbonCallback); 25 | } 26 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/exceptions/EndPointTimeOut.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging.exceptions; 20 | 21 | /** 22 | * A class that represent EndpointTimeout Exception. 23 | */ 24 | public class EndPointTimeOut extends Exception { 25 | 26 | public EndPointTimeOut(String message) { 27 | super(message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/CarbonCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | /** 22 | * Callbacks are created in request path for execute in response path. 23 | * When response arrives callback methods should execute. 24 | */ 25 | public interface CarbonCallback { 26 | 27 | /** 28 | * Calls in response path to do work for response. 29 | * 30 | * @param cMsg CarbonMessage to be processed. 31 | */ 32 | void done(CarbonMessage cMsg); 33 | } 34 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/exceptions/ClientConnectorException.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 | package org.wso2.carbon.messaging.exceptions; 19 | 20 | /** 21 | * Client connector exception class used with client connector related invocation errors. 22 | */ 23 | public class ClientConnectorException extends Exception { 24 | public ClientConnectorException(String message) { 25 | super(message); 26 | } 27 | 28 | public ClientConnectorException(String message, Throwable cause) { 29 | super(message, cause); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/exceptions/ServerConnectorException.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 | package org.wso2.carbon.messaging.exceptions; 19 | 20 | /** 21 | * Server connector exception class used with server connector related invocation errors. 22 | */ 23 | public class ServerConnectorException extends Exception { 24 | public ServerConnectorException(String message) { 25 | super(message); 26 | } 27 | 28 | public ServerConnectorException(String message, Throwable cause) { 29 | super(message, cause); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/StreamingCarbonMessage.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.messaging; 20 | 21 | import java.io.InputStream; 22 | 23 | /** 24 | * {@link CarbonMessage} for streaming. This message type is capable of carrying a reference 25 | * to an {@link InputStream}. 26 | */ 27 | public class StreamingCarbonMessage extends CarbonMessage { 28 | private InputStream inputStream; 29 | 30 | public StreamingCarbonMessage(InputStream inputStream) { 31 | this.inputStream = inputStream; 32 | } 33 | 34 | @Override 35 | public InputStream getInputStream() { 36 | return inputStream; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/MessageProcessorException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | /** 22 | * Exception class to represent the MessageProcessorExceptions. 23 | */ 24 | public class MessageProcessorException extends Exception { 25 | 26 | public MessageProcessorException() { 27 | } 28 | 29 | public MessageProcessorException(String message) { 30 | super(message); 31 | } 32 | 33 | public MessageProcessorException(Throwable cause) { 34 | super(cause); 35 | } 36 | 37 | public MessageProcessorException(String message, Throwable cause) { 38 | super(message, cause); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/Writer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | import java.nio.ByteBuffer; 22 | 23 | /** 24 | * An interface which represents Write byte content directly. 25 | */ 26 | public interface Writer { 27 | /** 28 | * A method which is used to write content to underlying IO channel directly. 29 | * 30 | * @param byteBuffer ByteBuffer instance. 31 | */ 32 | void write(ByteBuffer byteBuffer); 33 | 34 | /** 35 | * A method which is used to write Last content and flush to underlying IO channel directly. 36 | * 37 | * @param carbonMessage Carbon Message Instance. 38 | */ 39 | void writeLastContent(CarbonMessage carbonMessage); 40 | } 41 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/MessageDataSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | import java.util.Map; 22 | 23 | /** 24 | * An interface which represents the message after loading in to memory (After first built) 25 | */ 26 | public interface MessageDataSource { 27 | 28 | public String getValueAsString(String path); 29 | 30 | public String getValueAsString(String path, Map properties); 31 | 32 | public Object getValue(String path); 33 | 34 | public Object getDataObject(); 35 | 36 | public String getContentType(); 37 | 38 | public void setContentType(String type); 39 | 40 | public void serializeData(); 41 | 42 | public String getMessageAsString(); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/TransportSender.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | /** 22 | * Interface for Message Sender to the BE. 23 | * @deprecated use {@link ClientConnector} instead. 24 | */ 25 | @Deprecated 26 | public interface TransportSender { 27 | /** 28 | * Should include the logic for handover messages to BE. 29 | * 30 | * @param msg Mediated Request. 31 | * @param callback Carbon callback created by Carbon Message Processor. 32 | * @return void 33 | * @throws MessageProcessorException to signal any failures to the upper layers. 34 | */ 35 | boolean send(CarbonMessage msg, CarbonCallback callback) throws MessageProcessorException; 36 | 37 | String getId(); 38 | } 39 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/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 | package org.wso2.carbon.messaging; 19 | 20 | /** 21 | * To hold the header data. 22 | */ 23 | public class Header { 24 | private String name; 25 | private String value; 26 | 27 | public Header(String name, String value) { 28 | this.name = name; 29 | this.value = value; 30 | } 31 | 32 | public String getName() { 33 | return name; 34 | } 35 | 36 | public void setName(String name) { 37 | this.name = name; 38 | } 39 | 40 | public String getValue() { 41 | return value; 42 | } 43 | 44 | public void setValue(String value) { 45 | this.value = value; 46 | } 47 | 48 | public Header getClone() { 49 | return new Header(name, value); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/CarbonTransportInitializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | import java.util.Map; 22 | 23 | /** 24 | * An implementation of this class is used for registering additional handlers to the channel pipeline. 25 | */ 26 | public interface CarbonTransportInitializer { 27 | 28 | /** 29 | * The setup method is called once to setup the CarbonNettyServerInitializer. 30 | * 31 | * @param parameters Optional transport parameters. Key - parameter name, Value - parameter value 32 | */ 33 | void setup(Map parameters); 34 | 35 | /** 36 | * This method will be true if Server Initializer. 37 | * 38 | * @return true if ServerSide Initializer 39 | */ 40 | boolean isServerInitializer(); 41 | } 42 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/DefaultCarbonMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import java.nio.ByteBuffer; 25 | import java.nio.charset.Charset; 26 | 27 | /** 28 | * Default implementation for carbon message. 29 | */ 30 | public class DefaultCarbonMessage extends CarbonMessage { 31 | private static final Logger log = LoggerFactory.getLogger(DefaultCarbonMessage.class); 32 | 33 | public DefaultCarbonMessage() { 34 | } 35 | 36 | public DefaultCarbonMessage(Boolean buffercontent) { 37 | super(buffercontent); 38 | } 39 | 40 | public void setStringMessageBody(String stringMessageBody) { 41 | addMessageBody(ByteBuffer.wrap(stringMessageBody.getBytes(Charset.defaultCharset()))); 42 | setEndOfMsgAdded(true); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/CarbonMessageProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | /** 22 | * Interface of the Carbon Message Processor which is used to process Carbon Messages. 23 | */ 24 | public interface CarbonMessageProcessor { 25 | 26 | /** 27 | * @param msg CarbopnMessage received for processing. 28 | * @param callback Callback received from transport layer to the CarbonMessageProcessor. 29 | * @return void 30 | * @throws Exception Exception to signal any failure at the message processor. 31 | */ 32 | boolean receive(CarbonMessage msg, CarbonCallback callback) throws Exception; 33 | 34 | /** 35 | * @deprecated use {@link CarbonMessageProcessor#setClientConnector(ClientConnector)} instead. 36 | */ 37 | @Deprecated 38 | void setTransportSender(TransportSender sender); 39 | 40 | /** 41 | * Method which sets the given client connector for this message processor instance which is used with 42 | * sending message via a response channel. 43 | * @param clientConnector client connector instance. 44 | */ 45 | void setClientConnector(ClientConnector clientConnector); 46 | 47 | String getId(); 48 | } 49 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/ServerConnectorErrorHandler.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 | package org.wso2.carbon.messaging; 19 | 20 | /** 21 | * {@code ServerConnectorErrorHandler} is the interface for protocol specific error handlers in ballerina. 22 | * This error handler interface should be implemented for handling transport listener related errors. 23 | * This will be used by the application for a given transport protocol associated with a {@code ServerConnector} 24 | * implementation. 25 | */ 26 | public interface ServerConnectorErrorHandler { 27 | /** 28 | * The error handling method which will be invoked by the application using the {@code ServerConnector} 29 | * associated with this error handler. 30 | * @param exception error thrown from the application level to be handled. 31 | * @param carbonMessage the carbonMessage associated with the currently request/response flow 32 | * @param callback the callback used with responding 33 | */ 34 | void handleError(Exception exception, CarbonMessage carbonMessage, CarbonCallback callback); 35 | 36 | /** 37 | * Returns the string value of the transport protocol (eg: "http", "jms", etc. ) this listener is bound to. 38 | * @return transport protocol 39 | */ 40 | String getProtocol(); 41 | } 42 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/AbstractCarbonCallback.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.messaging; 20 | 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | 24 | /** 25 | * Abstract implementation for the CarbonCallback. 26 | * Methods of this class are not thread-safe 27 | * 28 | * @since 2.3.2 29 | */ 30 | public abstract class AbstractCarbonCallback implements CarbonCallback { 31 | 32 | protected Map properties = new HashMap<>(); 33 | 34 | /** 35 | * Get a property associated with the callback 36 | * 37 | * @param key property name 38 | * @return property for a particular key 39 | */ 40 | public Object getProperty(String key) { 41 | return properties.get(key); 42 | } 43 | 44 | /** 45 | * Get all the properties associated with the callback 46 | * 47 | * @return map of all the properties 48 | */ 49 | public Map getProperties() { 50 | return properties; 51 | } 52 | 53 | /** 54 | * Set a property which is associated with the callback 55 | * 56 | * @param key key of the property 57 | * @param value value of the property 58 | */ 59 | public void setProperty(String key, Object value) { 60 | properties.put(key, value); 61 | } 62 | 63 | /** 64 | * Remove a property associated with the callback 65 | * 66 | * @param key key of the property 67 | */ 68 | public void removeProperty(String key) { 69 | properties.remove(key); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/StatusCarbonMessage.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.messaging; 20 | 21 | /** 22 | * {@link CarbonMessage} type for Status Message. This contains a status, status code and if prefers a reason text. 23 | * This is a useful message type to know about the status of a connection. If you have to process something when 24 | * the status of a connection is changed use this message type to transfer all the necessary details. 25 | */ 26 | public class StatusCarbonMessage extends CarbonMessage { 27 | 28 | private final String status; 29 | private final String reasonText; //Reason saying why the connection is closed 30 | private final int statusCode; //Status code of the connection close 31 | 32 | /** 33 | * @param statusCode Status code of reason to close. 34 | * @param reasonText Reason to close the connection. 35 | */ 36 | public StatusCarbonMessage(String status, int statusCode, String reasonText) { 37 | this.status = status; 38 | this.statusCode = statusCode; 39 | this.reasonText = reasonText; 40 | } 41 | 42 | /** 43 | * @return the status of a connection 44 | */ 45 | public String getStatus() { 46 | return status; 47 | } 48 | 49 | /** 50 | * @return Reason for closing the connection. 51 | */ 52 | public String getReasonText() { 53 | return reasonText; 54 | } 55 | 56 | /** 57 | * @return Status code of the reason to close the connection. 58 | */ 59 | public int getStatusCode() { 60 | return statusCode; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/BinaryCarbonMessage.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.messaging; 20 | 21 | import java.nio.ByteBuffer; 22 | 23 | /** 24 | * {@link CarbonMessage} type for Binary Messages. This message type handles the binary data easily. So if you are 25 | * dealing with binary data it is better to use this message type. This will work as a binary data carrier from 26 | * transport level to application level. 27 | */ 28 | public class BinaryCarbonMessage extends CarbonMessage { 29 | 30 | private ByteBuffer bytes; //ByteBuffer to store binary data 31 | private boolean finalFragment; //Check whether given fragment is final when partial messages are sent 32 | 33 | /** 34 | * @param bytes byte array of binary data. 35 | * @param finalFragment true if the message is the final fragment of the binary message. 36 | * First fragment can also be the final fragment. 37 | */ 38 | public BinaryCarbonMessage(ByteBuffer bytes, boolean finalFragment) { 39 | this.bytes = bytes; 40 | this.finalFragment = finalFragment; 41 | } 42 | 43 | /** 44 | * @return byte array of binary data contained in the message. 45 | */ 46 | public ByteBuffer readBytes() { 47 | return bytes; 48 | } 49 | 50 | /** 51 | * @return true if the message is the final fragment of the binary message. 52 | */ 53 | public boolean isFinalFragment() { 54 | return finalFragment; 55 | } 56 | 57 | @Override 58 | public ByteBuffer getMessageBody() { 59 | setAlreadyRead(true); 60 | return bytes; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/TextCarbonMessage.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.messaging; 20 | 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import java.io.ByteArrayInputStream; 25 | import java.io.InputStream; 26 | import java.io.UnsupportedEncodingException; 27 | import java.nio.ByteBuffer; 28 | import java.nio.charset.StandardCharsets; 29 | 30 | /** 31 | * {@link CarbonMessage} type for Text Messages. This message type is better if you are dealing with 32 | * text data. This will work as the text data carrier from transport level to application level. 33 | */ 34 | public class TextCarbonMessage extends CarbonMessage { 35 | 36 | private static final Logger LOG = LoggerFactory.getLogger(TextCarbonMessage.class); 37 | 38 | private final String text; 39 | 40 | /** 41 | * @param text Text Message 42 | */ 43 | public TextCarbonMessage(String text) { 44 | this.text = text; 45 | } 46 | 47 | /** 48 | * @return String included in Text Message. 49 | */ 50 | public String getText() { 51 | return text; 52 | } 53 | 54 | @Override 55 | public InputStream getInputStream() { 56 | if (text == null) { 57 | return null; 58 | } 59 | return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)); 60 | } 61 | 62 | @Override 63 | public ByteBuffer getMessageBody() { 64 | setEndOfMsgAdded(true); 65 | try { 66 | return ByteBuffer.wrap(text.getBytes("UTF-8")); 67 | } catch (UnsupportedEncodingException e) { 68 | LOG.error("Couldn't get the byteBuffer from string payload"); 69 | return null; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/MapCarbonMessage.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.messaging; 20 | 21 | import java.util.Collections; 22 | import java.util.Enumeration; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | /** 27 | * {@link CarbonMessage} type for Map message. This message type is better if you are dealing with 28 | * map types of messages. 29 | */ 30 | public class MapCarbonMessage extends CarbonMessage { 31 | private Map mapData; 32 | 33 | /** 34 | * Initializes a Map Carbon Message. 35 | */ 36 | public MapCarbonMessage() { 37 | mapData = new HashMap<>(); 38 | } 39 | 40 | /** 41 | * To set a value in the map. 42 | * 43 | * @param mapKeyName Key that need to be added to map 44 | * @param mapValue Value of the key that need to be added to map 45 | */ 46 | public void setValue(String mapKeyName, String mapValue) { 47 | mapData.put(mapKeyName, mapValue); 48 | } 49 | 50 | /** 51 | * To get the value of a particular key. 52 | * 53 | * @param mapKeyName Key to get the value for 54 | * @return the value for the specific key 55 | */ 56 | public String getValue(String mapKeyName) { 57 | return mapData.get(mapKeyName); 58 | } 59 | 60 | /** 61 | * Return all the keys in the {@link MapCarbonMessage} object. 62 | * 63 | * @return an enumeration of all the keys in the {@link MapCarbonMessage} 64 | */ 65 | public Enumeration getMapNames() { 66 | return Collections.enumeration(mapData.keySet()); 67 | } 68 | 69 | /** 70 | * Clear internal payload of the message 71 | */ 72 | public void clearMapPayload() { 73 | mapData = new HashMap<>(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/Constants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | /** 22 | * Common constants used in message property. 23 | */ 24 | public final class Constants { 25 | 26 | 27 | public static final String PROTOCOL = "PROTOCOL"; 28 | 29 | public static final String RESPONSE_CALLBACK = "RESPONSE_CALLBACK"; 30 | 31 | public static final String HOST = "HOST"; 32 | 33 | public static final String PORT = "PORT"; 34 | 35 | public static final String TO = "TO"; 36 | 37 | public static final String DIRECTION = "DIRECTION"; 38 | 39 | public static final String DIRECTION_REQUEST = "DIRECTION_REQUEST"; 40 | 41 | public static final String DIRECTION_RESPONSE = "DIRECTION_RESPONSE"; 42 | 43 | public static final String CALL_BACK = "CALL_BACK"; 44 | 45 | public static final String EXCHANGE = "EXCHANGE"; 46 | 47 | public static final String LISTENER_PORT = "LISTENER_PORT"; 48 | 49 | public static final String ERROR_MESSAGE = "ERROR_MESSAGE"; 50 | 51 | public static final String ERROR_DETAIL = "ERROR_DETAIL"; 52 | 53 | public static final String ERROR_CODE = "ERROR_CODE"; 54 | 55 | public static final String ERROR_EXCEPTION = "ERROR_EXCEPTION"; 56 | 57 | public static final String LISTENER_INTERFACE_ID = "LISTENER_INTERFACE_ID"; 58 | 59 | // Status of a status message 60 | public static final String STATUS_OPEN = "STATUS_OPEN"; 61 | public static final String STATUS_CLOSE = "STATUS_CLOSE"; 62 | 63 | // Control Signals for control message 64 | public static final String CONTROL_SIGNAL_OPEN = "CONTROL_SIGNAL_OPEN"; 65 | public static final String CONTROL_SIGNAL_CLOSE = "CONTROL_SIGNAL_CLOSE"; 66 | public static final String CONTROL_SIGNAL_HEARTBEAT = "CONTROL_SIGNAL_HEARTBEAT"; 67 | 68 | private Constants() { 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/exceptions/MessagingException.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 | 20 | package org.wso2.carbon.messaging.exceptions; 21 | 22 | /** 23 | * MessagingException is generic exception wrapper. 24 | * 25 | * An Exception that is originating from Transport level, can be wrapped as MessagingException. A CarbonMessage can be 26 | * made faulty by setting a MessagingException. 27 | * 28 | * MessagingException creator must assign an unique {@link #errorCode} for each faulty scenario. 29 | * This error code can be used to handle fault handling scenarios like Circuit Breaker. 30 | * 31 | * Eg: ( Demonstration purpose only. Refer CarbonMessageProcess implantation for specific errorCodes) 32 | * 33 | * 10100 HTTP Connection timeout. 34 | * 10101 HTTP Connection Failed. 35 | * 10102 HTTP Connection Failed. etc. 36 | */ 37 | public class MessagingException extends Exception { 38 | 39 | private int errorCode; 40 | 41 | public MessagingException(int errorCode) { 42 | this.errorCode = errorCode; 43 | } 44 | 45 | public MessagingException(String message, int errorCode) { 46 | super(message); 47 | this.errorCode = errorCode; 48 | } 49 | 50 | public MessagingException(String message, Throwable cause, int errorCode) { 51 | super(message, cause); 52 | this.errorCode = errorCode; 53 | } 54 | 55 | public MessagingException(Throwable cause, int errorCode) { 56 | super(cause); 57 | this.errorCode = errorCode; 58 | } 59 | 60 | public MessagingException(String message, Throwable cause, boolean enableSuppression, boolean 61 | writableStackTrace, int errorCode) { 62 | super(message, cause, enableSuppression, writableStackTrace); 63 | this.errorCode = errorCode; 64 | } 65 | 66 | /* Getters */ 67 | 68 | public int getErrorCode() { 69 | return errorCode; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/ServerConnectorProvider.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 | package org.wso2.carbon.messaging; 19 | 20 | import org.wso2.carbon.kernel.startupresolver.CapabilityProvider; 21 | 22 | import java.util.List; 23 | import java.util.Map; 24 | 25 | /** 26 | * This class represents the provider for server connectors. This should be used when a new connector instance 27 | * need to be created after server is started. There should be a server connector provider for each transport 28 | * protocol that server connectors are written. This will create and provide new instances of server connector 29 | * when the id is given. Also this will initialize the default server connectors that should be initialized 30 | * during application startup. 31 | */ 32 | public abstract class ServerConnectorProvider implements CapabilityProvider { 33 | /** 34 | * Protocol representing this server connector provider. 35 | */ 36 | protected String protocol; 37 | 38 | public ServerConnectorProvider(String protocol) { 39 | this.protocol = protocol; 40 | } 41 | 42 | /** 43 | * Returns the transport protocol associated with this provider. 44 | * @return transport protocol. 45 | */ 46 | public String getProtocol() { 47 | return protocol; 48 | } 49 | 50 | /** 51 | * Initialize the default set of server connectors using the given configuration and return them. 52 | * @return list of initialized connectors. 53 | */ 54 | public abstract List initializeConnectors(); 55 | 56 | /** 57 | * Returns an instance of the {@link ServerConnector} using the given id. 58 | * @param id id used to create the server connector instance. 59 | * @param properties properties required for the {@link ServerConnector} 60 | * @return newly created server connector instance. 61 | */ 62 | public abstract ServerConnector createConnector(String id, Map properties); 63 | } 64 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/BufferFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | import java.nio.ByteBuffer; 25 | import java.util.Queue; 26 | import java.util.concurrent.ConcurrentLinkedQueue; 27 | 28 | /** 29 | * A class which is responsible for create ByteBuffers. 30 | */ 31 | public class BufferFactory { 32 | 33 | private static final Logger LOGGER = LoggerFactory.getLogger(BufferFactory.class); 34 | private static BufferFactory preConfiguredBufferFactory; 35 | private static BufferFactory defaultBufferFactory = new BufferFactory(); 36 | 37 | private Queue byteBufferQueue = new ConcurrentLinkedQueue<>(); 38 | 39 | private int bufferSize = 1024 * 8; 40 | 41 | public BufferFactory(int bufferSize) { 42 | LOGGER.debug("Creating BufferFactory with BufferSize " + bufferSize); 43 | this.bufferSize = bufferSize; 44 | 45 | } 46 | 47 | private BufferFactory() { 48 | LOGGER.debug("Creating BufferFactory with default BufferSize " + this.bufferSize); 49 | 50 | } 51 | 52 | public ByteBuffer getBuffer() { 53 | 54 | ByteBuffer byteBuffer = null; 55 | if (byteBufferQueue.isEmpty()) { 56 | byteBuffer = ByteBuffer.allocate(bufferSize); 57 | } else { 58 | byteBuffer = byteBufferQueue.poll(); 59 | } 60 | return byteBuffer; 61 | } 62 | 63 | public void release(ByteBuffer buffer) { 64 | buffer.clear(); 65 | byteBufferQueue.add(buffer); 66 | } 67 | 68 | public static synchronized BufferFactory createInstance(int bufferSize) { 69 | return preConfiguredBufferFactory = new BufferFactory(bufferSize); 70 | } 71 | 72 | public static BufferFactory getInstance() { 73 | if (preConfiguredBufferFactory == null) { 74 | return defaultBufferFactory; 75 | } 76 | return preConfiguredBufferFactory; 77 | 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /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. -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/SerializableCarbonMessage.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 | package org.wso2.carbon.messaging; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.io.InputStream; 22 | import java.io.Serializable; 23 | import java.nio.charset.StandardCharsets; 24 | import java.util.HashMap; 25 | 26 | /** 27 | * {@link CarbonMessage} serializable implementation. This message type can be used when it is 28 | * required to serialize carbon messages. 29 | */ 30 | public class SerializableCarbonMessage extends CarbonMessage implements Serializable { 31 | 32 | private static final long serialVersionUID = 1; 33 | private String payload; 34 | private String payloadType; 35 | private HashMap headers; 36 | 37 | /** 38 | * To get a map of header values. 39 | * 40 | * @return Map containing headers 41 | */ 42 | public HashMap getHeadersMap() { 43 | return headers; 44 | } 45 | 46 | /** 47 | * To set header values as a map. 48 | * 49 | * @param headers Map containing headers to be set 50 | */ 51 | public void setHeaders(HashMap headers) { 52 | this.headers = headers; 53 | } 54 | 55 | /** 56 | * To get the payload of the message. 57 | * 58 | * @return Message payload 59 | */ 60 | public String getPayload() { 61 | return payload; 62 | } 63 | 64 | /** 65 | * To set the payload of the message. 66 | * 67 | * @param payload Payload string to be set 68 | */ 69 | public void setPayload(String payload) { 70 | this.payload = payload; 71 | } 72 | 73 | /** 74 | * To get the payload type of the message. 75 | * 76 | * @return String payload type 77 | */ 78 | public String getPayloadType() { 79 | return payloadType; 80 | } 81 | 82 | /** 83 | * To set the payload type of the message. 84 | * 85 | * @param payloadType String payload type 86 | */ 87 | public void setPayloadType(String payloadType) { 88 | this.payloadType = payloadType; 89 | } 90 | 91 | @Override 92 | public InputStream getInputStream() { 93 | if (payload == null) { 94 | return null; 95 | } 96 | return new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8)); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/ControlCarbonMessage.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.messaging; 20 | 21 | import java.nio.ByteBuffer; 22 | 23 | /** 24 | * {@link CarbonMessage} for control messages. This is used for control messages of a connection. 25 | * This message type is useful when you need to send control messages for a given connection. 26 | */ 27 | public class ControlCarbonMessage extends CarbonMessage { 28 | 29 | private final String controlSignal; 30 | private final ByteBuffer buffer; 31 | private final boolean finalFragment; 32 | 33 | /** 34 | * @param controlSignal String saying what kind of message signal it is. 35 | */ 36 | public ControlCarbonMessage(String controlSignal) { 37 | this.controlSignal = controlSignal; 38 | this.buffer = null; 39 | this.finalFragment = true; 40 | } 41 | 42 | /** 43 | * @param buffer byte bufffer of binary data. 44 | * @param finalFragment true if the message is the final fragment of the binary message. First fragment can 45 | * also be the final fragment. 46 | */ 47 | @Deprecated 48 | public ControlCarbonMessage(ByteBuffer buffer, boolean finalFragment) { 49 | this.controlSignal = null; 50 | this.buffer = buffer; 51 | this.finalFragment = finalFragment; 52 | } 53 | 54 | /** 55 | * This constructor is mostly used by the heart beat checks. 56 | * @param controlSignal String saying what kind of message signal it is. 57 | * @param buffer byte buffer of binary data. 58 | * @param finalFragment true if the message is the final fragment of the binary message. First fragment can 59 | * also be the final fragment. 60 | */ 61 | public ControlCarbonMessage(String controlSignal, ByteBuffer buffer, boolean finalFragment) { 62 | this.controlSignal = controlSignal; 63 | this.buffer = buffer; 64 | this.finalFragment = finalFragment; 65 | } 66 | 67 | /** 68 | * Retrieve the control signal. 69 | * @return the control signal if exists else null. 70 | */ 71 | public String getControlSignal() { 72 | return controlSignal; 73 | } 74 | 75 | /** 76 | * Retrieve the bytes sent or received in controlling. 77 | * @return byte buffer of the control data. 78 | */ 79 | public ByteBuffer readBytes() { 80 | return buffer; 81 | } 82 | 83 | /** 84 | * Retrieve whether this control message include the final fragment of control data. 85 | * @return true if this is the final fragment of control data else false. 86 | */ 87 | public boolean isFinalFragment() { 88 | return finalFragment; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /features/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | 21 | 22 | 4.0.0 23 | 24 | 25 | org.wso2.carbon.messaging 26 | org.wso2.carbon.messaging.parent 27 | 3.0.4-SNAPSHOT 28 | ../pom.xml 29 | 30 | 31 | org.wso2.carbon.messaging.feature 32 | carbon-feature 33 | WSO2 Carbon Messaging Feature 34 | http://wso2.org 35 | 36 | 37 | 38 | org.wso2.carbon.messaging 39 | org.wso2.carbon.messaging 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.wso2.carbon.maven 47 | carbon-feature-plugin 48 | ${carbon.feature.plugin.version} 49 | true 50 | 51 | 52 | 1-p2-feature-generation 53 | 54 | generate 55 | 56 | 57 | 58 | ./etc/feature.properties 59 | 60 | 61 | org.wso2.carbon.p2.category.type 62 | server 63 | 64 | 65 | org.eclipse.equinox.p2.type.group 66 | false 67 | 68 | 69 | 70 | 71 | org.wso2.carbon.messaging 72 | ${carbon.messaging.version} 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/ClientConnector.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 | package org.wso2.carbon.messaging; 19 | 20 | import org.wso2.carbon.messaging.exceptions.ClientConnectorException; 21 | 22 | import java.util.Map; 23 | 24 | /** 25 | * Message sending interface that should be implemented for a given transport protocol which will be used with 26 | * sending messages to a desired endpoint. 27 | */ 28 | public interface ClientConnector { 29 | 30 | /** 31 | * Initialize the connection. If initializing a connection is needed this can be used. 32 | * 33 | * @param cMsg carbon message used to initialize th connection. 34 | * @param callback carbon callback used to get responds if needed. 35 | * @param properties properties which needs to initialize the connection. 36 | * @return Object which can be retrieved after initializing the connection. 37 | * @throws ClientConnectorException on error while trying to initializing the connection. 38 | */ 39 | Object init(CarbonMessage cMsg, CarbonCallback callback, Map properties) 40 | throws ClientConnectorException; 41 | 42 | /** 43 | * Message sending logic to send message to a backend endpoint. 44 | * 45 | * @param msg the carbon message used with sending the a message to backend. 46 | * @param callback carbon callback used with responding any error. 47 | * @return return true if the sending was successful, false otherwise. 48 | * @throws ClientConnectorException on error while trying to send message to backend. 49 | */ 50 | boolean send(CarbonMessage msg, CarbonCallback callback) throws ClientConnectorException; 51 | 52 | /** 53 | * Message sending logic to send message to a backend endpoint. Additionally, this method accepts a map of 54 | * parameters that is used as data to construct the message to be send. 55 | * 56 | * @param msg the carbon message used with sending the a message to backend. 57 | * @param callback carbon callback used with responding any error. 58 | * @param parameters data passed from application level to be used with creating the message. 59 | * @return return true if the sending was successful, false otherwise. 60 | * @throws ClientConnectorException on error while trying to send message to backend. 61 | */ 62 | boolean send(CarbonMessage msg, CarbonCallback callback, Map parameters) 63 | throws ClientConnectorException; 64 | 65 | /** 66 | * Transport protocol associated with this client connector instance. 67 | * @return string value of the transport protocol. 68 | */ 69 | String getProtocol(); 70 | 71 | /** 72 | * Set message processor reference to the ClientConnector. 73 | * 74 | * Reference to the engine Message Processor might be required for some of the client connectors to 75 | * get back to the engine with the response. 76 | * 77 | * @param messageProcessor CarbonMessageProcessor 78 | */ 79 | void setMessageProcessor(CarbonMessageProcessor messageProcessor); 80 | } 81 | -------------------------------------------------------------------------------- /components/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | 21 | 4.0.0 22 | 23 | 24 | org.wso2.carbon.messaging 25 | org.wso2.carbon.messaging.parent 26 | 3.0.4-SNAPSHOT 27 | ../pom.xml 28 | 29 | 30 | org.wso2.carbon.messaging 31 | bundle 32 | 3.0.4-SNAPSHOT 33 | WSO2 Carbon Messaging Component 34 | 35 | 36 | 37 | org.wso2.carbon 38 | org.wso2.carbon.launcher 39 | 40 | 41 | org.wso2.carbon 42 | org.wso2.carbon.core 43 | 44 | 45 | org.wso2.carbon 46 | org.wso2.carbon.server.feature 47 | zip 48 | 49 | 50 | org.wso2.carbon 51 | org.wso2.carbon.osgi.feature 52 | zip 53 | 54 | 55 | org.slf4j 56 | slf4j-log4j12 57 | 58 | 59 | org.testng 60 | testng 61 | test 62 | 63 | 64 | 65 | 66 | 67 | 68 | org.apache.maven.plugins 69 | maven-checkstyle-plugin 70 | 71 | 72 | 73 | org.codehaus.mojo 74 | findbugs-maven-plugin 75 | 76 | 77 | org.jacoco 78 | jacoco-maven-plugin 79 | 80 | 81 | org.apache.maven.plugins 82 | maven-surefire-plugin 83 | 84 | 85 | 86 | 87 | 88 | 89 | org.wso2.carbon.messaging.*;version="${carbon.messaging.package.export.version}" 90 | 91 | 92 | org.wso2.carbon.kernel.transports.*;version="${carbon.kernel.package.import.version.range}", 93 | org.wso2.carbon.kernel.startupresolver.*;version="${carbon.kernel.package.import.version.range}", 94 | org.slf4j.*;version="${slf4j.logging.package.import.version.range}", 95 | org.apache.log4j.*, 96 | org.apache.commons.logging.* 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/handler/MessagingHandler.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.messaging.handler; 20 | 21 | import org.wso2.carbon.messaging.CarbonCallback; 22 | import org.wso2.carbon.messaging.CarbonMessage; 23 | 24 | /** 25 | * Interface for MessagingHandler. 26 | */ 27 | public interface MessagingHandler { 28 | 29 | /** 30 | * Invoked when source request's headers are received at the source handler. 31 | * 32 | * @param carbonMessage client request with headers 33 | * @param callback response callback 34 | * @return true to continue the message flow, 35 | * false to discontinue the flow and done the callback with response message. 36 | */ 37 | boolean validateRequestContinuation(CarbonMessage carbonMessage, CarbonCallback callback); 38 | 39 | /** 40 | * Invoked when source connection is initiated. 41 | * 42 | * @param metadata unique string key to identify the connection 43 | */ 44 | void invokeAtSourceConnectionInitiation(String metadata); 45 | 46 | /** 47 | * Invoked when source connection is terminated. 48 | * 49 | * @param metadata unique string key to identify the connection 50 | */ 51 | void invokeAtSourceConnectionTermination(String metadata); 52 | 53 | /** 54 | * Invoked when target connection is initiated. 55 | * 56 | * @param metadata unique string key to identify the connection 57 | */ 58 | void invokeAtTargetConnectionInitiation(String metadata); 59 | 60 | /** 61 | * Invoked when target connection is terminated. 62 | * 63 | * @param metadata unique string key to identify the connection 64 | */ 65 | void invokeAtTargetConnectionTermination(String metadata); 66 | 67 | /** 68 | * Invoked when source request is started receiving at source handler. 69 | * 70 | * @param carbonMessage newly created carbon message. 71 | */ 72 | void invokeAtSourceRequestReceiving(CarbonMessage carbonMessage); 73 | 74 | /** 75 | * Invoked when source request is started sending to the message processor. 76 | * 77 | * @param carbonMessage client request (i.e headers, property and message body) 78 | */ 79 | void invokeAtSourceRequestSending(CarbonMessage carbonMessage); 80 | 81 | /** 82 | * Invoked when the request is received again to the transport level after being processed at message processor. 83 | * 84 | * @param carbonMessage processed (or mediated) request (i.e headers, properties and message body) 85 | */ 86 | void invokeAtTargetRequestReceiving(CarbonMessage carbonMessage); 87 | 88 | /** 89 | * Invoked when the request is started sending to the backend. 90 | * 91 | * @param carbonMessage sent request (i.e the message is already had started to send to backend) 92 | * So no message body will be available. Even though the headers and properties are available, 93 | * manipulating them won't change the request send to the back end (because the headers are 94 | * already been send to the backend) 95 | */ 96 | void invokeAtTargetRequestSending(CarbonMessage carbonMessage); 97 | 98 | /** 99 | * Invoked when target response is started receiving at target handler. 100 | * 101 | * @param carbonMessage newly created carbon message. 102 | */ 103 | void invokeAtTargetResponseReceiving(CarbonMessage carbonMessage); 104 | 105 | /** 106 | * Invoked when target response is started sending to the message processor. 107 | * 108 | * @param carbonMessage target response (i.e headers, property and message body) 109 | */ 110 | void invokeAtTargetResponseSending(CarbonMessage carbonMessage); 111 | 112 | /** 113 | * Invoked when the response is received again to the transport level after being processed at message processor. 114 | * 115 | * @param carbonMessage processed (or mediated) response (i.e headers, properties and message body) 116 | */ 117 | void invokeAtSourceResponseReceiving(CarbonMessage carbonMessage); 118 | 119 | /** 120 | * Invoked when the response is started sending to the client. 121 | * 122 | * @param carbonMessage sent response (i.e with empty message body. 123 | * similar carbon message to {@link #invokeAtTargetRequestSending(CarbonMessage)} 124 | */ 125 | void invokeAtSourceResponseSending(CarbonMessage carbonMessage); 126 | 127 | /** 128 | * Gives handler name. 129 | * 130 | * @return handler name 131 | */ 132 | String handlerName(); 133 | } 134 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/Headers.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.messaging; 19 | 20 | import java.util.LinkedList; 21 | import java.util.List; 22 | import java.util.Map; 23 | import java.util.TreeMap; 24 | import java.util.stream.Collectors; 25 | 26 | /** 27 | * This will hold all the headers of incoming message. 28 | */ 29 | public class Headers { 30 | /** 31 | * A map that case insensitive to hold the headers. 32 | */ 33 | private Map headerMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); 34 | 35 | /** 36 | * A list to hold all the headers without considering the header name case. This will hold 37 | * duplicated headers as well if incoming message containing duplicated headers. 38 | */ 39 | private List
headerList = new LinkedList<>(); 40 | 41 | public Headers() { 42 | } 43 | 44 | public Headers(List
list) { 45 | this.set(list); 46 | } 47 | 48 | /** 49 | * To add a header. if the header name is already exist, It will replace the existing header 50 | * value and if there is duplicated header with same name, remove all of them and add the given 51 | * header. 52 | * @param name name of the header field 53 | * @param value value of the header field 54 | */ 55 | public void set(String name, String value) { 56 | headerList.removeIf(header -> header.getName().equalsIgnoreCase(name)); 57 | add(name, value); 58 | } 59 | 60 | /** 61 | * This will add all header in given map to the message. 62 | * @param map map containing header fields 63 | */ 64 | public void set(final Map map) { 65 | map.forEach(this::add); 66 | } 67 | 68 | /** 69 | * This will add all header objects in given list into the message. 70 | * @param list list containing Header objects 71 | */ 72 | public void set(final List
list) { 73 | list.forEach(h -> add(h.getName(), h.getValue())); 74 | } 75 | 76 | /** 77 | * This will remove all the headers from the message regardless of the case sensitivity. 78 | * @param name name of the header to be removed 79 | */ 80 | public void remove(String name) { 81 | headerList.removeIf(header -> header.getName().equalsIgnoreCase(name)); 82 | headerMap.remove(name); 83 | } 84 | 85 | /** 86 | * This will return the header field value for given name. if there are multiple headers with 87 | * same name, It will return the value of last header matching the name regardless of 88 | * case sensitivity of the name of the header. 89 | * @param name name of the header field 90 | * @return value of the header for given name 91 | */ 92 | public String get(String name) { 93 | return headerMap.get(name); 94 | } 95 | 96 | /** 97 | * This will return a LinkedList having all the headers. 98 | * 99 | * @return a list containing all the headers fields. 100 | */ 101 | public List
getAll() { 102 | return headerList; 103 | } 104 | 105 | public List
getClone() { 106 | List
clonedHeaderList = new LinkedList<>(); 107 | 108 | headerList.forEach(header -> { 109 | clonedHeaderList.add(header.getClone()); 110 | }); 111 | return clonedHeaderList; 112 | } 113 | 114 | /** 115 | * This will return a values fo given header name regardless of case sensitivity as a LikedList. 116 | * @param name name of the header 117 | * @return a list containing the value of given header name regardless of case sensitivity 118 | */ 119 | public List getAllBy(String name) { 120 | List hList = headerList.stream().filter(entry -> entry.getName().equalsIgnoreCase(name)) 121 | .map(Header::getValue).collect(Collectors.toCollection(LinkedList::new)); 122 | return hList; 123 | } 124 | 125 | /** 126 | * To check whether given header name is exist in the header map regardless of the 127 | * case sensitivity. 128 | * @param name name of a header 129 | * @return true if the header is exist. 130 | */ 131 | public boolean contains(String name) { 132 | return headerMap.containsKey(name); 133 | } 134 | 135 | /** 136 | * To clear all the header from list. 137 | */ 138 | public void clear() { 139 | headerList.clear(); 140 | headerMap.clear(); 141 | } 142 | 143 | /** 144 | * To get the distinct size of headers. 145 | * @return distinct size of the headers 146 | */ 147 | public int distinctSize() { 148 | return headerMap.size(); 149 | } 150 | 151 | /** 152 | * To get the number of of headers in list. 153 | * @return size of header list 154 | */ 155 | public int size() { 156 | return headerList.size(); 157 | } 158 | 159 | private void add(String key, String value) { 160 | headerList.add(new Header(key, value)); 161 | headerMap.put(key, value); 162 | } 163 | 164 | } 165 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/MessageUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | import java.nio.ByteBuffer; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | import java.util.Map; 25 | 26 | /** 27 | * A class which has utility methods for manipulate messages 28 | */ 29 | public class MessageUtil { 30 | 31 | /** 32 | * Copy Message properties and transport headers 33 | * 34 | * @param carbonMessage CarbonMessage 35 | * @return CarbonMessage 36 | */ 37 | public static CarbonMessage cloneCarbonMessageWithOutData(CarbonMessage carbonMessage) { 38 | 39 | CarbonMessage newCarbonMessage = new DefaultCarbonMessage(carbonMessage.isBufferContent()); 40 | 41 | List
transportHeaders = carbonMessage.getHeaders().getClone(); 42 | 43 | newCarbonMessage.setHeaders(transportHeaders); 44 | 45 | Map propertiesMap = carbonMessage.getProperties(); 46 | 47 | propertiesMap.forEach((key, value) -> newCarbonMessage.setProperty(key, value)); 48 | 49 | newCarbonMessage.setWriter(carbonMessage.getWriter()); 50 | newCarbonMessage.setFaultHandlerStack(carbonMessage.getFaultHandlerStack()); 51 | return newCarbonMessage; 52 | } 53 | 54 | /** 55 | * Copy the Full carbon message with data 56 | * 57 | * @param carbonMessage CarbonMessage 58 | * @return carbonMessage 59 | */ 60 | public static CarbonMessage cloneCarbonMessageWithData(CarbonMessage carbonMessage) { 61 | 62 | CarbonMessage newCarbonMessage = new DefaultCarbonMessage(carbonMessage.isBufferContent()); 63 | 64 | List
transportHeaders = carbonMessage.getHeaders().getClone(); 65 | 66 | newCarbonMessage.setHeaders(transportHeaders); 67 | 68 | Map propertiesMap = carbonMessage.getProperties(); 69 | 70 | propertiesMap.forEach((key, value) -> newCarbonMessage.setProperty(key, value)); 71 | 72 | newCarbonMessage.setWriter(carbonMessage.getWriter()); 73 | newCarbonMessage.setFaultHandlerStack(carbonMessage.getFaultHandlerStack()); 74 | 75 | carbonMessage.getCopyOfFullMessageBody().forEach(buffer -> newCarbonMessage.addMessageBody(buffer)); 76 | newCarbonMessage.setEndOfMsgAdded(true); 77 | return newCarbonMessage; 78 | } 79 | 80 | public static ByteBuffer deepCopy(ByteBuffer orig) { 81 | int pos = orig.position(), lim = orig.limit(); 82 | try { 83 | orig.position(0).limit(orig.capacity()); // set range to entire buffer 84 | ByteBuffer toReturn = deepCopyVisible(orig); // deep copy range 85 | toReturn.position(pos).limit(lim); // set range to original 86 | return toReturn; 87 | } finally { // do in finally in case something goes wrong we don't bork the orig 88 | 89 | orig.position(pos).limit(lim); // restore original 90 | } 91 | } 92 | 93 | public static ByteBuffer deepCopyVisible(ByteBuffer orig) { 94 | int pos = orig.position(); 95 | try { 96 | ByteBuffer toReturn; 97 | // try to maintain implementation to keep performance 98 | if (orig.isDirect()) { 99 | toReturn = ByteBuffer.allocateDirect(orig.remaining()); 100 | } else { 101 | toReturn = ByteBuffer.allocate(orig.remaining()); 102 | } 103 | 104 | toReturn.put(orig); 105 | toReturn.order(orig.order()); 106 | 107 | return (ByteBuffer) toReturn.position(0); 108 | } finally { 109 | orig.position(pos); 110 | } 111 | } 112 | 113 | /** 114 | * Creates a {@link MapCarbonMessage} using a {@link CarbonMessage}. Internal Map content is 115 | * not populated when {@link MapCarbonMessage} is constructed. 116 | * 117 | * @param carbonMessage {@link CarbonMessage} 118 | */ 119 | public static MapCarbonMessage createMapMessageWithoutData(CarbonMessage carbonMessage) { 120 | MapCarbonMessage mapCarbonMessage = new MapCarbonMessage(); 121 | mapCarbonMessage.setHeaders(new ArrayList<>(carbonMessage.headers.getAll())); 122 | carbonMessage.getProperties().forEach(mapCarbonMessage::setProperty); 123 | mapCarbonMessage.setWriter(carbonMessage.getWriter()); 124 | mapCarbonMessage.setFaultHandlerStack(carbonMessage.getFaultHandlerStack()); 125 | return mapCarbonMessage; 126 | } 127 | 128 | /** 129 | * Creates a {@link TextCarbonMessage} using a {@link CarbonMessage}. 130 | * 131 | * @param message {@link CarbonMessage} 132 | * @return TextCarbonMessage 133 | */ 134 | public static TextCarbonMessage createTextMessageWithData(CarbonMessage message) { 135 | TextCarbonMessage textCarbonMessage = 136 | new TextCarbonMessage(message.getMessageDataSource().getMessageAsString()); 137 | textCarbonMessage.setHeaders(new ArrayList<>(message.headers.getAll())); 138 | message.getProperties().forEach(textCarbonMessage::setProperty); 139 | textCarbonMessage.setWriter(message.getWriter()); 140 | textCarbonMessage.setFaultHandlerStack(message.getFaultHandlerStack()); 141 | return textCarbonMessage; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/ServerConnector.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 | package org.wso2.carbon.messaging; 19 | 20 | import org.wso2.carbon.messaging.exceptions.ServerConnectorException; 21 | 22 | import java.util.Map; 23 | 24 | /** 25 | * This class represents a server connector. When adding a new server connector to handle inbound requests over 26 | * different transport protocols such as http, jms, file, etc., this class needs to be extended, and the relevant 27 | * methods needs to be implemented. A server connector initialize ans destroy methods are called during application 28 | * startup and shutdown respectively. Similarly, the start-maintenance and end-maintenance methods will be invoked, 29 | * when the application runtime needs to put in the maintenance mode and then resume accepting requests. 30 | */ 31 | public abstract class ServerConnector { 32 | /** 33 | * Unique ID representing a server connector. 34 | */ 35 | protected String id; 36 | 37 | protected State state = State.UNINITIALIZED; 38 | 39 | protected Map properties; 40 | 41 | protected ServerConnectorErrorHandler errorHandler; 42 | 43 | public ServerConnector(String id, Map properties) { 44 | this.id = id; 45 | this.properties = properties; 46 | } 47 | 48 | /** 49 | * Get the properties of the connector. 50 | * @return properties. 51 | */ 52 | public Map getProperties() { 53 | return properties; 54 | } 55 | 56 | /** 57 | * Set the message processor to be used with this connector for dispatching. 58 | * @param messageProcessor message processor instance 59 | */ 60 | public abstract void setMessageProcessor(CarbonMessageProcessor messageProcessor); 61 | 62 | /** 63 | * Set the error handler to be used with this connector 64 | * @param errorHandler error handler instance 65 | */ 66 | public void setServerConnectorErrorHandler(ServerConnectorErrorHandler errorHandler) { 67 | this.errorHandler = errorHandler; 68 | } 69 | 70 | /** 71 | * Returns the error handler associated with this connector 72 | * @return error handler instance 73 | */ 74 | public ServerConnectorErrorHandler getServerConnectorErrorHandler() { 75 | return errorHandler; 76 | } 77 | 78 | /** 79 | * Returns the id of the connector. 80 | * @return connector id 81 | */ 82 | public String getId() { 83 | return id; 84 | } 85 | 86 | /** 87 | * Returns the current life cycle state of the connector. 88 | * @return lifecycle state 89 | */ 90 | public State getState() { 91 | return state; 92 | } 93 | 94 | public void initConnector() throws ServerConnectorException { 95 | if (state.equals(State.UNINITIALIZED) || state.equals(State.IN_MAINTENANCE)) { 96 | init(); 97 | state = State.INITIALIZED; 98 | } else { 99 | throw new IllegalStateException("Cannot initialize connector " + id + ". Current state: " + state); 100 | } 101 | } 102 | 103 | /** 104 | * Implementation of the connector init process. 105 | */ 106 | protected abstract void init() throws ServerConnectorException; 107 | 108 | public void destroyConnector() throws ServerConnectorException { 109 | if (state.equals(State.INITIALIZED)) { 110 | destroy(); 111 | state = State.UNINITIALIZED; 112 | } else { 113 | throw new IllegalStateException("Cannot destroy connector " + id + ". Current state: " + state); 114 | } 115 | } 116 | 117 | /** 118 | * Implementation of the connector destroy process. 119 | */ 120 | protected abstract void destroy() throws ServerConnectorException; 121 | 122 | public void beginConnectorMaintenance() throws ServerConnectorException { 123 | if (state.equals(State.INITIALIZED)) { 124 | beginMaintenance(); 125 | state = State.IN_MAINTENANCE; 126 | } else { 127 | throw new IllegalStateException("Cannot put connector " + id + 128 | " into maintenance. Current state: " + state); 129 | } 130 | } 131 | 132 | /** 133 | * Implementation of the connector begin maintenance process. 134 | */ 135 | protected abstract void beginMaintenance() throws ServerConnectorException; 136 | 137 | public void endConnectorMaintenance() throws ServerConnectorException { 138 | if (state.equals(State.IN_MAINTENANCE)) { 139 | endMaintenance(); 140 | state = State.INITIALIZED; 141 | } else { 142 | throw new IllegalStateException("Cannot end maintenance of connector " + id + ". Current state: " + state); 143 | } 144 | } 145 | 146 | /** 147 | * Implementation of the connector end maintenance process. 148 | */ 149 | protected abstract void endMaintenance() throws ServerConnectorException; 150 | 151 | /** 152 | * Implementation of the connector start method. Different connectors will use various approach to start the 153 | * connector (http will start bind on an interface, jms will start subscribe to a topic/queue). 154 | * 155 | * @throws ServerConnectorException when an error occurs during starting the connector. 156 | */ 157 | public abstract void start() throws ServerConnectorException; 158 | 159 | /** 160 | * Implementation of the connector stop method. Different connectors will use various approach to stop the 161 | * connector (http will stop bind from an interface, jms will un-subscribe from a topic/queue) 162 | * 163 | * @throws ServerConnectorException when an error occurs during stoping the connector. 164 | */ 165 | public abstract void stop() throws ServerConnectorException; 166 | 167 | /** 168 | * Enum to holds the state of connector. 169 | */ 170 | public enum State { 171 | UNINITIALIZED, INITIALIZED, IN_MAINTENANCE; 172 | 173 | @Override 174 | public String toString() { 175 | return name(); 176 | } 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/handler/HandlerExecutor.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.messaging.handler; 20 | 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | import org.wso2.carbon.messaging.CarbonCallback; 24 | import org.wso2.carbon.messaging.CarbonMessage; 25 | 26 | import java.util.HashMap; 27 | import java.util.Map; 28 | 29 | /** 30 | * The class that is responsible for engaging all the interceptors. 31 | */ 32 | public class HandlerExecutor { 33 | 34 | private static final Logger LOG = LoggerFactory.getLogger(HandlerExecutor.class); 35 | private Map handlers = new HashMap<>(); 36 | 37 | public boolean executeRequestContinuationValidator(CarbonMessage carbonMessage, CarbonCallback callback) { 38 | try { 39 | handlers.forEach((k, v) -> v.validateRequestContinuation(carbonMessage, callback)); 40 | for (Map.Entry messagingHandlerEntry : handlers.entrySet()) { 41 | if (!messagingHandlerEntry.getValue() 42 | .validateRequestContinuation(carbonMessage, callback)) { 43 | return false; 44 | } 45 | } 46 | } catch (Exception e) { 47 | LOG.error("Error while executing handler at Source connection initiation ", e); 48 | } 49 | return true; 50 | } 51 | 52 | public void executeAtSourceConnectionInitiation(String metadata) { 53 | try { 54 | handlers.forEach((k, v) -> v.invokeAtSourceConnectionInitiation(metadata)); 55 | } catch (Exception e) { 56 | LOG.error("Error while executing handler at Source connection initiation ", e); 57 | } 58 | } 59 | 60 | public void executeAtSourceConnectionTermination(String metadata) { 61 | try { 62 | handlers.forEach((k, v) -> v.invokeAtSourceConnectionTermination(metadata)); 63 | } catch (Exception e) { 64 | LOG.error("Error while executing handler at Source connection termination ", e); 65 | } 66 | } 67 | 68 | public void executeAtSourceRequestReceiving(CarbonMessage carbonMessage) { 69 | try { 70 | handlers.forEach((k, v) -> v.invokeAtSourceRequestReceiving(carbonMessage)); 71 | } catch (Exception e) { 72 | LOG.error("Error while executing handler at Source request receiving ", e); 73 | } 74 | } 75 | 76 | public void executeAtSourceRequestSending(CarbonMessage carbonMessage) { 77 | try { 78 | handlers.forEach((k, v) -> v.invokeAtSourceRequestSending(carbonMessage)); 79 | } catch (Exception e) { 80 | LOG.error("Error while executing handler at Source request sending ", e); 81 | } 82 | } 83 | 84 | public void executeAtTargetRequestReceiving(CarbonMessage carbonMessage) { 85 | try { 86 | handlers.forEach((k, v) -> v.invokeAtTargetRequestReceiving(carbonMessage)); 87 | } catch (Exception e) { 88 | LOG.error("Error while executing handler at Target request receiving ", e); 89 | } 90 | } 91 | 92 | public void executeAtTargetRequestSending(CarbonMessage carbonMessage) { 93 | try { 94 | handlers.forEach((k, v) -> v.invokeAtTargetRequestSending(carbonMessage)); 95 | } catch (Exception e) { 96 | LOG.error("Error while executing handler at Target request sending ", e); 97 | } 98 | } 99 | 100 | public void executeAtTargetResponseReceiving(CarbonMessage carbonMessage) { 101 | try { 102 | handlers.forEach((k, v) -> v.invokeAtTargetResponseReceiving(carbonMessage)); 103 | } catch (Exception e) { 104 | LOG.error("Error while executing handler at Target response receiving ", e); 105 | } 106 | } 107 | 108 | public void executeAtTargetResponseSending(CarbonMessage carbonMessage) { 109 | try { 110 | handlers.forEach((k, v) -> v.invokeAtTargetResponseSending(carbonMessage)); 111 | } catch (Exception e) { 112 | LOG.error("Error while executing handler at Target response sending ", e); 113 | } 114 | } 115 | 116 | public void executeAtSourceResponseReceiving(CarbonMessage carbonMessage) { 117 | try { 118 | handlers.forEach((k, v) -> v.invokeAtSourceResponseReceiving(carbonMessage)); 119 | } catch (Exception e) { 120 | LOG.error("Error while executing handler at Source response receiving ", e); 121 | } 122 | } 123 | 124 | public void executeAtSourceResponseSending(CarbonMessage carbonMessage) { 125 | try { 126 | handlers.forEach((k, v) -> v.invokeAtSourceResponseSending(carbonMessage)); 127 | } catch (Exception e) { 128 | LOG.error("Error while executing handler at Source response sending ", e); 129 | } 130 | } 131 | 132 | public void executeAtTargetConnectionInitiation(String metadata) { 133 | try { 134 | handlers.forEach((k, v) -> v.invokeAtTargetConnectionInitiation(metadata)); 135 | } catch (Exception e) { 136 | LOG.error("Error while executing handler at Target connection initiation ", e); 137 | } 138 | } 139 | 140 | public void executeAtTargetConnectionTermination(String metadata) { 141 | try { 142 | handlers.forEach((k, v) -> v.invokeAtTargetConnectionTermination(metadata)); 143 | } catch (Exception e) { 144 | LOG.error("Error while executing handler at Target connection termination ", e); 145 | } 146 | } 147 | 148 | public void addHandler(MessagingHandler messagingHandler) { 149 | handlers.put(messagingHandler.handlerName(), messagingHandler); 150 | LOG.info("A new handler named " + messagingHandler.handlerName() + " is added to the Handler Executor"); 151 | } 152 | 153 | public void removeHandler(MessagingHandler messagingHandler) { 154 | handlers.remove(messagingHandler.handlerName()); 155 | LOG.info("Handler named " + messagingHandler.handlerName() + " is removed from the Handler Executor"); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /components/src/test/java/org/wso2/carbon/messaging/HeadersTestCase.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.messaging; 19 | 20 | import org.testng.Assert; 21 | import org.testng.annotations.BeforeClass; 22 | import org.testng.annotations.Test; 23 | 24 | import java.util.HashMap; 25 | import java.util.LinkedList; 26 | import java.util.List; 27 | import java.util.Map; 28 | 29 | /** 30 | * Test case for headers 31 | */ 32 | public class HeadersTestCase { 33 | private List
list = new LinkedList<>(); 34 | 35 | @BeforeClass(alwaysRun = true) 36 | public void createHeaders() { 37 | list.add(new Header("Content-Type", "application/xml")); 38 | list.add(new Header("POST", "/services/SimpleStockQuoteService HTTP/1.1")); 39 | list.add(new Header("Connection", "close")); 40 | list.add(new Header("Set-Cookie", "31abd753ed23r876")); 41 | list.add(new Header("Set-Cookie", "41abd054ed23r325")); 42 | list.add(new Header("set-cookie", "51abv054ed23r338")); 43 | } 44 | 45 | @Test(description = "Creating the headers instance by parsing a list") 46 | public void createHeadersInstanceTest() { 47 | Headers headers = new Headers(list); 48 | Assert.assertEquals(headers.getAll().size(), list.size(), "Headers length mismatched"); 49 | } 50 | 51 | @Test(description = "Testing the get operation") 52 | public void getTest() { 53 | Headers headers = new Headers(list); 54 | Assert.assertEquals(headers.get("Content-Type"), "application/xml", "Not the correct header value"); 55 | Assert.assertEquals(headers.get("content-type"), "application/xml", "Not the correct header value " + 56 | "when getting value case insensitively"); 57 | Assert.assertEquals(headers.get("Set-Cookie"), "51abv054ed23r338", "Not the last value of the header"); 58 | Assert.assertNull(headers.get("NonExistingHeader"), "Value must be null for non existing header"); 59 | } 60 | 61 | @Test(description = "Testing the contains operation") 62 | public void containsTest() { 63 | Headers headers = new Headers(list); 64 | Assert.assertTrue(headers.contains("Content-Type"), "Header should be present"); 65 | Assert.assertTrue(headers.contains("content-type"), "Header should be present when checking " + 66 | "case insensitively"); 67 | Assert.assertTrue(headers.contains("Set-Cookie"), "Header should be present"); 68 | Assert.assertFalse(headers.contains("NonExistingHeader"), "Header should not be present"); 69 | } 70 | 71 | @Test(description = "Testing the getAll operation") 72 | public void getAllTest() { 73 | Headers headers = new Headers(list); 74 | Assert.assertEquals(headers.getAllBy("Content-Type").size(), 1, "Only one record should exist"); 75 | Assert.assertEquals(headers.getAllBy("content-type").size(), 1, "Only one record should exist"); 76 | Assert.assertEquals(headers.getAllBy("content-type").get(0), "application/xml" 77 | , "Header value not correct"); 78 | Assert.assertEquals(headers.getAllBy("Content-Type").get(0), "application/xml" 79 | , "Header value not correct"); 80 | Assert.assertEquals(headers.getAllBy("Set-Cookie").size(), 3, "List should have 3 elements"); 81 | Assert.assertEquals(headers.getAllBy("Set-Cookie").get(0), "31abd753ed23r876", "Invalid value"); 82 | Assert.assertEquals(headers.getAllBy("Set-Cookie").get(1), "41abd054ed23r325", "Invalid value"); 83 | Assert.assertEquals(headers.getAllBy("Set-Cookie").get(2), "51abv054ed23r338", "Invalid value"); 84 | Assert.assertEquals(headers.getAllBy("NonExistingHeader").size(), 0 85 | , "Should be a empty list for nun existing header"); 86 | } 87 | 88 | @Test(description = "Testing the set header operation") 89 | public void setTest() { 90 | Headers headers = new Headers(list); 91 | //setting a single header entry 92 | headers.set("Content-Type", "application/json"); 93 | Assert.assertEquals(headers.get("Content-Type"), "application/json", "header not updated"); 94 | Assert.assertEquals(headers.getAll().size(), list.size(), "Number of headers changed while " + 95 | "setting a single value header"); 96 | //setting a single header entry case insensitively 97 | headers.set("connection", "keepAlive"); 98 | Assert.assertEquals(headers.get("connection"), "keepAlive", "header not updated case insensitively"); 99 | Assert.assertEquals(headers.getAll().size(), list.size(), "Number of headers changed while " + 100 | "setting a single value header"); 101 | //setting a new header 102 | headers.set("NewHeader", "NewValue"); 103 | Assert.assertEquals(headers.get("NewHeader"), "NewValue", "header not updated"); 104 | Assert.assertEquals(headers.getAll().size(), list.size() + 1, "Number of headers changed while " + 105 | "setting a header"); 106 | //setting a header which has multiple entries 107 | int currentLength = headers.getAll().size(); 108 | headers.set("Set-Cookie", "61abv054ed23r338"); 109 | Assert.assertEquals(headers.get("Set-Cookie"), "61abv054ed23r338", "header not updated"); 110 | Assert.assertEquals(headers.getAll().size(), currentLength - 2, "Number of headers changed while " + 111 | "setting a header"); 112 | Assert.assertEquals(headers.get("set-cookie"), "61abv054ed23r338", "header not updated case insensitively"); 113 | Assert.assertEquals(headers.getAll().size(), currentLength - 2, "Number of headers changed while " + 114 | "setting a header multivalued header"); 115 | } 116 | 117 | @Test(description = "Updating header by priding header map") 118 | public void setMapTest() { 119 | Headers headers = new Headers(list); 120 | Assert.assertEquals(list.size(), headers.getAll().size(), "Headers length mismatched"); 121 | Map headerMap = new HashMap<>(); 122 | headerMap.put("Content-Type", "application/json"); 123 | headerMap.put("connection", "KeepAlive"); 124 | headerMap.put("Set-Cookie", "71abv054ed23r483"); 125 | headerMap.put("NewHeader", "NewValue"); 126 | headers.set(headerMap); 127 | Assert.assertEquals(headers.getAll().size(), 10, "Invalid headers list length after updating from map"); 128 | Assert.assertEquals(headers.distinctSize(), 5, "Invalid headers map length after updating from map"); 129 | Assert.assertEquals(headers.get("Content-Type"), "application/json", "Not the correct header value"); 130 | Assert.assertEquals(headers.get("NewHeader"), "NewValue", "Not the correct header value"); 131 | Assert.assertEquals(headers.get("Set-Cookie"), "71abv054ed23r483", "Not the correct header value"); 132 | Assert.assertEquals(headers.get("Connection"), "KeepAlive", "Not the correct header value"); 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 4.0.0 21 | 22 | 23 | org.wso2 24 | wso2 25 | 5 26 | 27 | 28 | org.wso2.carbon.messaging 29 | org.wso2.carbon.messaging.parent 30 | pom 31 | 3.0.4-SNAPSHOT 32 | WSO2 Carbon Messaging Parent 33 | 34 | 35 | https://github.com/wso2/carbon-messaging.git 36 | scm:git:https://github.com/wso2/carbon-messaging.git 37 | scm:git:https://github.com/wso2/carbon-messaging.git 38 | HEAD 39 | 40 | 41 | 42 | 43 | 44 | org.wso2.carbon 45 | org.wso2.carbon.core 46 | ${carbon.kernel.version} 47 | 48 | 49 | org.wso2.carbon 50 | org.wso2.carbon.launcher 51 | ${carbon.kernel.version} 52 | 53 | 54 | org.wso2.carbon 55 | org.wso2.carbon.server.feature 56 | ${carbon.kernel.version} 57 | zip 58 | 59 | 60 | org.wso2.carbon 61 | org.wso2.carbon.osgi.feature 62 | ${carbon.kernel.version} 63 | zip 64 | 65 | 66 | org.slf4j 67 | slf4j-log4j12 68 | ${slf4j.version} 69 | 70 | 71 | org.wso2.carbon.messaging 72 | org.wso2.carbon.messaging 73 | ${carbon.messaging.version} 74 | 75 | 76 | org.testng 77 | testng 78 | ${testng.version} 79 | test 80 | 81 | 82 | 83 | 84 | 85 | 5.2.0 86 | [5.0.0, 6.0.0) 87 | 88 | 1.7.5 89 | [1.7.1, 2.0.0) 90 | 91 | 3.0.0 92 | 93 | 3.0.4-SNAPSHOT 94 | 95 | ${carbon.messaging.version} 96 | 97 | 98 | 1.8 99 | 1.8 100 | 101 | 6.9.10 102 | 103 | 104 | 105 | 106 | components 107 | features 108 | 109 | 110 | 111 | 112 | 113 | org.jacoco 114 | jacoco-maven-plugin 115 | 0.8.0 116 | 117 | 118 | default-prepare-agent-by-coverage-enforcer 119 | 120 | prepare-agent 121 | 122 | 123 | argLine 124 | ${project.build.directory}/jacoco.exec 125 | 126 | 127 | 128 | default-report-by-coverage-enforcer 129 | 130 | report 131 | 132 | 133 | ${project.build.directory}/jacoco.exec 134 | 135 | 136 | 137 | default-check-by-coverage-enforcer 138 | 139 | check 140 | 141 | 142 | ${project.build.directory}/jacoco.exec 143 | 144 | 145 | 146 | BUNDLE 147 | 148 | 149 | 150 | LINE 151 | COVEREDRATIO 152 | 0.0 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | org.apache.maven.plugins 163 | maven-surefire-plugin 164 | 2.21.0 165 | 166 | ${argLine} 167 | 168 | 169 | 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /features/etc/feature.properties: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ################################################################################ 16 | 17 | providerName=WSO2 Inc. 18 | 19 | ########################## license properties ################################## 20 | licenseURL=http://www.apache.org/licenses/LICENSE-2.0 21 | 22 | license=\ 23 | Apache License\n\ 24 | Version 2.0, January 2004\n\ 25 | http://www.apache.org/licenses/\n\ 26 | \n\ 27 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\ 28 | \n\ 29 | 1. Definitions.\n\ 30 | \n\ 31 | "License" shall mean the terms and conditions for use, reproduction,\n\ 32 | and distribution as defined by Sections 1 through 9 of this document.\n\ 33 | \n\ 34 | "Licensor" shall mean the copyright owner or entity authorized by\n\ 35 | the copyright owner that is granting the License.\n\ 36 | \n\ 37 | "Legal Entity" shall mean the union of the acting entity and all\n\ 38 | other entities that control, are controlled by, or are under common\n\ 39 | control with that entity. For the purposes of this definition,\n\ 40 | "control" means (i) the power, direct or indirect, to cause the\n\ 41 | direction or management of such entity, whether by contract or\n\ 42 | otherwise, or (ii) ownership of fifty percent (50%) or more of the\n\ 43 | outstanding shares, or (iii) beneficial ownership of such entity.\n\ 44 | \n\ 45 | "You" (or "Your") shall mean an individual or Legal Entity\n\ 46 | exercising permissions granted by this License.\n\ 47 | \n\ 48 | "Source" form shall mean the preferred form for making modifications,\n\ 49 | including but not limited to software source code, documentation\n\ 50 | source, and configuration files.\n\ 51 | \n\ 52 | "Object" form shall mean any form resulting from mechanical\n\ 53 | transformation or translation of a Source form, including but\n\ 54 | not limited to compiled object code, generated documentation,\n\ 55 | and conversions to other media types.\n\ 56 | \n\ 57 | "Work" shall mean the work of authorship, whether in Source or\n\ 58 | Object form, made available under the License, as indicated by a\n\ 59 | copyright notice that is included in or attached to the work\n\ 60 | (an example is provided in the Appendix below).\n\ 61 | \n\ 62 | "Derivative Works" shall mean any work, whether in Source or Object\n\ 63 | form, that is based on (or derived from) the Work and for which the\n\ 64 | editorial revisions, annotations, elaborations, or other modifications\n\ 65 | represent, as a whole, an original work of authorship. For the purposes\n\ 66 | of this License, Derivative Works shall not include works that remain\n\ 67 | separable from, or merely link (or bind by name) to the interfaces of,\n\ 68 | the Work and Derivative Works thereof.\n\ 69 | \n\ 70 | "Contribution" shall mean any work of authorship, including\n\ 71 | the original version of the Work and any modifications or additions\n\ 72 | to that Work or Derivative Works thereof, that is intentionally\n\ 73 | submitted to Licensor for inclusion in the Work by the copyright owner\n\ 74 | or by an individual or Legal Entity authorized to submit on behalf of\n\ 75 | the copyright owner. For the purposes of this definition, "submitted"\n\ 76 | means any form of electronic, verbal, or written communication sent\n\ 77 | to the Licensor or its representatives, including but not limited to\n\ 78 | communication on electronic mailing lists, source code control systems,\n\ 79 | and issue tracking systems that are managed by, or on behalf of, the\n\ 80 | Licensor for the purpose of discussing and improving the Work, but\n\ 81 | excluding communication that is conspicuously marked or otherwise\n\ 82 | designated in writing by the copyright owner as "Not a Contribution."\n\ 83 | \n\ 84 | "Contributor" shall mean Licensor and any individual or Legal Entity\n\ 85 | on behalf of whom a Contribution has been received by Licensor and\n\ 86 | subsequently incorporated within the Work.\n\ 87 | \n\ 88 | 2. Grant of Copyright License. Subject to the terms and conditions of\n\ 89 | this License, each Contributor hereby grants to You a perpetual,\n\ 90 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n\ 91 | copyright license to reproduce, prepare Derivative Works of,\n\ 92 | publicly display, publicly perform, sublicense, and distribute the\n\ 93 | Work and such Derivative Works in Source or Object form.\n\ 94 | \n\ 95 | 3. Grant of Patent License. Subject to the terms and conditions of\n\ 96 | this License, each Contributor hereby grants to You a perpetual,\n\ 97 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n\ 98 | (except as stated in this section) patent license to make, have made,\n\ 99 | use, offer to sell, sell, import, and otherwise transfer the Work,\n\ 100 | where such license applies only to those patent claims licensable\n\ 101 | by such Contributor that are necessarily infringed by their\n\ 102 | Contribution(s) alone or by combination of their Contribution(s)\n\ 103 | with the Work to which such Contribution(s) was submitted. If You\n\ 104 | institute patent litigation against any entity (including a\n\ 105 | cross-claim or counterclaim in a lawsuit) alleging that the Work\n\ 106 | or a Contribution incorporated within the Work constitutes direct\n\ 107 | or contributory patent infringement, then any patent licenses\n\ 108 | granted to You under this License for that Work shall terminate\n\ 109 | as of the date such litigation is filed.\n\ 110 | \n\ 111 | 4. Redistribution. You may reproduce and distribute copies of the\n\ 112 | Work or Derivative Works thereof in any medium, with or without\n\ 113 | modifications, and in Source or Object form, provided that You\n\ 114 | meet the following conditions:\n\ 115 | \n\ 116 | (a) You must give any other recipients of the Work or\n\ 117 | Derivative Works a copy of this License; and\n\ 118 | \n\ 119 | (b) You must cause any modified files to carry prominent notices\n\ 120 | stating that You changed the files; and\n\ 121 | \n\ 122 | (c) You must retain, in the Source form of any Derivative Works\n\ 123 | that You distribute, all copyright, patent, trademark, and\n\ 124 | attribution notices from the Source form of the Work,\n\ 125 | excluding those notices that do not pertain to any part of\n\ 126 | the Derivative Works; and\n\ 127 | \n\ 128 | (d) If the Work includes a "NOTICE" text file as part of its\n\ 129 | distribution, then any Derivative Works that You distribute must\n\ 130 | include a readable copy of the attribution notices contained\n\ 131 | within such NOTICE file, excluding those notices that do not\n\ 132 | pertain to any part of the Derivative Works, in at least one\n\ 133 | of the following places: within a NOTICE text file distributed\n\ 134 | as part of the Derivative Works; within the Source form or\n\ 135 | documentation, if provided along with the Derivative Works; or,\n\ 136 | within a display generated by the Derivative Works, if and\n\ 137 | wherever such third-party notices normally appear. The contents\n\ 138 | of the NOTICE file are for informational purposes only and\n\ 139 | do not modify the License. You may add Your own attribution\n\ 140 | notices within Derivative Works that You distribute, alongside\n\ 141 | or as an addendum to the NOTICE text from the Work, provided\n\ 142 | that such additional attribution notices cannot be construed\n\ 143 | as modifying the License.\n\ 144 | \n\ 145 | You may add Your own copyright statement to Your modifications and\n\ 146 | may provide additional or different license terms and conditions\n\ 147 | for use, reproduction, or distribution of Your modifications, or\n\ 148 | for any such Derivative Works as a whole, provided Your use,\n\ 149 | reproduction, and distribution of the Work otherwise complies with\n\ 150 | the conditions stated in this License.\n\ 151 | \n\ 152 | 5. Submission of Contributions. Unless You explicitly state otherwise,\n\ 153 | any Contribution intentionally submitted for inclusion in the Work\n\ 154 | by You to the Licensor shall be under the terms and conditions of\n\ 155 | this License, without any additional terms or conditions.\n\ 156 | Notwithstanding the above, nothing herein shall supersede or modify\n\ 157 | the terms of any separate license agreement you may have executed\n\ 158 | with Licensor regarding such Contributions.\n\ 159 | \n\ 160 | 6. Trademarks. This License does not grant permission to use the trade\n\ 161 | names, trademarks, service marks, or product names of the Licensor,\n\ 162 | except as required for reasonable and customary use in describing the\n\ 163 | origin of the Work and reproducing the content of the NOTICE file.\n\ 164 | \n\ 165 | 7. Disclaimer of Warranty. Unless required by applicable law or\n\ 166 | agreed to in writing, Licensor provides the Work (and each\n\ 167 | Contributor provides its Contributions) on an "AS IS" BASIS,\n\ 168 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n\ 169 | implied, including, without limitation, any warranties or conditions\n\ 170 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n\ 171 | PARTICULAR PURPOSE. You are solely responsible for determining the\n\ 172 | appropriateness of using or redistributing the Work and assume any\n\ 173 | risks associated with Your exercise of permissions under this License.\n\ 174 | \n\ 175 | 8. Limitation of Liability. In no event and under no legal theory,\n\ 176 | whether in tort (including negligence), contract, or otherwise,\n\ 177 | unless required by applicable law (such as deliberate and grossly\n\ 178 | negligent acts) or agreed to in writing, shall any Contributor be\n\ 179 | liable to You for damages, including any direct, indirect, special,\n\ 180 | incidental, or consequential damages of any character arising as a\n\ 181 | result of this License or out of the use or inability to use the\n\ 182 | Work (including but not limited to damages for loss of goodwill,\n\ 183 | work stoppage, computer failure or malfunction, or any and all\n\ 184 | other commercial damages or losses), even if such Contributor\n\ 185 | has been advised of the possibility of such damages.\n\ 186 | \n\ 187 | 9. Accepting Warranty or Additional Liability. While redistributing\n\ 188 | the Work or Derivative Works thereof, You may choose to offer,\n\ 189 | and charge a fee for, acceptance of support, warranty, indemnity,\n\ 190 | or other liability obligations and/or rights consistent with this\n\ 191 | License. However, in accepting such obligations, You may act only\n\ 192 | on Your own behalf and on Your sole responsibility, not on behalf\n\ 193 | of any other Contributor, and only if You agree to indemnify,\n\ 194 | defend, and hold each Contributor harmless for any liability\n\ 195 | incurred by, or claims asserted against, such Contributor by reason\n\ 196 | of your accepting any such warranty or additional liability.\n\ 197 | \n\ 198 | END OF TERMS AND CONDITIONS\n\ 199 | -------------------------------------------------------------------------------- /components/src/main/java/org/wso2/carbon/messaging/CarbonMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 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.messaging; 20 | 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | import org.wso2.carbon.messaging.exceptions.MessagingException; 24 | import org.wso2.carbon.messaging.exceptions.NelException; 25 | 26 | import java.io.IOException; 27 | import java.io.InputStream; 28 | import java.io.OutputStream; 29 | import java.nio.ByteBuffer; 30 | import java.util.ArrayList; 31 | import java.util.HashMap; 32 | import java.util.List; 33 | import java.util.Map; 34 | import java.util.Stack; 35 | import java.util.concurrent.BlockingQueue; 36 | import java.util.concurrent.LinkedBlockingQueue; 37 | import java.util.concurrent.atomic.AtomicBoolean; 38 | import java.util.concurrent.locks.Lock; 39 | import java.util.concurrent.locks.ReentrantLock; 40 | import java.util.stream.Collectors; 41 | 42 | /** 43 | * Data carrier between the components. 44 | */ 45 | public abstract class CarbonMessage { 46 | 47 | private static final Logger LOG = LoggerFactory.getLogger(CarbonMessage.class); 48 | 49 | protected Headers headers = new Headers(); 50 | protected Map properties = new HashMap<>(); 51 | protected BlockingQueue messageBody = new LinkedBlockingQueue<>(); 52 | protected Stack faultHandlerStack = new Stack<>(); 53 | protected MessageDataSource messageDataSource; 54 | /** 55 | * @deprecated This field will be replace by {@link #messagingException} 56 | */ 57 | @Deprecated 58 | protected NelException nelException = null; 59 | 60 | /** 61 | * Exception related to fault CarbonMessage. 62 | */ 63 | protected MessagingException messagingException = null; 64 | 65 | protected ByteBufferInputStream byteBufferInputStream; 66 | 67 | private ByteBufferOutputStream byteBufferOutputStream; 68 | 69 | protected Lock lock = new ReentrantLock(); 70 | 71 | protected boolean bufferContent = true; 72 | 73 | protected AtomicBoolean alreadyRead = new AtomicBoolean(false); 74 | 75 | private AtomicBoolean endOfMsgAdded = new AtomicBoolean(false); 76 | 77 | private Writer writer; 78 | private boolean isMessageBodyAdded; 79 | 80 | public CarbonMessage() { 81 | } 82 | 83 | /** 84 | * This enable you to avoid filling content in to internal buffer. 85 | * Use this constructor when creating response message and need to write content and 86 | * 87 | * @param buffercontent enables direct writing to channel if true else buffer content in message queue 88 | */ 89 | public CarbonMessage(Boolean buffercontent) { 90 | this.bufferContent = buffercontent; 91 | } 92 | 93 | public void setBufferContent(boolean bufferContent) { 94 | if (isMessageBodyAdded) { 95 | throw new IllegalStateException( 96 | "CarbonMessage#setBufferContent cannot " + "be called after adding message body"); 97 | } 98 | this.bufferContent = bufferContent; 99 | } 100 | 101 | public boolean isEndOfMsgAdded() { 102 | return endOfMsgAdded.get(); 103 | } 104 | 105 | public boolean isEmpty() { 106 | return messageBody.isEmpty(); 107 | } 108 | 109 | public ByteBuffer getMessageBody() { 110 | try { 111 | return (ByteBuffer) messageBody.take(); 112 | } catch (InterruptedException e) { 113 | LOG.error("Error while retrieving chunk from queue.", e); 114 | return null; 115 | } 116 | 117 | } 118 | 119 | /** 120 | * Calling this method will be blocked until all the message content is received. 121 | * 122 | * @return Full message body as list of {@link ByteBuffer} 123 | */ 124 | public List getFullMessageBody() { 125 | List byteBufferList = new ArrayList<>(); 126 | 127 | while (true) { 128 | try { 129 | if (endOfMsgAdded.get() && messageBody.isEmpty()) { 130 | break; 131 | } 132 | byteBufferList.add((ByteBuffer) messageBody.take()); 133 | } catch (InterruptedException e) { 134 | LOG.error("Error while getting full message body", e); 135 | } 136 | } 137 | return byteBufferList; 138 | } 139 | 140 | public void addMessageBody(ByteBuffer msgBody) { 141 | isMessageBodyAdded = true; 142 | if (bufferContent) { 143 | messageBody.add(msgBody); 144 | } else { 145 | if (writer != null) { 146 | writer.write(msgBody); 147 | } else { 148 | LOG.error("Cannot write content no registered writer found"); 149 | } 150 | } 151 | } 152 | 153 | /** 154 | * Method to be used for resources clean up after using Carbon Messaging. 155 | */ 156 | public void release() { 157 | 158 | } 159 | 160 | public Headers getHeaders() { 161 | return headers; 162 | } 163 | 164 | public String getHeader(String key) { 165 | return headers.get(key); 166 | } 167 | 168 | public void setHeader(String key, String value) { 169 | headers.set(key, value); 170 | } 171 | 172 | public void setHeaders(Map headerMap) { 173 | headers.set(headerMap); 174 | } 175 | 176 | public void setHeaders(List
headerList) { 177 | headers.set(headerList); 178 | } 179 | 180 | public Object getProperty(String key) { 181 | if (properties != null) { 182 | return properties.get(key); 183 | } else { 184 | return null; 185 | } 186 | } 187 | 188 | public Map getProperties() { 189 | return properties; 190 | } 191 | 192 | public void setProperty(String key, Object value) { 193 | properties.put(key, value); 194 | } 195 | 196 | public void removeHeader(String key) { 197 | headers.remove(key); 198 | } 199 | 200 | public void removeProperty(String key) { 201 | properties.remove(key); 202 | } 203 | 204 | public Stack getFaultHandlerStack() { 205 | return faultHandlerStack; 206 | } 207 | 208 | public void setFaultHandlerStack(Stack faultHandlerStack) { 209 | this.faultHandlerStack = faultHandlerStack; 210 | } 211 | 212 | public Lock getLock() { 213 | return lock; 214 | } 215 | 216 | public int getFullMessageLength() { 217 | List fullMessageBody = getFullMessageBody(); 218 | int size = (int) fullMessageBody.stream().mapToInt(byteBuffer -> byteBuffer.limit()).sum(); 219 | fullMessageBody.forEach(byteBuffer -> addMessageBody(byteBuffer)); 220 | return size; 221 | } 222 | 223 | public List getCopyOfFullMessageBody() { 224 | List fullMessageBody = getFullMessageBody(); 225 | List newCopy = fullMessageBody.stream().map(byteBuffer -> MessageUtil.deepCopy(byteBuffer)) 226 | .collect(Collectors.toList()); 227 | fullMessageBody.forEach(byteBuffer -> addMessageBody(byteBuffer)); 228 | markMessageEnd(); 229 | return newCopy; 230 | } 231 | 232 | /** 233 | * This method is used to mark the end of the message when cloning the content. 234 | */ 235 | public void markMessageEnd() { 236 | } 237 | 238 | public void setEndOfMsgAdded(boolean endOfMsgAdded) { 239 | this.endOfMsgAdded.compareAndSet(false, endOfMsgAdded); 240 | if (byteBufferOutputStream != null) { 241 | try { 242 | this.byteBufferOutputStream.flush(); 243 | } catch (IOException e) { 244 | LOG.error("Exception occured while flushing the buffer", e); 245 | byteBufferOutputStream.close(); 246 | } 247 | } 248 | ; 249 | if (writer != null) { 250 | writer.writeLastContent(this); 251 | } 252 | } 253 | 254 | public Writer getWriter() { 255 | return writer; 256 | } 257 | 258 | public void setWriter(Writer writer) { 259 | this.writer = writer; 260 | } 261 | 262 | public boolean isBufferContent() { 263 | return bufferContent; 264 | } 265 | 266 | public MessageDataSource getMessageDataSource() { 267 | return messageDataSource; 268 | } 269 | 270 | public void setMessageDataSource(MessageDataSource messageDataSource) { 271 | this.messageDataSource = messageDataSource; 272 | } 273 | 274 | public boolean isAlreadyRead() { 275 | return alreadyRead.get(); 276 | } 277 | 278 | public void setAlreadyRead(boolean alreadyRead) { 279 | this.alreadyRead.set(alreadyRead); 280 | } 281 | 282 | /** 283 | * This is a blocking call and provides full message as inputStream 284 | * removes original content from queue. 285 | * 286 | * @return InputStream Instance. 287 | */ 288 | public InputStream getInputStream() { 289 | if (byteBufferInputStream == null) { 290 | byteBufferInputStream = new ByteBufferInputStream(); 291 | } 292 | return byteBufferInputStream; 293 | } 294 | 295 | /** 296 | * This provide access to write byte stream in to message content Queue as 297 | * Stream 298 | * 299 | * @return OutputStream Instance. 300 | */ 301 | public OutputStream getOutputStream() { 302 | if (byteBufferOutputStream == null) { 303 | byteBufferOutputStream = new ByteBufferOutputStream(); 304 | } 305 | return byteBufferOutputStream; 306 | } 307 | 308 | /** 309 | * A class which represents the InputStream of the ByteBuffers 310 | * No need to worry about thread safety of this class this is called only once by 311 | * for a message instance from one thread. 312 | */ 313 | protected class ByteBufferInputStream extends InputStream { 314 | 315 | private int count; 316 | private boolean chunkFinished = true; 317 | private int limit; 318 | private ByteBuffer byteBuffer; 319 | 320 | @Override 321 | public int read() throws IOException { 322 | setAlreadyRead(true); // TODO: No need to set this again and again 323 | if (isEndOfMsgAdded() && isEmpty() && chunkFinished) { 324 | return -1; 325 | } else if (chunkFinished) { 326 | byteBuffer = getMessageBody(); 327 | count = 0; 328 | limit = byteBuffer.limit(); 329 | if (limit == 0) { 330 | return -1; 331 | } 332 | chunkFinished = false; 333 | } 334 | count++; 335 | if (count == limit) { 336 | chunkFinished = true; 337 | } 338 | return byteBuffer.get() & 0xff; 339 | } 340 | } 341 | 342 | /** 343 | * A class which write byteStream into ByteBuffers and add those 344 | * ByteBuffers to Content Queue. 345 | * No need to worry about thread safety of this class this is called only once by 346 | * one thread at particular time. 347 | */ 348 | protected class ByteBufferOutputStream extends OutputStream { 349 | 350 | private ByteBuffer buffer; 351 | 352 | @Override 353 | public void write(int b) throws IOException { 354 | if (buffer == null) { 355 | buffer = BufferFactory.getInstance().getBuffer(); 356 | } 357 | if (buffer.hasRemaining()) { 358 | buffer.put((byte) b); 359 | } else { 360 | buffer.flip(); 361 | addMessageBody(buffer); 362 | buffer = BufferFactory.getInstance().getBuffer(); 363 | buffer.put((byte) b); 364 | } 365 | 366 | } 367 | 368 | @Override 369 | public void flush() throws IOException { 370 | if (buffer != null && buffer.position() > 0) { 371 | buffer.flip(); 372 | addMessageBody(buffer); 373 | buffer = BufferFactory.getInstance().getBuffer(); 374 | } 375 | } 376 | 377 | @Override 378 | public void close() { 379 | try { 380 | super.close(); 381 | } catch (IOException e) { 382 | LOG.error("Error while closing output stream but underlying resources are reset", e); 383 | } finally { 384 | byteBufferOutputStream = null; 385 | buffer = null; 386 | } 387 | 388 | } 389 | } 390 | 391 | /** 392 | * Get NelException 393 | * 394 | * @return NelException instance. 395 | * @deprecated Get NelException method will be replaced by {@link #getMessagingException()} method. 396 | */ 397 | @Deprecated 398 | public NelException getNelException() { 399 | return nelException; 400 | } 401 | 402 | /** 403 | * Set NelException. 404 | * 405 | * @param nelException NelException instance related to faulty CarbonMessage. 406 | * @deprecated Set NelException will be replaced by 407 | * {@link #setMessagingException(MessagingException carbonMessageException)} method. 408 | */ 409 | @Deprecated 410 | public void setNelException(NelException nelException) { 411 | this.nelException = nelException; 412 | } 413 | 414 | /** 415 | * Get CarbonMessageException 416 | * 417 | * @return CarbonMessageException instance related to faulty CarbonMessage. 418 | */ 419 | public MessagingException getMessagingException() { 420 | return messagingException; 421 | } 422 | 423 | /** 424 | * Set CarbonMessageException. 425 | * 426 | * @param messagingException exception related to faulty CarbonMessage. 427 | */ 428 | public void setMessagingException(MessagingException messagingException) { 429 | this.messagingException = messagingException; 430 | } 431 | 432 | public boolean isFaulty() { 433 | // TODO: Remove {@link #nelException} reference. 434 | return (this.messagingException != null || this.nelException != null); 435 | } 436 | } 437 | --------------------------------------------------------------------------------