Check if null, payload element using XPATH

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="com.example.mdp.sequence" trace="disable"
    xmlns="http://ws.apache.org/ns/synapse">
    <iterate expression="//plans" continueParent="true" sequential="true">
        <target>
            <sequence>
                <iterate expression="//planModules">
                    <target>
                        <sequence>
                            <property expression="$body//*[local-name()='moduleName']/text()" name="moduleName" />
                            <log level="custom">
                                <property name="==========" expression="$ctx:moduleName" />
                            </log>
                            <filter xpath="boolean($ctx:moduleName)">
                                <then>
                                    <log level="custom">
                                        <property name="==========TRUE" value="============" />
                                    </log>
                                </then>
                                <else>
                                    <log level="custom">
                                        <property name="==========FALSE" value="============" />
                                    </log>
                                </else>
                            </filter>
                        </sequence>
                    </target>
                </iterate>

            </sequence>
        </target>
    </iterate>
</sequence>

SMART Clear DEP Space

------Compress files------
cd /var/gateway/log/
tar -czf req_res_logger_30.tar request-response-logger.log.2018-09-30

------Upload files------
aws s3 cp req_res_logger_28.tar s3://smart-log-archive/DEP1-gateway-logs/archive/
aws s3 cp req_res_logger_29.tar s3://smart-log-archive/DEP1-gateway-logs/archive/
aws s3 cp req_res_logger_30.tar s3://smart-log-archive/DEP1-gateway-logs/archive/

------List files------
aws s3 ls smart-log-archive/DEP1-gateway-logs/archive


How to Invoke MO SMS

1. Notify URL : https://mife.smart.com.kh:8243/smsmessaging/v1/ReceivedInfoNotification/241

2. Take 241

3. mysql> select * from subscriptions where mo_subscription_did=241;
+---------------------+-------------------------------------------------------------------+---------+--------------+-------------+------------------+------------------+-----------+
| mo_subscription_did | notifyurl                                                         | created | created_date | lastupdated | lastupdated_date | service_provider | is_active |
+---------------------+-------------------------------------------------------------------+---------+--------------+-------------+------------------+------------------+-----------+
|                 241 | http://vuclip.smart.com.kh/notifications/DeliveryInfoNotification | NULL    | NULL         | NULL        | NULL             | NULL             |      NULL |
+---------------------+-------------------------------------------------------------------+---------+--------------+-------------+------------------+------------------+-----------+

4. Use postman or Curl

http://vuclip.smart.com.kh/notifications/DeliveryInfoNotification

{

 "inboundSMSMessageNotification": {
  "callbackData": "smart_tune_pack_7774",
  "inboundSMSMessage": {
   "resourceURL": "https://mife.smart.com.kh:8243/smsmessaging/v1/ReceivedInfoNotification/838",
   "dateTime": "2018-09-07T10:52:00",
   "destinationAddress": "7774",
   "messageId": "test00001",
   "message": "test 100 by wso2telco",
   "senderAddress": "+855005747251"
  }
 }
}


Curl

curl -X POST http://vuclip.smart.com.kh/notifications/DeliveryInfoNotification -H 'authorization: Bearer b7d8f11d-d616-36a3-a7d8-4134919aa6a0' -H 'content-type: application/json' -d '{ "inboundSMSMessageNotification": { "callbackData": "smart_tune_pack_7774", "inboundSMSMessage": { "resourceURL": "https://mife.smart.com.kh:8243/smsmessaging/v1/ReceivedInfoNotification/838", "dateTime": "2018-09-07T10:52:00", "destinationAddress": "7774", "messageId": "test00001", "message": "test 100 by wso2telco", "senderAddress": "+855005747251" } } }'

List files bigger than filesize specified

find . -type f -size +100M

Split large text files

priyan@pc ~/Documents $ ls -l
total 1364056
-rw-r--r-- 1 priyan priyan 1287231363 Jul  8 21:53 wso2carbon.log
priyan@pc ~/Documents $ wc -l 
wso2carbon.log         
priyan@pc ~/Documents $ wc -l wso2carbon.log 
7312324 wso2carbon.log
priyan@pc ~/Documents $ split -731232 wso2carbon.log 
priyan@pc ~/Documents $ ls
wso2carbon.log  xaa  xab  xac  xad  xae  xaf  xag  xah  xai  xaj  xak

Curl with Response Time

curl -o /dev/null -s -w 'Connect: %{time_connect}\nStart Transfer: %{time_starttransfer}\nTotal: %{time_total}\n' -X POST https://fwbb.smart.com.kh/api/booking/availableDeliveryDate -H 'Content-Type: application/x-www-form-urlencoded' -d 'latitude=11.5400043808865&longitude=104.922801107168'


