jiapeng 8 years ago
parent
commit
e44c36a402

+ 3 - 1
src/main/java/com/ekexiu/console/system/dao/UserDao.java

@ -4,6 +4,7 @@ import java.sql.Connection;
4 4
import java.sql.SQLException;
5 5

6 6
import org.jfw.apt.annotation.Nullable;
7
import org.jfw.apt.orm.annotation.dao.Batch;
7 8
import org.jfw.apt.orm.annotation.dao.DAO;
8 9
import org.jfw.apt.orm.annotation.dao.Dynamic;
9 10
import org.jfw.apt.orm.annotation.dao.method.From;
@ -44,10 +45,11 @@ public interface UserDao {
44 45
	@Dynamic
45 46
	int update(Connection con, User user) throws SQLException;
46 47

48
	@Batch
47 49
	@IncludeFixSet("modifyTime")
48 50
	@UpdateWith
49 51
	@From(User.class)
50
	int resetPassword(Connection con, String id, @Set String loginPassword) throws SQLException;
52
	int[] resetPassword(Connection con,@Batch  String[] id, @Set String loginPassword) throws SQLException;
51 53

52 54
	@IncludeFixSet("modifyTime")
53 55
	@UpdateWith

+ 8 - 0
src/main/java/com/ekexiu/console/system/service/OrgService.java

@ -7,6 +7,7 @@ import org.jfw.apt.annotation.Autowrie;
7 7
import org.jfw.apt.annotation.DefaultValue;
8 8
import org.jfw.apt.annotation.Nullable;
9 9
import org.jfw.apt.web.annotation.Path;
10
import org.jfw.apt.web.annotation.operate.Delete;
10 11
import org.jfw.apt.web.annotation.operate.Get;
11 12
import org.jfw.apt.web.annotation.operate.Post;
12 13
import org.jfw.apt.web.annotation.operate.Put;
@ -61,6 +62,13 @@ public class OrgService {
61 62
	public Organization query(@JdbcConn Connection con,@PathVar String id)throws SQLException{
62 63
		return this.orgDao.query(con, id);
63 64
	}
65
	@Delete
66
	@Path("/id/{id}")
67
	public void deleteOne(@JdbcConn(true) Connection con,@PathVar String id)throws SQLException{
68
		this.orgDao.delete(con, id);
69
	}
70
	
71
	
64 72
	@Post
65 73
	@Path("/del")
66 74
	public void delete(@JdbcConn(true) Connection con,String[] ids)throws SQLException{

+ 1 - 1
src/main/java/com/ekexiu/console/system/service/RightService.java

@ -12,8 +12,8 @@ import java.util.concurrent.atomic.AtomicReference;
12 12
import javax.sql.DataSource;
13 13

14 14
import org.jfw.apt.annotation.Autowrie;
15
import org.jfw.apt.orm.annotation.dao.method.operator.Delete;
16 15
import org.jfw.apt.web.annotation.Path;
16
import org.jfw.apt.web.annotation.operate.Delete;
17 17
import org.jfw.apt.web.annotation.operate.Get;
18 18
import org.jfw.apt.web.annotation.operate.Post;
19 19
import org.jfw.apt.web.annotation.operate.Put;

+ 56 - 0
src/main/java/com/ekexiu/console/system/service/TestService.java

@ -0,0 +1,56 @@
1
package com.ekexiu.console.system.service;
2

3
import java.sql.Connection;
4
import java.sql.PreparedStatement;
5
import java.sql.ResultSet;
6
import java.sql.SQLException;
7
import java.util.ArrayList;
8
import java.util.List;
9

10
import javax.sql.DataSource;
11

12
import org.jfw.apt.annotation.Autowrie;
13
import org.jfw.apt.web.annotation.Path;
14
import org.jfw.apt.web.annotation.operate.Get;
15

16
@Path("/test")
17
public class TestService {
18

19
	@Autowrie("dataSource2")
20
	private DataSource ds;
21

22
	public DataSource getDs() {
23
		return ds;
24
	}
25

26
	public void setDs(DataSource ds) {
27
		this.ds = ds;
28
	}
29

30
	@Get
31
	@Path
32
	public Object get() throws SQLException {
33
		Connection con = ds.getConnection();
34
		try {
35

36
			PreparedStatement ps = con.prepareStatement("SELECT ARTICLE_TITLE FROM ARTICLE");
37
			try {
38
				ResultSet rs = ps.executeQuery();
39
				try {
40
					List<String> list = new ArrayList<String>();
41
					while (rs.next()) {
42
						list.add(rs.getString(1));
43
					}
44
					return list;
45
				} finally {
46
					rs.close();
47
				}
48
			} finally {
49
				ps.close();
50
			}
51

52
		} finally {
53
			con.close();
54
		}
55
	}
56
}

+ 16 - 0
src/main/java/com/ekexiu/console/system/service/UserService.java

@ -133,8 +133,24 @@ public class UserService extends Upload {
133 133
	@Put
134 134
	@Path
135 135
	public void update(@JdbcConn(true) Connection con,@RequestBody User user)throws SQLException{
136
		user.setLoginPassword(null);
136 137
		this.userDao.update(con, user);
137 138
	}
139
	
140
	@Get
141
	@Path("/info")
142
	public User editInfo(@JdbcConn Connection con,@LoginUser ConsoleAuthUser cau)throws SQLException{
143
		User user = this.userDao.queryById(con, cau.getId());
144
		if(user!=null){
145
			user.setLoginPassword(null);
146
		}
147
		return user;
148
	}
149
	@Post
150
	@Path("/resetpw")
151
	public void resetpw(@JdbcConn(true) Connection con,String[] ids)throws SQLException{
152
		this.userDao.resetPassword(con, ids,DEFAULT_PW_STR);
153
	}
138 154

139 155

140 156
	@Post

+ 13 - 0
src/main/java/com/ekexiu/console/util/HttpService.java

@ -0,0 +1,13 @@
1
package com.ekexiu.console.util;
2

3
import java.io.IOException;
4
import java.lang.reflect.Type;
5

6
import org.jfw.util.exception.JfwBaseException;
7

8
public interface HttpService {
9
	<T> T get(String url,Type typeOfT) throws JfwBaseException,IOException;
10
	<T> T post(String url, String data,Type typeOfT) throws JfwBaseException,IOException;
11
	<T> T put(String url, Object data,Type typeOfT) throws JfwBaseException,IOException;
12
	<T> T delete(String url,Type typeOfT) throws JfwBaseException,IOException;
13
}

+ 215 - 0
src/main/java/com/ekexiu/console/util/HttpServiceImpl.java

@ -0,0 +1,215 @@
1
package com.ekexiu.console.util;
2

3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.InputStreamReader;
6
import java.io.OutputStream;
7
import java.io.StringWriter;
8
import java.lang.reflect.ParameterizedType;
9
import java.lang.reflect.Type;
10
import java.net.HttpURLConnection;
11
import java.net.URL;
12

13
import org.jfw.util.ConstData;
14
import org.jfw.util.exception.JfwBaseException;
15
import org.jfw.util.io.IoUtil;
16
import org.jfw.util.json.JsonService;
17
import org.jfw.util.reflect.TypeReference;
18

19
public class HttpServiceImpl implements HttpService {
20
	private static final byte[] SIMPLE_POST_DATA = "1=1".getBytes(ConstData.UTF8);
21

22
	private String charset = "UTF-8";
23

24
	public Type buildResultType(final Type t) {
25
		return new ParameterizedType() {
26

27
			@Override
28
			public Type getRawType() {
29
				return Result.class;
30
			}
31

32
			@Override
33
			public Type getOwnerType() {
34
				return HttpServiceImpl.class;
35
			}
36

37
			@Override
38
			public Type[] getActualTypeArguments() {
39
				return new Type[] { t };
40
			}
41
		};
42
	}
43

44
	private void raise(Result<?> ret) throws JfwBaseException {
45
		throw new JfwBaseException(ret.getCode().intValue(), "remote calll error:" + ret.getMsg() + ";\r\n" + ret.getDetailMsg());
46
	}
47

48
	private <T> T handResponse(InputStream in, Type typeOfT) throws JfwBaseException {
49
		if (null != typeOfT) {
50
			Result<T> ret = JsonService.fromJson(new InputStreamReader(in, ConstData.UTF8), buildResultType(typeOfT));
51
			if (ret.isSuccess()) {
52
				return ret.getData();
53
			} else {
54
				raise(ret);
55
			}
56

57
		} else {
58
			Result<String> ret = JsonService.fromJson(new InputStreamReader(in, ConstData.UTF8), new TypeReference<Result<String>>() {
59
			}.getType());
60
			if (ret.isSuccess())
61
				return null;
62
			else {
63
				raise(ret);
64
			}
65
		}
66
		return null;
67
	}
68

69
	@Override
70
	public <T> T get(String url, Type typeOfT) throws JfwBaseException, IOException {
71
		URL u = new URL(url);
72
		HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
73
		httpURLConnection.setRequestProperty("Accept-Charset", charset);
74
		httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
75
		if (httpURLConnection.getResponseCode() != 200) {
76
			throw new IOException("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
77
		}
78
		InputStream in = httpURLConnection.getInputStream();
79
		try {
80
			return this.handResponse(in, typeOfT);
81
		} finally {
82
			IoUtil.close(in);
83
		}
84
	}
85

86
	@Override
87
	public <T> T post(String url, String data, Type typeOfT) throws JfwBaseException, IOException {
88
		byte[] bs = SIMPLE_POST_DATA;
89
		if (data != null && data.length() > 0) {
90
			bs = data.getBytes(ConstData.UTF8);
91
		}
92
		URL u = new URL(url);
93
		HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
94
		httpURLConnection.setDoOutput(true);
95
		httpURLConnection.setRequestMethod("POST");
96
		httpURLConnection.setRequestProperty("Accept-Charset", charset);
97
		httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
98
		httpURLConnection.setRequestProperty("Content-Length", String.valueOf(bs.length));
99
		OutputStream outputStream = httpURLConnection.getOutputStream();
100
		try {
101
			outputStream.write(bs);
102
			outputStream.flush();
103
		} finally {
104
			IoUtil.close(outputStream);
105
		}
106
		if (httpURLConnection.getResponseCode() != 200) {
107
			throw new IOException("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
108
		}
109
		InputStream in = httpURLConnection.getInputStream();
110
		try {
111
			return this.handResponse(in, typeOfT);
112
		} finally {
113
			IoUtil.close(in);
114
		}
115
	}
116

117
	@Override
118
	public <T> T put(String url, Object data, Type typeOfT) throws JfwBaseException, IOException {
119
		StringWriter sw = new StringWriter();
120

121
		JsonService.toJson(data, sw);
122
		byte[] bs = sw.toString().getBytes(ConstData.UTF8);
123

124
		URL u = new URL(url);
125
		HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
126
		httpURLConnection.setDoOutput(true);
127
		httpURLConnection.setRequestMethod("PUT");
128
		httpURLConnection.setRequestProperty("Accept-Charset", charset);
129
		httpURLConnection.setRequestProperty("Content-Type", "application/json");
130
		httpURLConnection.setRequestProperty("Content-Length", String.valueOf(bs.length));
131
		OutputStream outputStream = httpURLConnection.getOutputStream();
132
		try {
133
			outputStream.write(bs);
134
			outputStream.flush();
135
		} finally {
136
			IoUtil.close(outputStream);
137
		}
138
		if (httpURLConnection.getResponseCode() != 200) {
139
			throw new IOException("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
140
		}
141
		InputStream in = httpURLConnection.getInputStream();
142
		try {
143
			return this.handResponse(in, typeOfT);
144
		} finally {
145
			IoUtil.close(in);
146
		}
147
	}
148

149
	@Override
150
	public <T> T delete(String url, Type typeOfT) throws JfwBaseException, IOException {
151
		URL u = new URL(url);
152
		HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
153
		httpURLConnection.setRequestProperty("Accept-Charset", charset);
154
		httpURLConnection.setRequestMethod("DELETE");
155
		httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
156
		if (httpURLConnection.getResponseCode() != 200) {
157
			throw new IOException("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
158
		}
159
		InputStream in = httpURLConnection.getInputStream();
160
		try {
161
			return this.handResponse(in, typeOfT);
162
		} finally {
163
			IoUtil.close(in);
164
		}
165
	}
166

167
	public static class Result<T> {
168
		private boolean success;
169
		private Integer code;
170
		private String msg;
171
		private String detailMsg;
172
		private T data;
173

174
		public boolean isSuccess() {
175
			return success;
176
		}
177

178
		public void setSuccess(boolean success) {
179
			this.success = success;
180
		}
181

182
		public Integer getCode() {
183
			return code;
184
		}
185

186
		public void setCode(Integer code) {
187
			this.code = code;
188
		}
189

190
		public String getMsg() {
191
			return msg;
192
		}
193

194
		public void setMsg(String msg) {
195
			this.msg = msg;
196
		}
197

198
		public String getDetailMsg() {
199
			return detailMsg;
200
		}
201

202
		public void setDetailMsg(String detailMsg) {
203
			this.detailMsg = detailMsg;
204
		}
205

206
		public T getData() {
207
			return data;
208
		}
209

210
		public void setData(T data) {
211
			this.data = data;
212
		}
213
	}
214

215
}

+ 71 - 0
src/test/java/com/ekexiu/console/util/HttpServiceTest.java

@ -0,0 +1,71 @@
1
package com.ekexiu.console.util;
2

3
import java.lang.reflect.Method;
4
import java.lang.reflect.ParameterizedType;
5
import java.lang.reflect.Type;
6
import java.net.URLEncoder;
7

8
import org.jfw.util.PageQueryResult;
9
import org.jfw.util.json.JsonService;
10
import org.jfw.util.reflect.TypeReference;
11

12
import com.ekexiu.console.system.po.Organization;
13
import com.ekexiu.console.util.HttpServiceImpl.Result;
14

15
public class HttpServiceTest {
16
	private static HttpService http = new HttpServiceImpl();
17

18
	public static Organization get()throws Exception{
19
		PageQueryResult<Organization> ret = http.get("http://192.168.3.76:82/ajax/sys/org/pq?name="+URLEncoder.encode("%中国%","UTF-8"), new TypeReference<PageQueryResult<Organization>>() {
20
		}.getType());
21
		return ret.getData().get(0);
22
	}
23
	
24
	public static void post()throws Exception{
25
		StringBuilder sb  = new StringBuilder();
26
		sb.append("name=").append(URLEncoder.encode("中国","UTF-8")).append("&type=1&descp=").append(URLEncoder.encode("中华人民共和国","UTF-8"));
27
		
28
		
29
	 http.post("http://192.168.3.76:82/ajax/sys/org",sb.toString(),null);
30
	
31
	}
32
	
33

34
	
35
	
36
	
37
	public static void ss(Result<PageQueryResult<Organization>> s){}
38
	
39
	public static void main(String[] args)throws Exception{
40
		post();	
41
	Organization o = get();
42
	System.out.println(JsonService.toJson(o));
43
	o.setDescp("我的中国心");
44
	http.put("http://192.168.3.76:82/ajax/sys/org", o, null);
45
	 o = get();
46
	System.out.println(JsonService.toJson(o));
47
	http.delete("http://192.168.3.76:82/ajax/sys/org/id/"+o.getId(), null);
48
	
49
	
50
		
51
//	  Method m =null ;
52
//	  
53
//	  
54
//	 for(Method mm:  HttpServiceTest.class.getDeclaredMethods()){
55
//		 if(mm.getName().equals("ss")){
56
//			 m = mm;
57
//			 break;
58
//		 }
59
//	 }
60
//	 Type t = m.getGenericParameterTypes()[0];	
61
//	 System.out.println(t instanceof ParameterizedType);
62
//	 ParameterizedType p = (ParameterizedType)t;
63
//	 Type r = p.getRawType();
64
//	 
65
//	 System.out.println( r instanceof Class );
66
//	 System.out.println( ((Class)r).getName());
67
//	 System.out.println(p.getOwnerType());
68
//	 
69
		
70
	}
71
}