Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ShopeX/prism-java

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

shopex Prism sdk (java version)

用途

实现shopex Prism 的Java版SDK供第三方使用

功能

  • 提供http API调用(GET/POST方式)
  • 连接Websocket,可以发布/消费/应答消息
  • 提供oauth认证

要求

JDK6或者更高版本

构建安装

使用maven构建

  • 下载安装websocket客户端依赖
git clone https://github.com/hashio/websocket-client.git
cd websocket-client
mvn clean install
  • 下载安装prism-java sdk
git clone https://github.com/ShopEx/prism-java.git
cd prism-java
mvn clean install
  • 在你的应用中添加prism-java sdk依赖到pom.xml
<dependency>
  <groupId>cn.shopex</groupId>
  <artifactId>prism-sdk</artifactId>
  <version>1.0</version>
</dependency>

用法

  • 创建PrismClient实例对象
String url = "http://dilbmtcv.apihub.cn/api";
String key = "buwb2lii";
String secret = "ucr72ygfutspqeuu6s36";
Boolean https_model = true;#可选

PrismClient prismClient = new PrismClient(url,key,secret);
或者
PrismClient prismClient = new PrismClient(url,key,secret,https_model);
  • 发起API POST请求
//api请求PATH路径
String methodPath = "/platform/notify/write";
Map<String,String> appParams = new HashMap<String, String>();
//添加API请求所需的应用级参数
appParams.put("data","hello world");
//发送POST请求
String apiResult = prismClient.doPost(methodPath, appParams);
System.out.println(apiResult);
  • 发起API GET请求
//api请求PATH路径
String methodPath = "/platform/notify/status";
Map<String,String> appParams = new HashMap<String, String>();
//发送GET请求
String apiResult = prismClient.doGet(methodPath,appParams);
System.out.println(apiResult);
  • websocket连接

设置WebSocket生命周期函数

prismClient.setPrismMsgHandler(new PrismMsgHandler() {
      //Websocket连接完成时触发调用
      @Override
      public void onOpen(WebSocket socket) {
        System.out.println("---> open");
      }
      //接受到Websocket服务端信息时触发调用
      @Override
      public void onMessage(WebSocket socket, PrismMsg prismMsg) {
        System.out.println("---> receive msg:"+prismMsg);
        if (prismMsg.getTag() == 1) {//这里只对第一条消息做ACK应答
          try {
            socket.send(prismClient.assembleAckData(prismMsg.getTag()));
            System.out.println("发送ACK完成:"+prismMsg.getTag());
          } catch (WebSocketException e) {
            e.printStackTrace();
          }
        }
      }
      //Websocket发生异常时触发调用
      @Override
      public void onError(WebSocket socket, WebSocketException e) {
        e.printStackTrace();
        System.out.println("---> error:"+e);
      }
      //Websocket连接关闭时触发调用
      @Override
      public void onClose(WebSocket socket) {
        System.out.println("---> close");
      }
});

建立websocket连接

prismClient.executeNotify();

发布消息

prismClient.publish("order.new","hello world");

开启队列消费

prismClient.consume();

应答消息

prismClient.ack(1);

详细使用请代码点击这里

  • oauth认证
prismClient.requireOauth(request,response);//启动oauth认证

我们以spring mvc为框架的web项目为例,下面的代码在用户登录时会启动oauth认证,SDK会检查session中是否存在session_id,如果不存在则跳转到oauth页面, 完成登录后会回跳回来。 完成登录后会将当前登录信息session_id设置到session中

@Controller
@RequestMapping("/user")
public class LoginController {

  @Resource
  private PrismClient prismClient;

  //测试oauth授权
  @RequestMapping("/login")
  public ModelAndView login(HttpServletRequest request,HttpServletResponse response) {
    ModelAndView view = new ModelAndView("/index");
    prismClient.requireOauth(request,response);
    return view;
  }
}

如果授权过期,可以调用refreshToken()方法来刷新授权令牌

详细代码可以在源码测试下查看,先进入Main启动web容器,然后浏览器打开登录页面localhost:8002/user/login 即可测试Oauth认证

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.