rop 入门(五)

前边几章已经对rop做了大概的讲解,本章节我们一起来搭建rop框架
1、首先我们创建一个maven web项目(这里就不详细介绍了)
2、在maven的配置文件pom.xml中引入rop 和 spring(rop依赖于spring)的相关依赖文件

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.bookegou</groupId>
            <artifactId>rop</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.unitils</groupId>
            <artifactId>unitils-core</artifactId>
            <version>${unitils.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.unitils</groupId>
            <artifactId>unitils-testng</artifactId>
            <version>${unitils.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.unitils</groupId>
            <artifactId>unitils-spring</artifactId>
            <version>${unitils.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>

3、修改spring的配置文件


    <description>Spring Configuration</description>
    <context:annotation-config />
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
    <!--扫描Spring Bean -->
    <context:component-scan base-package="com.lyl.api"/>

  <!-- 启动Rop框架 -->
    <rop:annotation-driven  id="router" session-manager="sampleSessionManager" app-secret-manager="appSecretManager" service-timeout-seconds="50" upload-file-max-size="500" upload-file-types="png,gif,jpg" core-pool-size="200" max-pool-size="500" queue-capacity="50" keep-alive-seconds="300" sign-enable="true"/>

 <bean id="sampleSessionManager" class="com.lyl.api.component.SampleSessionManager" /> 

 <!-- rop 签名判断拦截器 -->
 <bean id="appSecretManager" class="com.lyl.api.component.SimpleAppSecretManager"/>

4、web.xml配置详情

   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>

    <!-- <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> -->

    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEnCoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> -->
    <!--Rop框架请求捕获-->
    <servlet>
        <servlet-name>rop</servlet-name>
        <servlet-class>
            com.rop.RopServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>rop</servlet-name>
        <url-pattern>/router</url-pattern>
    </servlet-mapping>

SampleSessionManager.java类

package com.lyl.api.component;

import com.rop.session.Session;
import com.rop.session.SessionManager;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SampleSessionManager implements SessionManager{

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    private final Map<String, Session> sessionCache = new ConcurrentHashMap<String, Session>(128, 0.75f, 32);

    public void addSession(String sessionId, Session session) {
        sessionCache.put(sessionId, session);
    }

    public Session getSession(String sessionId) {
        return sessionCache.get(sessionId);
    }

    public void removeSession(String sessionId) {
        sessionCache.remove(sessionId);
    }
}

SimpleAppSecretManager.java

package com.lyl.api.component; 

import com.rop.security.AppSecretManager;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class SimpleAppSecretManager implements AppSecretManager {

    private static Map<String, String> appKeySecretMap = new HashMap<String, String>();

    //TODO
    static {
        //自用 这些应该从数据库查询出来 这里先写死了 用来和测试(接口调用)的时候传递的秘钥进行比对
        appKeySecretMap.put("100001", "a4160d00-b083-40f9-a749-07aef8781d52");
        //第三方
        //appKeySecretMap.put("200001", "f7140046-c46f-443d-a151-00e3b8bb5924");
    }


    public String getSecret(String appKey) {
        System.out.println("use SimpleAppSecretManager!");
        return appKeySecretMap.get(appKey);
    }

    public boolean isValidAppKey(String appKey) {
        return getSecret(appKey) != null;
// return true;
    }

}

服务方法

package com.lyl.api.component; 

import com.rop.security.AppSecretManager;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class SimpleAppSecretManager implements AppSecretManager {

    private static Map<String, String> appKeySecretMap = new HashMap<String, String>();

    //TODO
    static {
        //自用 这些应该从数据库查询出来 这里先写死了 用来和测试(接口调用)的时候传递的秘钥进行比对
        appKeySecretMap.put("100001", "a4160d00-b083-40f9-a749-07aef8781d52");
        //第三方
        //appKeySecretMap.put("200001", "f7140046-c46f-443d-a151-00e3b8bb5924");
    }


    public String getSecret(String appKey) {
        System.out.println("use SimpleAppSecretManager!");
        return appKeySecretMap.get(appKey);
    }

    public boolean isValidAppKey(String appKey) {
        return getSecret(appKey) != null;
// return true;
    }

}

测试方法

package com.lyl.api.test;


import org.junit.Test;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.lyl.api.support.utils.CopUtils;
import com.rop.MessageFormat;
import com.rop.client.DefaultRopClient;

public class TestTravelNotesAction {

     public static final String SERVER_URL = "http://127.0.0.1:8080/test-rop/router";
     public static final String APP_KEY = "100001";
     public static final String APP_SECRET = "a4160d00-b083-40f9-a749-07aef8781d52";
     private DefaultRopClient ropClient = new DefaultRopClient(SERVER_URL, APP_KEY, APP_SECRET, MessageFormat.json);

     @Test
     public void test_view_Travel(){
         MultiValueMap<String, String> paramValues = new LinkedMultiValueMap<String, String>();
            //系统级参数
            paramValues.add("method", "view-travel");
            paramValues.add("appKey", APP_KEY);
            paramValues.add("appSecret", APP_SECRET);
            paramValues.add("v", "1.0");
            paramValues.add("format", "json");
            String sign = CopUtils.sign(paramValues.toSingleValueMap(), APP_SECRET);
            paramValues.add("sign", sign);
            //业务参数  不参与签名
            paramValues.add("travelPage", "3");

            String buildGetUrl = CopUtils.buildGetUrl(paramValues.toSingleValueMap(), SERVER_URL);
            String responseContent = new RestTemplate().getForObject(buildGetUrl, String.class, paramValues);
            System.out.println(responseContent);
     }

}

TravelRequest.java

package com.lyl.api.request;

import com.rop.AbstractRopRequest;
import com.rop.annotation.IgnoreSign;

public class TravelRequest extends AbstractRopRequest{

    //用来标注该参数不需要进行签名
    @IgnoreSign
    private String travelPage;


    public String getTravelPage() {
        return travelPage;
    }

    public void setTravelPage(String travelPage) {
        this.travelPage = travelPage;
    }

}

Response.java

package com.lyl.api.response;

import java.io.Serializable;

import com.rop.security.MainError;

public class Response implements Serializable{

    /** * */
    private static final long serialVersionUID = 1L;

    private Result result=new Result();
    private Object data;


    public Response() {
        super();
    }

    public Response(MainError mainError) {
        this.result.setResult("0");
        this.result.setMessage(mainError.getMessage());
        this.result.setErrorCode(mainError.getCode());
    }

    public Response(Result result, Object data) {
        super();
        this.result = result;
        this.data = data;
    }

    public Result getResult() {
        return result;
    }
    public void setResult(Result result) {
        this.result = result;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }

    /** * 对服务名进行标准化处理:如book.upload转换为book-upload, * * @param method * @return */
    protected String transform(String method) {
        if(method != null){
            method = method.replace(".", "-");
            return method;
        }else{
            return "LACK_METHOD";
        }
    }

    public class Result implements Serializable{

        /** * */
        private static final long serialVersionUID = 1L;
        private String result = "0";
        private String message;
        //默认值
        private String errorCode="999";

        public String getResult() {
            return result;
        }
        public void setResult(String result) {
            this.result = result;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        public String getErrorCode() {
            return errorCode;
        }
        public void setErrorCode(String errorCode) {
            this.errorCode = errorCode;
        }


    }
}

demo源码下载地址:
这里写链接内容

你可能感兴趣的:(rop 入门(五))