curl -o /dev/null -s -w 'Connect: %{time_connect}\nStart Transfer: %{time_starttransfer}\nTotal: %{time_total}\n'  https://www.google.com

use grep to search specific file types only

find -name '*.java' -exec grep regex {} \+

Update throttling without restarting API Manager


1. Invoke API Calls using the Access token (for keep cache)
2. Select API/Application subscription record from table & update to relevant throttle

mysql> update am_subscription set TIER_ID='Unlimited' where APPLICATION_ID='12' and API_ID=5;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from am_subscription where APPLICATION_ID='12' and API_ID=5;
+-----------------+-----------+--------+---------------+----------------+------------+-------------------+------------+---------------------+------------+---------------------+--------------------------------------+
| SUBSCRIPTION_ID | TIER_ID   | API_ID | LAST_ACCESSED | APPLICATION_ID | SUB_STATUS | SUBS_CREATE_STATE | CREATED_BY | CREATED_TIME        | UPDATED_BY | UPDATED_TIME        | UUID                                 |
+-----------------+-----------+--------+---------------+----------------+------------+-------------------+------------+---------------------+------------+---------------------+--------------------------------------+
|              12 | Unlimited |      5 | NULL          |             12 | UNBLOCKED  | SUBSCRIBE         | hubsp1     | 2018-05-16 11:34:21 | NULL       | 2018-05-16 11:34:21 | 8cc991d7-5bf8-45bb-be50-c1724ac7b944 |
+-----------------+-----------+--------+---------------+----------------+------------+-------------------+------------+---------------------+------------+---------------------+--------------------------------------+
1 row in set (0.00 sec)

mysql> update am_subscription set TIER_ID='Gold' where APPLICATION_ID='12' and API_ID=5;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from am_subscription where APPLICATION_ID='12' and API_ID=5;
+-----------------+---------+--------+---------------+----------------+------------+-------------------+------------+---------------------+------------+---------------------+--------------------------------------+
| SUBSCRIPTION_ID | TIER_ID | API_ID | LAST_ACCESSED | APPLICATION_ID | SUB_STATUS | SUBS_CREATE_STATE | CREATED_BY | CREATED_TIME        | UPDATED_BY | UPDATED_TIME        | UUID                                 |
+-----------------+---------+--------+---------------+----------------+------------+-------------------+------------+---------------------+------------+---------------------+--------------------------------------+
|              12 | Gold    |      5 | NULL          |             12 | UNBLOCKED  | SUBSCRIBE         | hubsp1     | 2018-05-16 11:34:21 | NULL       | 2018-05-16 11:34:21 | 8cc991d7-5bf8-45bb-be50-c1724ac7b944 |
+-----------------+---------+--------+---------------+----------------+------------+-------------------+------------+---------------------+------------+---------------------+--------------------------------------+
1 row in set (0.00 sec)

mysql> update am_subscription set TIER_ID='Unlimited' where APPLICATION_ID='12' and API_ID=5;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from am_subscription where APPLICATION_ID='12' and API_ID=5;
+-----------------+-----------+--------+---------------+----------------+------------+-------------------+------------+---------------------+------------+---------------------+--------------------------------------+
| SUBSCRIPTION_ID | TIER_ID   | API_ID | LAST_ACCESSED | APPLICATION_ID | SUB_STATUS | SUBS_CREATE_STATE | CREATED_BY | CREATED_TIME        | UPDATED_BY | UPDATED_TIME        | UUID                                 |
+-----------------+-----------+--------+---------------+----------------+------------+-------------------+------------+---------------------+------------+---------------------+--------------------------------------+
|              12 | Unlimited |      5 | NULL          |             12 | UNBLOCKED  | SUBSCRIBE         | hubsp1     | 2018-05-16 11:34:21 | NULL       | 2018-05-16 11:34:21 | 8cc991d7-5bf8-45bb-be50-c1724ac7b944 |
+-----------------+-----------+--------+---------------+----------------+------------+-------------------+------------+---------------------+------------+---------------------+--------------------------------------+
1 row in set (0.00 sec)

3. Using API Manager admin service (APIAuthenticationService) invoke invalidateCachedTokens as per below (+ basic auth)

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://service.security.handlers.gateway.apimgt.carbon.wso2.org">
   <soap:Header/>
   <soap:Body>
      <ser:invalidateCachedTokens>
         <!--Zero or more repetitions:-->
         <ser:accessTokens>cee59744-4e64-303a-aa45-5631e2c6953c</ser:accessTokens>
      </ser:invalidateCachedTokens>
   </soap:Body>
