Commit 5270c151f69b3a0decf1c3cb28df36d7c314327b
1 parent
d260b19ba0
Exists in
master
and in
1 other branch
permission2
Showing 6 changed files with 197 additions and 173 deletions
- platform-operate-api/src/main/java/com/lyms/hospitalapi/NIOServer.java
- platform-operate-api/src/main/java/com/lyms/hospitalapi/NioClient.java
- platform-operate-api/src/main/java/com/lyms/nio/NIOServer.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AccessPermissionFacade.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/request/AccessPermissionAddRequest.java
- platform-operate-api/src/main/resources/config.properties
platform-operate-api/src/main/java/com/lyms/hospitalapi/NIOServer.java
View file @
5270c15
| 1 | +package com.lyms.hospitalapi; | |
| 2 | + | |
| 3 | +import com.lyms.platform.common.utils.PropertiesUtils; | |
| 4 | +import org.springframework.stereotype.Service; | |
| 5 | + | |
| 6 | +import javax.annotation.PostConstruct; | |
| 7 | +import java.io.IOException; | |
| 8 | +import java.net.InetSocketAddress; | |
| 9 | +import java.nio.ByteBuffer; | |
| 10 | +import java.nio.channels.*; | |
| 11 | +import java.util.Iterator; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Created by riecard on 2016/11/17. | |
| 15 | + */ | |
| 16 | +@Service | |
| 17 | +public class NIOServer { | |
| 18 | + | |
| 19 | + public static final String NIO_SERVER_PORT = PropertiesUtils.getPropertyValue("nio_server_port"); | |
| 20 | + public static final String NIO_SERVER_START = PropertiesUtils.getPropertyValue("nio_server_start"); | |
| 21 | + | |
| 22 | + //通道管理器 | |
| 23 | + private Selector selector; | |
| 24 | + | |
| 25 | + @PostConstruct | |
| 26 | + public void init() throws IOException { | |
| 27 | + if ("1".equals(NIO_SERVER_START)) { | |
| 28 | + NIOServer server = new NIOServer(); | |
| 29 | + server.initServer(Integer.valueOf(NIO_SERVER_PORT)); | |
| 30 | + server.listen(); | |
| 31 | + } | |
| 32 | + } | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * 获得一个ServerSocket通道,并对该通道做一些初始化的工作 | |
| 36 | + * @param port 绑定的端口号 | |
| 37 | + * @throws IOException | |
| 38 | + */ | |
| 39 | + public void initServer(int port) throws IOException { | |
| 40 | + // 获得一个ServerSocket通道 | |
| 41 | + ServerSocketChannel serverChannel = ServerSocketChannel.open(); | |
| 42 | + // 设置通道为非阻塞 | |
| 43 | + serverChannel.configureBlocking(false); | |
| 44 | + // 将该通道对应的ServerSocket绑定到port端口 | |
| 45 | + serverChannel.socket().bind(new InetSocketAddress(port)); | |
| 46 | + // 获得一个通道管理器 | |
| 47 | + this.selector = Selector.open(); | |
| 48 | + //将通道管理器和该通道绑定,并为该通道注册SelectionKey.OP_ACCEPT事件,注册该事件后, | |
| 49 | + //当该事件到达时,selector.select()会返回,如果该事件没到达selector.select()会一直阻塞。 | |
| 50 | + serverChannel.register(selector, SelectionKey.OP_ACCEPT); | |
| 51 | + new BeatTask(selector).start(); | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * 采用轮询的方式监听selector上是否有需要处理的事件,如果有,则进行处理 | |
| 56 | + * @throws IOException | |
| 57 | + */ | |
| 58 | + @SuppressWarnings("unchecked") | |
| 59 | + public void listen() throws IOException { | |
| 60 | + System.out.println("服务端启动成功!"); | |
| 61 | + // 轮询访问selector | |
| 62 | + while (true) { | |
| 63 | + //当注册的事件到达时,方法返回;否则,该方法会一直阻塞 | |
| 64 | + selector.select(); | |
| 65 | + // 获得selector中选中的项的迭代器,选中的项为注册的事件 | |
| 66 | + Iterator ite = this.selector.selectedKeys().iterator(); | |
| 67 | + while (ite.hasNext()) { | |
| 68 | + SelectionKey key = (SelectionKey) ite.next(); | |
| 69 | + // 删除已选的key,以防重复处理 | |
| 70 | + ite.remove(); | |
| 71 | + // 客户端请求连接事件 | |
| 72 | + if (key.isAcceptable()) { | |
| 73 | + ServerSocketChannel server = (ServerSocketChannel) key | |
| 74 | + .channel(); | |
| 75 | + // 获得和客户端连接的通道 | |
| 76 | + SocketChannel channel = server.accept(); | |
| 77 | + // 设置成非阻塞 | |
| 78 | + channel.configureBlocking(false); | |
| 79 | + | |
| 80 | + //在这里可以给客户端发送信息哦 | |
| 81 | + channel.write(ByteBuffer.wrap(new String("向客户端发送了一条信息").getBytes())); | |
| 82 | + //在和客户端连接成功之后,为了可以接收到客户端的信息,需要给通道设置读的权限。 | |
| 83 | + channel.register(this.selector, SelectionKey.OP_READ); | |
| 84 | + | |
| 85 | + // 获得了可读的事件 | |
| 86 | + } else if (key.isReadable()) { | |
| 87 | + int length = read(key); | |
| 88 | + if (length == 0) { | |
| 89 | + key.channel().close(); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 93 | + } | |
| 94 | + | |
| 95 | + } | |
| 96 | + } | |
| 97 | + /** | |
| 98 | + * 处理读取客户端发来的信息 的事件 | |
| 99 | + * @param key | |
| 100 | + * @throws IOException | |
| 101 | + */ | |
| 102 | + public int read(SelectionKey key) throws IOException{ | |
| 103 | + // 服务器可读取消息:得到事件发生的Socket通道 | |
| 104 | + SocketChannel channel = (SocketChannel) key.channel(); | |
| 105 | + // 创建读取的缓冲区 | |
| 106 | + ByteBuffer buffer = ByteBuffer.allocate(10); | |
| 107 | + channel.read(buffer); | |
| 108 | + byte[] data = buffer.array(); | |
| 109 | + String msg = new String(data).trim(); | |
| 110 | + if (msg.length() == 0) { | |
| 111 | + return 0; | |
| 112 | + } | |
| 113 | + System.out.println("服务端收到信息:"+msg); | |
| 114 | + ByteBuffer outBuffer = ByteBuffer.wrap("success".getBytes()); | |
| 115 | + channel.write(outBuffer);// 将消息回送给客户端 | |
| 116 | + return msg.length(); | |
| 117 | + } | |
| 118 | + | |
| 119 | + class BeatTask extends Thread { | |
| 120 | + | |
| 121 | + private Selector selector; | |
| 122 | + | |
| 123 | + public BeatTask(Selector selector) { | |
| 124 | + this.selector = selector; | |
| 125 | + } | |
| 126 | + | |
| 127 | + @Override | |
| 128 | + public void run() { | |
| 129 | + while (true) { | |
| 130 | + for (SelectionKey key:selector.keys()) { | |
| 131 | + System.out.println(key); | |
| 132 | + Channel channel = key.channel(); | |
| 133 | + if (channel instanceof SocketChannel) { | |
| 134 | + ByteBuffer outBuffer = ByteBuffer.wrap("heartbeat".getBytes()); | |
| 135 | + try { | |
| 136 | + ((SocketChannel)channel).write(outBuffer);// 将消息回送给客户端 | |
| 137 | + } catch (IOException e) { | |
| 138 | + e.printStackTrace(); | |
| 139 | + } | |
| 140 | + } | |
| 141 | + } | |
| 142 | + try { | |
| 143 | + sleep(3000L); | |
| 144 | + } catch (InterruptedException e) { | |
| 145 | + e.printStackTrace(); | |
| 146 | + } | |
| 147 | + } | |
| 148 | + } | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * 启动服务端测试 | |
| 153 | + * @throws IOException | |
| 154 | + */ | |
| 155 | + public static void main(String[] args) throws IOException { | |
| 156 | + NIOServer server = new NIOServer(); | |
| 157 | + server.initServer(8000); | |
| 158 | + server.listen(); | |
| 159 | + } | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | +} |
platform-operate-api/src/main/java/com/lyms/hospitalapi/NioClient.java
View file @
5270c15
| 1 | +package com.lyms.hospitalapi; | |
| 2 | + | |
| 3 | +import com.lyms.platform.common.utils.PropertiesUtils; | |
| 4 | +import org.springframework.stereotype.Service; | |
| 5 | + | |
| 6 | +import javax.annotation.PostConstruct; | |
| 7 | +import java.io.IOException; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Created by riecard on 2016/11/24. | |
| 11 | + */ | |
| 12 | +@Service | |
| 13 | +public class NioClient { | |
| 14 | + | |
| 15 | + public static final String NIO_CLIENT_START = PropertiesUtils.getPropertyValue("nio_client_start"); | |
| 16 | + | |
| 17 | + @PostConstruct | |
| 18 | + public void init() throws IOException { | |
| 19 | + if ("1".equals(NIO_CLIENT_START)) { | |
| 20 | + } | |
| 21 | + } | |
| 22 | +} |
platform-operate-api/src/main/java/com/lyms/nio/NIOServer.java
View file @
5270c15
| 1 | -package com.lyms.nio; | |
| 2 | - | |
| 3 | -import java.io.IOException; | |
| 4 | -import java.net.InetSocketAddress; | |
| 5 | -import java.nio.ByteBuffer; | |
| 6 | -import java.nio.channels.*; | |
| 7 | -import java.util.Iterator; | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * Created by riecard on 2016/11/17. | |
| 11 | - */ | |
| 12 | -public class NIOServer { | |
| 13 | - //通道管理器 | |
| 14 | - private Selector selector; | |
| 15 | - | |
| 16 | - /** | |
| 17 | - * 获得一个ServerSocket通道,并对该通道做一些初始化的工作 | |
| 18 | - * @param port 绑定的端口号 | |
| 19 | - * @throws IOException | |
| 20 | - */ | |
| 21 | - public void initServer(int port) throws IOException { | |
| 22 | - // 获得一个ServerSocket通道 | |
| 23 | - ServerSocketChannel serverChannel = ServerSocketChannel.open(); | |
| 24 | - // 设置通道为非阻塞 | |
| 25 | - serverChannel.configureBlocking(false); | |
| 26 | - // 将该通道对应的ServerSocket绑定到port端口 | |
| 27 | - serverChannel.socket().bind(new InetSocketAddress(port)); | |
| 28 | - // 获得一个通道管理器 | |
| 29 | - this.selector = Selector.open(); | |
| 30 | - //将通道管理器和该通道绑定,并为该通道注册SelectionKey.OP_ACCEPT事件,注册该事件后, | |
| 31 | - //当该事件到达时,selector.select()会返回,如果该事件没到达selector.select()会一直阻塞。 | |
| 32 | - serverChannel.register(selector, SelectionKey.OP_ACCEPT); | |
| 33 | - new BeatTask(selector).start(); | |
| 34 | - } | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * 采用轮询的方式监听selector上是否有需要处理的事件,如果有,则进行处理 | |
| 38 | - * @throws IOException | |
| 39 | - */ | |
| 40 | - @SuppressWarnings("unchecked") | |
| 41 | - public void listen() throws IOException { | |
| 42 | - System.out.println("服务端启动成功!"); | |
| 43 | - // 轮询访问selector | |
| 44 | - while (true) { | |
| 45 | - //当注册的事件到达时,方法返回;否则,该方法会一直阻塞 | |
| 46 | - selector.select(); | |
| 47 | - // 获得selector中选中的项的迭代器,选中的项为注册的事件 | |
| 48 | - Iterator ite = this.selector.selectedKeys().iterator(); | |
| 49 | - while (ite.hasNext()) { | |
| 50 | - SelectionKey key = (SelectionKey) ite.next(); | |
| 51 | - // 删除已选的key,以防重复处理 | |
| 52 | - ite.remove(); | |
| 53 | - // 客户端请求连接事件 | |
| 54 | - if (key.isAcceptable()) { | |
| 55 | - ServerSocketChannel server = (ServerSocketChannel) key | |
| 56 | - .channel(); | |
| 57 | - // 获得和客户端连接的通道 | |
| 58 | - SocketChannel channel = server.accept(); | |
| 59 | - // 设置成非阻塞 | |
| 60 | - channel.configureBlocking(false); | |
| 61 | - | |
| 62 | - //在这里可以给客户端发送信息哦 | |
| 63 | - channel.write(ByteBuffer.wrap(new String("向客户端发送了一条信息").getBytes())); | |
| 64 | - //在和客户端连接成功之后,为了可以接收到客户端的信息,需要给通道设置读的权限。 | |
| 65 | - channel.register(this.selector, SelectionKey.OP_READ); | |
| 66 | - | |
| 67 | - // 获得了可读的事件 | |
| 68 | - } else if (key.isReadable()) { | |
| 69 | - int length = read(key); | |
| 70 | - if (length == 0) { | |
| 71 | - key.channel().close(); | |
| 72 | - } | |
| 73 | - } | |
| 74 | - | |
| 75 | - } | |
| 76 | - | |
| 77 | - } | |
| 78 | - } | |
| 79 | - /** | |
| 80 | - * 处理读取客户端发来的信息 的事件 | |
| 81 | - * @param key | |
| 82 | - * @throws IOException | |
| 83 | - */ | |
| 84 | - public int read(SelectionKey key) throws IOException{ | |
| 85 | - // 服务器可读取消息:得到事件发生的Socket通道 | |
| 86 | - SocketChannel channel = (SocketChannel) key.channel(); | |
| 87 | - // 创建读取的缓冲区 | |
| 88 | - ByteBuffer buffer = ByteBuffer.allocate(10); | |
| 89 | - channel.read(buffer); | |
| 90 | - byte[] data = buffer.array(); | |
| 91 | - String msg = new String(data).trim(); | |
| 92 | - if (msg.length() == 0) { | |
| 93 | - return 0; | |
| 94 | - } | |
| 95 | - System.out.println("服务端收到信息:"+msg); | |
| 96 | - ByteBuffer outBuffer = ByteBuffer.wrap(msg.getBytes()); | |
| 97 | - channel.write(outBuffer);// 将消息回送给客户端 | |
| 98 | - return msg.length(); | |
| 99 | - } | |
| 100 | - | |
| 101 | - class BeatTask extends Thread { | |
| 102 | - | |
| 103 | - private Selector selector; | |
| 104 | - | |
| 105 | - public BeatTask(Selector selector) { | |
| 106 | - this.selector = selector; | |
| 107 | - } | |
| 108 | - | |
| 109 | - @Override | |
| 110 | - public void run() { | |
| 111 | - while (true) { | |
| 112 | - for (SelectionKey key:selector.keys()) { | |
| 113 | - System.out.println(key); | |
| 114 | - Channel channel = key.channel(); | |
| 115 | - if (channel instanceof SocketChannel) { | |
| 116 | - ByteBuffer outBuffer = ByteBuffer.wrap("heartbeat".getBytes()); | |
| 117 | - try { | |
| 118 | - ((SocketChannel)channel).write(outBuffer);// 将消息回送给客户端 | |
| 119 | - } catch (IOException e) { | |
| 120 | - e.printStackTrace(); | |
| 121 | - } | |
| 122 | - } | |
| 123 | - } | |
| 124 | - try { | |
| 125 | - sleep(3000L); | |
| 126 | - } catch (InterruptedException e) { | |
| 127 | - e.printStackTrace(); | |
| 128 | - } | |
| 129 | - } | |
| 130 | - } | |
| 131 | - } | |
| 132 | - | |
| 133 | - /** | |
| 134 | - * 启动服务端测试 | |
| 135 | - * @throws IOException | |
| 136 | - */ | |
| 137 | - public static void main(String[] args) throws IOException { | |
| 138 | - NIOServer server = new NIOServer(); | |
| 139 | - server.initServer(8000); | |
| 140 | - server.listen(); | |
| 141 | - } | |
| 142 | - | |
| 143 | - | |
| 144 | - | |
| 145 | -} |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/AccessPermissionFacade.java
View file @
5270c15
| ... | ... | @@ -65,34 +65,12 @@ |
| 65 | 65 | }else{ |
| 66 | 66 | dataPermissionsModel=new DataPermissionsModel(); |
| 67 | 67 | } |
| 68 | - | |
| 69 | - //如果院内权限 | |
| 70 | - if(PermissionTypeEnums.Type.USER.getId()==NumberUtils.toInt(accessPermissionAddRequest.getType())){ | |
| 71 | - //根据角色id拿对应的访问权限 | |
| 72 | - Roles roles = rolesService.getRoles(Integer.valueOf(accessPermissionAddRequest.getBizId())); | |
| 73 | - //角色没有绑定访问权限的情况 | |
| 74 | - if(null==roles.getPermissiontype()){ | |
| 75 | - return new BaseResponse().setErrorcode(ErrorCodeConstants.BUSINESS_ERROR).setErrormsg("角色没有绑定权限."); | |
| 76 | - } | |
| 77 | - | |
| 78 | - PermissionTypeEnums permissionTypeEnums = PermissionTypeEnums.getEnumsById(roles.getPermissiontype()); | |
| 79 | - | |
| 80 | - Assert.notNull(permissionTypeEnums, "权限类型不存在."); | |
| 81 | - if(CollectionUtils.isNotEmpty(accessPermissionAddRequest.getDeptId())){ | |
| 82 | - permission.setDeptid(accessPermissionAddRequest.getDeptId()); | |
| 83 | - } | |
| 84 | - permission.setType(permissionTypeEnums.getId()); | |
| 68 | + permission.setType(PermissionTypeEnums.ALL_HOSPITAL.getId()); | |
| 69 | + if(StringUtils.isNotEmpty(accessPermissionAddRequest.getBizId())){ | |
| 85 | 70 | dataPermissionsModel.getData().clear(); |
| 86 | - dataPermissionsModel.addOnePer(roles.getHospitalid(), permission); | |
| 87 | - }else if(PermissionTypeEnums.Type.ADMIN.getId()==NumberUtils.toInt(accessPermissionAddRequest.getType())){ | |
| 88 | - //如果是院外的权限,就只需要设置医院的id | |
| 89 | - permission.setType(PermissionTypeEnums.ALL_HOSPITAL.getId()); | |
| 90 | - if(StringUtils.isNotEmpty(accessPermissionAddRequest.getBizId())){ | |
| 91 | - dataPermissionsModel.getData().clear(); | |
| 92 | - String[] hospitalIds= accessPermissionAddRequest.getBizId().split(","); | |
| 93 | - for(String id:hospitalIds){ | |
| 94 | - dataPermissionsModel.addOnePer(id, permission); | |
| 95 | - } | |
| 71 | + String[] hospitalIds= accessPermissionAddRequest.getBizId().split(","); | |
| 72 | + for(String id:hospitalIds){ | |
| 73 | + dataPermissionsModel.addOnePer(id, permission); | |
| 96 | 74 | } |
| 97 | 75 | } |
| 98 | 76 | dataPermissionsModel.setAreaPermission(accessPermissionAddRequest.getAreaPermission()); |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/request/AccessPermissionAddRequest.java
View file @
5270c15
platform-operate-api/src/main/resources/config.properties
View file @
5270c15