Search code examples
javawso2complex-event-processingsiddhi

JAR for extends SIDDHI


I want extend siddhi, mi java code is:

package org.wso2.siddhi.extension.fraude;

import org.wso2.siddhi.core.config.ExecutionPlanContext;
import org.wso2.siddhi.core.executor.ExpressionExecutor;
import org.wso2.siddhi.core.executor.function.FunctionExecutor;
import org.wso2.siddhi.query.api.definition.Attribute;
import org.wso2.siddhi.query.api.definition.Attribute.Type;


import javax.json.Json;
import javax.json.JsonReader;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import java.io.StringReader;

public class Swordphish extends FunctionExecutor {
    ExpressionExecutor[] url;

    public static void main (String[] arg1){
        System.out.println("Programa test phishing");
    }

    @Override
    protected void init(ExpressionExecutor[] url, ExecutionPlanContext arg1) {
        // TODO Auto-generated method stub
        this.url    =   url;
    }

    @Override
    public Type getReturnType() {
        // TODO Auto-generated method stub
        return Attribute.Type.FLOAT;
    }

    @Override
    public void start() {
        // TODO Auto-generated method stub

    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub

    }

    @Override
    public Object[] currentState() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void restoreState(Object[] arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    protected Object execute(Object[] arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Object execute(Object arg0) {
        // TODO Auto-generated method stub
        float res = 0;
        String e = null;
        try {
            Client client = ClientBuilder.newClient();
            e = client.target("http://52.37.125.225:3000/phishing").queryParam("url", url).request(MediaType.TEXT_HTML)
                    .get(String.class);

            try (JsonReader jr = Json.createReader(new StringReader(e))) {
                String valor = jr.readObject().getString("result");
                try {
                    res = Float.parseFloat(valor);
                } catch (Exception ex1) {
                    res = 0;
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new Float(res);
    }

}

the siddhiext:

#
# Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
#
# WSO2 Inc. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

swordurl=org.wso2.siddhi.extension.fraude.Swordphish

I generated my JAR, and your location is C:\wso2\wso2das-3.0.1\repository\components\lib

I can use "swordurl" from siddhi:

from DSBStream
select fraude:swordurl('www.babas.com') as porcsword
insert into testswordphish;

But I run my executionplan, and I get the next error:

java.lang.NoClassDefFoundError: javax/ws/rs/client/ClientBuilder

this error always happens when I use external dependencies to Siddhi. WHY ?

in this case I use the next external dependencies:

import javax.json.Json;
import javax.json.JsonReader;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import java.io.StringReader;

Solution

  • You have to add all of your dependency JARs to <DAS_HOME>\repository\components\lib directory. I saw you are using JAX-RS library, You have to add that JAR too.