</soap:Envelope>

4. continue API invoking & confirm

curl -X POST http://54.193.104.15:8280/paym4-303a-aa45-5631e2c6953c' -H 'content-type: application/json' -d '{ "amountTransaction": { "clientCorrelator": "123456", "endUserId": "tel:+94777323222", "paymentAmount": { "chargingInformation": { "amount": "0.01", "code": "PRBT", "currency": "LKR", "description": "In game currency" }, "chargingMetaData": { "channel": "WEB", "onBehalfOf": "MIFE_TEST", "purchaseCategoryCode": "STEAM", "serviceID": "eab99ce6-62fd-43d1-8952-f2dffdb144ac", "taxAmount": "0" } }, "referenceCode": "715", "transactionOperationStatus": "Charged" } }'
{"fault":{"code":900800,"message":"Message throttled out","description":"You have exceeded your quota"}}

WSO2 Solr Index

  1. Delete the solar directory.
  2. Open registry.xml and change the following line as shown below.
    • <lastAccessTimeLocation>/_system/local/repository/components/org.wso2.carbon.registry/indexing/lastaccesstime-1</lastAccessTimeLocation>
  3. Now restart the server. The server will re-index all the files again (It may get some time to this process after restart).
  4. Also, make sure the Databases are properly configured. Specially Registry mounting related configurations.

Compress files one by one at once

//=================COMPRESS


bzip2 *


//=================DECOMPRESS


bzip2 -d filename.bz2

Check MYSQL slow queries

SELECT *

FROM INFORMATION_SCHEMA.PROCESSLIST

WHERE time > 1 and COMMAND <>'Sleep' limit 3000;

CURL if telnet not works !

curl -v telnet://127.0.0.1:22

Grep files in Linux with Regex

Grep using Regex

grep '2018-03-20 15:55.*ERROR' wso2is-5.0.0/repository/logs/wso2carbon.log

Grep & return line number

grep -n '17:01:39,266' *.c wso2is-5.0.0/repository/logs/wso2carbon.log

RESULT- wso2carbon.log:2776828

Grep & export selected lines range

sed -n '2776828,3015070 p' wso2is-5.0.0/repository/logs/wso2carbon.log  > wso2carbon.log_ERROR_LOG

How to delete files more than 3 days old

find . -mtime +3 -print -delete

Import Certificates & JKS with WSO2 Products

First do the below change in hosts file as follows

#127.0.0.1      localhost
127.0.1.1       priyan-pc

127.0.0.1       local.wso2telco.com
#10.10.12.59    local.wso2telco.com

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
~                                                                                                                                           

"/etc/hosts" 


openssl req -x509 -nodes -days 20 -newkey rsa:2048 -keyout wso2telco.key -out wso2telco.crt

priyan@priyan-pc ~/wso2carbon/wso2telcohub-3.0.3-SNAPSHOT/repository/resources/security $ openssl req -x509 -nodes -days 20 -newkey rsa:2048 -keyout wso2telco.key -out wso2telco.crt
Generating a 2048 bit RSA private key
........................................................................+++
................................................................+++
writing new private key to 'wso2telco.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:SL    
State or Province Name (full name) [Some-State]:WESTERN
Locality Name (eg, city) []:COLOMBO
Organization Name (eg, company) [Internet Widgits Pty Ltd]:WSO2TELCO
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:*.wso2telco.com
Email Address []:support@wso2telco.com


openssl pkcs12 -export -in wso2telco.crt -inkey wso2telco.key -name "wso2carbon" -out wso2telco.pfx
password : wso2carbon

keytool -importkeystore -srckeystore wso2telco.pfx -srcstoretype pkcs12 -destkeystore wso2carbon.jks -deststoretype JKS

keytool -export -alias wso2carbon -keystore wso2carbon.jks -file wso2telco.pem

keytool -import -alias wso2carbon -file wso2telco.pem -keystore client-truststore.jks
if alredy exsists::::::::::::[keytool error: java.lang.Exception: Certificate not imported, alias <wso2carbon> already exists]
    Solution : keytool -delete -alias wso2carbon -keystore client-truststore.jks -storepass wso2carbon

Fix springframework DispatcherServlet in Tomcat & Eclipse

How to Fix java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Tomcat and Eclipse

1) If you are using Maven and if spring-webmvc is not added as a dependency in your project's pom.xml then please add that.

2) The second step is to add Maven Dependency into deployment assembly. 
This is required to move all dependent JAR files into WEB-INF/lib directory of Java web application.

