wex ajax,GitHub - IBM-Watson/wex-appbuilder-sample-proxy: Sample HTTP Proxy component useful for mak...

Note: A proxy is not required with WEX 11+. This sample code applies only for WEX versions before 11.0.

Application Builder Proxy

The Application Builder Proxy is a thin server side component that serves two important purposes.

The proxy is an abstraction between Application Builder widgets and web services that are external to Engine. Calling the proxy instead of directly calling an external web service from widgets promotes better maintainability. For example, when an external endpoint changes only the proxy must be updated rather than the URL references in individual widgets.

The proxy enables client-side interaction with external services via Ajax calls in the browser. Modern browser security prevents cross site Ajax requests due to the same-origin policy.

Watson Explorer Application Builder 11 and later includes a feature called Endpoints which serves this purpose. A proxy application like this one is not be needed if you are using version 11 of the software or later.

Installing the Sample Proxy

$AB_HOME refers to the home folder for the specific Application Builder installation. For example, on a default Windows installation, $AB_HOME might be C:\Program Files\IBM\WEX\AppBuilder

Create a new folder called 'proxy' in $AB_HOME/wlp/usr/servers/AppBuilder/apps

Once you've built the proxy WAR following the instructions below, unzip it into the newly created proxy folder. You should now have $AB_HOME/wlp/usr/servers/AppBuilder/apps/proxy/META-INF and $AB_HOME/wlp/usr/servers/AppBuilder/apps/proxy/WEB-INF.

Open server.xml, located at $AB_HOME/wlp/usr/servers/AppBuilder/ for editing. Add the following block to the server node.

Before using the Sample Proxy it is necessary to update the proxy configuration to do something interesting. For example, you might add functionality that points to custom Watson Developer Cloud applications.

Once you've modified the proxy to your liking, restart Application Builder's WebSphere according to the App Builder documentation - by restarting the Application Builder service using the normal commands for your installation.

Usage

The sample proxy includes a single endpoint out of the box for ping.

curl http://localhost:9080/proxy/ping

You can also point your browser at http://localhost:9080/proxy/ping. Change the port or hostname as appropriate specific to your sever.

Making reqests to the proxy from a widget can improve maintainability. In the case of Ajax interactions once the page has loaded, you'll need to use the proxy to avoid violating the same-origin request policy enforced by modern web browsers.

Here is an example for how to call the proxy from a Custom (ERB) widget in App Builder. This example code calls the Ping endpoint.

require 'net/http'

# Determine the endpoint for the proxy

# This assumes that the proxy is deployed

# to the same server as the current App Builder.

origin = URI.parse(request.original_url)

endpoint_builder = {

:host => origin.host,

:port => origin.port,

:scheme => origin.scheme,

:path => '/proxy/ping/'

}

url = URI::HTTP.build(endpoint_builder)

response = Net::HTTP.start(url.hostname, url.port) do |http|

req = Net::HTTP::Get.new(url.to_s)

http.request(req)

end

response = JSON.parse(response.body)

%>

You can also call the proxy from JavaScript in the client. This snippet shows how you can make the same request from a browser-side Ajax call using jQuery (included by default in App Builder). Be sure to to bind events to elements that exist in the DOM by using the standard jQuery binding methods (e.g. set your event bindings when the onLoad event is called or using delegates, etc.).

$.ajax({

type: "POST", // all methods use POST

url: "/proxy/ping/",

success: function(response) {

response = JSON.parse(response);

console.log(response);

},

failure: function(error) {

console.log(error);

}

});

Modifying the Proxy

The most common modification is to add new routes to the proxy. As long as no new ruby gems are required it is possible to modify the proxy directly. This can be done by updating proxy.rb located in $AB_HOME/wlp/usr/servers/AppBuilder/apps/proxy/WEB-INF/lib.

The proxy uses Ruby Sinatra to provide a REST style web service interface. Further information on Sinatra development is available on the web.

Once your changes are complete, restart Application Builder's WebSphere using the normal methods.

Bundling a Proxy WAR

Using the Proxy requires that you package it for use in a Java application server. This can be easily done once you're JRuby development envirionement is set up. Ruby was chosen for the ease of implementation (using frameworks such as Sinatra) and to allow for modifications to be made to a deployed proxy without requiring code to be recompiled in many cases.

The proxy assumes JRuby 1.7.18 for deployment but it might be possible to test the application using a native Ruby. The simplest approach is to use JRuby for development, testing, and deployment.

The following JRuby Gems are required to get started.

First install required gems.

$> bundle install

Now the proxy can be run as a Rack application for testing and development purposes.

$> rackup -p4567

At this point the proxy will be running at http://localhost:4567.