In order to add Maven Dependency into deployment assembly in Eclipse, follow below steps :

i) Right-click your project, select properties and choose Deployment Assembly.
ii) Now click Add and choose Java Build Path Entries and select Maven dependency theirs. 
If your project is not set for the automatic build then just refresh the project and build it.
Now you should be able to run your Spring MVC application in attached Tomcat from Eclipse IDE itself.

Copy Table with indexes

CREATE TABLE New_Table LIKE Old_Table; 
INSERT New_Table  SELECT * FROM Old_Table;

CSRF Attack WSO2ESB

ERROR
JavaLogger potential cross-site request forgery (CSRF) attack thwarted (user:<anonymous>, ip:127.0.0.1, method:POST, uri:/carbon/admin/login_action.jsp, error:required token is missing from the request)

Solution

Downgrade JDK to 1.8.0_144;
if you cannot proceed with the downgrade, so you can disable the compression in Tomcat 
repository/conf/tomcat/catalina-server.xml, 
switching compression to "off" 
instead of the default which is "on".

WSO2 Certificate Import & Export

Sample commands

The following two commands are examples if you are using the keystore and client-truststore of the Identity Server itself for the client. This is executed from the <IS_HOME>/repository/resources/security directory.

keytool -export -alias wso2carbon -file carbon_public2.crt -keystore wso2carbon.jks -storepass wso2carbon

keytool -import -trustcacerts -alias carbon -file carbon_public2.crt -keystore client-truststore.jks -storepass wso2carbon

See MYSQL DB CONSTRAINTS

USE INFORMATION_SCHEMA;

SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = "rs_dbApiMgt"  AND REFERENCED_COLUMN_NAME IS NOT NULL;

Database sizes in MB

DATABASE SIZES
SELECT table_schema "DB Name", 
   Round(Sum(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB" 
FROM   information_schema.tables 
GROUP  BY table_schema; 

TABLE SIZE
SELECT  table_name AS `Table`,  round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES WHERE table_schema = "rs_dbApiMgt" ;

MYSQL Create User & Grant Access

1st Install MySQL



$ sudo apt-get install mysql-server
$ sudo apt-get install mysql-client

CREATE USER 'ConfigDbUser1'@'localhost';

GRANT ALL ON qadbApimConfig.* TO 'ConfigDbUser1'@'localhost';

FLUSH PRIVILEGES;

MYSQL history of queries command

cat ~/.mysql_history

Linux grep & Replace Strings

find ./ -type f -exec sed -i 's/FIND_WORDS/REPLACE_WORDS/g' {} \;



grep -rl "SEARCH_WORDS" | xargs sed -i 's/SEARCH_WORDS/REPLACE_WORDS/g'

Tomcat enable Remote Debug

JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"

catalina.sh jpda start

Modify XML Payload & Update

public class XmlAlterMediator extends AbstractMediator {
    private Log log = LogFactory.getLog(this.getClass());
    private String removeEl;

public String getRemoveEl() {
return removeEl;
}

public void setRemoveEl(String removeEl) {
this.removeEl = removeEl;
}

Document doc ;
public boolean mediate(MessageContext context) {
String responsePayload=null;
try {
doc = getDocumentByXml(context.getEnvelope().toString());
String []elements=removeEl.split("_");
for (String element : elements) {
String []elementTmp=element.split("\\.");
String parentElement=elementTmp[0];
String childElement=elementTmp[1];
deleteElements(parentElement,childElement);
log.info(element);
}

responsePayload=xmlDocPrint(doc);

        SOAPBody body = context.getEnvelope().getBody();
        OMElement firstChild = body.getFirstElement();
        OMElement omXML = AXIOMUtil.stringToOM(responsePayload);
        if (firstChild == null) {
            body.addChild(omXML);
        } else {
            firstChild.insertSiblingAfter(omXML);
            firstChild.detach();
        }

} catch (ParserConfigurationException e) {
log.error(e.getMessage());
} catch (SAXException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
} catch (Exception e) {
log.error(e.getMessage());
}
return true;
}


private Document getDocumentByXml(String xml) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
Document doc = db.parse(is);
doc.getDocumentElement().normalize();
return doc;
}

private final String xmlDocPrint(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
log.info(out.toString());
return out.toString();
}

private Document deleteElements(String parentElement,String childElement) {
NodeList nodeList = doc.getElementsByTagName(childElement);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if ((node.getNodeType() == Node.ELEMENT_NODE)&&(node.getParentNode().getNodeName().equals(parentElement))) {
node.getParentNode().removeChild(node);
}
}
return doc;
}
}