The WAR can be created using Warbler. If adding new gems be sure that the gems are installed under JRuby and the Gemfile is fully up to date. A rake task is available to Warble the proxy application. The rake task should be used so that specific JARs required for proper operation in WebSphere are copied to the correct path locations within the generated WAR. The rake task requires that unzip and zip are in your path. On Linux and Mac OS these tools should be available by default. On Windows it's simplest to run the rake task through Git Bash, MinGW, or Cygwin. Another alternative on Windows is to install the GNU Zip and Unzip utilities and use the standard CMD prompt or Powershell. It is assumed that the Java bin folder is on your path.

$> rake -f warble.rake

There could be minor variations in the JRuby file names from one version to another. If you run into problems running the rake task you may need to update the script based on the version of JRuby you are running. When we moved from 1.7.13 to 1.7.18 the file names were pretty obvious and the changes trivial. Pull requests are welcome.

Licensing

All sample code contained within this project repository or any subdirectories is licensed according to the terms of the MIT license, which can be viewed in the license file.

Open Source @ IBM

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习是一种人工智能(AI)的子领域,致力于研究如何利用数据和算法让计算机系统具备学习能力,从而能够自动地完成特定任务或者改进自身性能。机器学习的核心思想是让计算机系统通过学习数据中的模式和规律来实现目标,而不需要显式地编程。 机器学习应用非常广泛,包括但不限于以下领域: 图像识别和计算机视觉: 机器学习在图像识别、目标检测、人脸识别、图像分割等方面有着广泛的应用。例如,通过深度学习技术,可以训练神经网络来识别图像中的对象、人脸或者场景,用于智能监控、自动驾驶、医学影像分析等领域。 自然语言处理: 机器学习在自然语言处理领域有着重要的应用,包括文本分类、情感分析、机器翻译、语音识别等。例如,通过深度学习模型,可以训练神经网络来理解和生成自然语言,用于智能客服、智能助手、机器翻译等场景。 推荐系统: 推荐系统利用机器学习算法分析用户的行为和偏好,为用户推荐个性化的产品或服务。例如,电商网站可以利用机器学习算法分析用户的购买历史和浏览行为,向用户推荐感兴趣的商品。 预测和预测分析: 机器学习可以用于预测未来事件的发生概率或者趋势。例如,金融领域可以利用机器学习算法进行股票价格预测、信用评分、欺诈检测等。 医疗诊断和生物信息学: 机器学习在医疗诊断、药物研发、基因组学等领域有着重要的应用。例如,可以利用机器学习算法分析医学影像数据进行疾病诊断,或者利用机器学习算法分析基因数据进行疾病风险预测。 智能交通和物联网: 机器学习可以应用于智能交通系统、智能城市管理和物联网等领域。例如,可以利用机器学习算法分析交通数据优化交通流量,或者利用机器学习算法分析传感器数据监测设备状态。 以上仅是机器学习应用的一部分,随着机器学习技术的不断发展和应用场景的不断拓展,机器学习在各个领域都有着重要的应用价值,并且正在改变我们的生活和工作方式。
机器学习是一种人工智能(AI)的子领域,致力于研究如何利用数据和算法让计算机系统具备学习能力,从而能够自动地完成特定任务或者改进自身性能。机器学习的核心思想是让计算机系统通过学习数据中的模式和规律来实现目标,而不需要显式地编程。 机器学习应用非常广泛,包括但不限于以下领域: 图像识别和计算机视觉: 机器学习在图像识别、目标检测、人脸识别、图像分割等方面有着广泛的应用。例如,通过深度学习技术,可以训练神经网络来识别图像中的对象、人脸或者场景,用于智能监控、自动驾驶、医学影像分析等领域。 自然语言处理: 机器学习在自然语言处理领域有着重要的应用,包括文本分类、情感分析、机器翻译、语音识别等。例如,通过深度学习模型,可以训练神经网络来理解和生成自然语言,用于智能客服、智能助手、机器翻译等场景。 推荐系统: 推荐系统利用机器学习算法分析用户的行为和偏好,为用户推荐个性化的产品或服务。例如,电商网站可以利用机器学习算法分析用户的购买历史和浏览行为,向用户推荐感兴趣的商品。 预测和预测分析: 机器学习可以用于预测未来事件的发生概率或者趋势。例如,金融领域可以利用机器学习算法进行股票价格预测、信用评分、欺诈检测等。 医疗诊断和生物信息学: 机器学习在医疗诊断、药物研发、基因组学等领域有着重要的应用。例如,可以利用机器学习算法分析医学影像数据进行疾病诊断,或者利用机器学习算法分析基因数据进行疾病风险预测。 智能交通和物联网: 机器学习可以应用于智能交通系统、智能城市管理和物联网等领域。例如,可以利用机器学习算法分析交通数据优化交通流量,或者利用机器学习算法分析传感器数据监测设备状态。 以上仅是机器学习应用的一部分,随着机器学习技术的不断发展和应用场景的不断拓展,机器学习在各个领域都有着重要的应用价值,并且正在改变我们的生活和工作方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值