本文整理汇总了Java中com.googlecode.jsonrpc4j.JsonRpcServer类的典型用法代码示例。如果您正苦于以下问题:Java JsonRpcServer类的具体用法?Java JsonRpcServer怎么用?Java JsonRpcServer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonRpcServer类属于com.googlecode.jsonrpc4j包,在下文中一共展示了JsonRpcServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
public void handle(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String uri = request.getRequestURI();
JsonRpcServer skeleton = skeletonMap.get(uri);
if (cors) {
response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
response.setHeader(ACCESS_CONTROL_ALLOW_METHODS_HEADER, "POST");
response.setHeader(ACCESS_CONTROL_ALLOW_HEADERS_HEADER, "*");
}
if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
response.setStatus(200);
} else if (request.getMethod().equalsIgnoreCase("POST")) {
RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort());
try {
skeleton.handle(request.getInputStream(), response.getOutputStream());
} catch (Throwable e) {
throw new ServletException(e);
}
} else {
response.setStatus(500);
}
}
示例2: doExport
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException {
String addr = url.getIp() + ":" + url.getPort();
HttpServer server = serverMap.get(addr);
if (server == null) {
server = httpBinder.bind(url, new InternalHandler(url.getParameter("cors", false)));
serverMap.put(addr, server);
}
final String path = url.getAbsolutePath();
JsonRpcServer skeleton = new JsonRpcServer(impl, type);
skeletonMap.put(path, skeleton);
return new Runnable() {
public void run() {
skeletonMap.remove(path);
}
};
}
示例3: testGzipResponse
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
@Test
public void testGzipResponse() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post");
request.addHeader(ACCEPT_ENCODING, "gzip");
request.setContentType("application/json");
request.setContent("{\"jsonrpc\":\"2.0\",\"id\":123,\"method\":\"testMethod\",\"params\":[\"Whir?inaki\"]}".getBytes(StandardCharsets.UTF_8));
MockHttpServletResponse response = new MockHttpServletResponse();
jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class, true);
jsonRpcServer.handle(request, response);
byte[] compressed = response.getContentAsByteArray();
String sb = getCompressedResponseContent(compressed);
Assert.assertEquals(sb, "{\"jsonrpc\":\"2.0\",\"id\":123,\"result\":null}");
Assert.assertEquals("gzip", response.getHeader(CONTENT_ENCODING));
}
示例4: testCorruptRequest
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
@Test
public void testCorruptRequest() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post");
request.setContentType("application/json");
request.setContent("{NOT JSON}".getBytes(StandardCharsets.UTF_8));
MockHttpServletResponse response = new MockHttpServletResponse();
jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class, true);
jsonRpcServer.handle(request, response);
String content = response.getContentAsString();
Assert.assertEquals(content, "{\"jsonrpc\":\"2.0\",\"id\":\"null\"," +
"\"error\":{\"code\":-32700,\"message\":\"JSON parse error\"}}\n");
}
示例5: testGzipRequest
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
@Test
public void testGzipRequest() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post");
request.addHeader(CONTENT_ENCODING, "gzip");
request.setContentType("application/json");
byte[] bytes = "{\"jsonrpc\":\"2.0\",\"id\":123,\"method\":\"testMethod\",\"params\":[\"Whir?inaki\"]}".getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(bytes);
gos.close();
request.setContent(baos.toByteArray());
MockHttpServletResponse response = new MockHttpServletResponse();
jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class, true);
jsonRpcServer.handle(request, response);
String responseContent = new String(response.getContentAsByteArray(), StandardCharsets.UTF_8);
Assert.assertEquals(responseContent, "{\"jsonrpc\":\"2.0\",\"id\":123,\"result\":null}\n");
Assert.assertNull(response.getHeader(CONTENT_ENCODING));
}
示例6: testGzipRequestAndResponse
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
@Test
public void testGzipRequestAndResponse() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post");
request.addHeader(CONTENT_ENCODING, "gzip");
request.addHeader(ACCEPT_ENCODING, "gzip");
request.setContentType("application/json");
byte[] bytes = "{\"jsonrpc\":\"2.0\",\"id\":123,\"method\":\"testMethod\",\"params\":[\"Whir?inaki\"]}".getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(bytes);
gos.close();
request.setContent(baos.toByteArray());
MockHttpServletResponse response = new MockHttpServletResponse();
jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class, true);
jsonRpcServer.handle(request, response);
byte[] compressed = response.getContentAsByteArray();
String sb = getCompressedResponseContent(compressed);
Assert.assertEquals(sb, "{\"jsonrpc\":\"2.0\",\"id\":123,\"result\":null}");
Assert.assertEquals("gzip", response.getHeader(CONTENT_ENCODING));
}
示例7: main
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
public static void main(String[] args) throws LifecycleException {
UserService userService = new UserServiceImpl();
JsonRpcServer jsonRpcServer = new JsonRpcServer(userService, UserService.class);
Tomcat tomcat = new Tomcat();
String baseDir = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
tomcat.setBaseDir(baseDir);
tomcat.setHostname("localhost");
tomcat.setPort(8099);
tomcat.getConnector().setProperty("maxThreads", "10");
tomcat.getConnector().setProperty("maxConnections", "10");
tomcat.getConnector().setProperty("URIEncoding", "UTF-8");
tomcat.getConnector().setProperty("connectionTimeout", "60000");
tomcat.getConnector().setProperty("maxKeepAliveRequests", "-1");
tomcat.getConnector().setProtocol("org.apache.coyote.http11.Http11NioProtocol");
Context context = tomcat.addContext("/", baseDir);
Tomcat.addServlet(context, "dispatcher", new HttpServlet() {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
jsonRpcServer.handle(req, resp);
}
});
context.addServletMapping("/*", "dispatcher");
tomcat.start();
tomcat.getServer().await();
}
示例8: setup
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
@Before
public void setup() {
jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class);
jsonRpcServer.setInterceptorList(new ArrayList<JsonRpcInterceptor>() {{
add(mockInterceptor);
}});
byteArrayOutputStream = new ByteArrayOutputStream();
}
示例9: assertHttpStatusCodeForJsonRpcRequest
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
public static void assertHttpStatusCodeForJsonRpcRequest(InputStream message, int expectedCode, JsonRpcServer server) throws Exception {
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
req.setMethod(HttpMethod.POST.name());
req.setContent(convertInputStreamToByteArray(message));
server.handle(req, res);
Assert.assertEquals(expectedCode, res.getStatus());
}
示例10: init
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
@Override
public void init() {
try {
final Class<?> aClass = Class.forName(getInitParameter("class"));
final Object instance = aClass.getConstructor().newInstance();
jsonRpcServer = new JsonRpcServer(instance);
jsonRpcServer.setErrorResolver(AnnotationsErrorResolver.INSTANCE);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
示例11: makeJsonRpcRequestObject
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
@SuppressWarnings("serial")
private static HashMap<String, Object> makeJsonRpcRequestObject(final Object id, final String methodName, final Object params) {
return new HashMap<String, Object>() {
{
if (id != null) put(JsonRpcBasicServer.ID, id);
put(JsonRpcBasicServer.JSONRPC, JsonRpcServer.VERSION);
if (methodName != null) put(JsonRpcBasicServer.METHOD, methodName);
if (params != null) put(JsonRpcBasicServer.PARAMS, params);
}
};
}
示例12: afterPropertiesSet
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
public final void afterPropertiesSet()
throws Exception {
// find the ObjectMapper
if (objectMapper == null
&& applicationContext != null
&& applicationContext.containsBean("objectMapper")) {
objectMapper = (ObjectMapper) applicationContext.getBean("objectMapper");
}
if (objectMapper == null && applicationContext != null) {
try {
objectMapper = (ObjectMapper)BeanFactoryUtils
.beanOfTypeIncludingAncestors(applicationContext, ObjectMapper.class);
} catch (Exception e) { /* no-op */ }
}
if (objectMapper==null) {
objectMapper = new ObjectMapper();
}
// create the service
Object service = ProxyUtil.createCompositeServiceProxy(
getClass().getClassLoader(), services,
serviceInterfaces, allowMultipleInheritance);
// create the server
jsonRpcServer = new JsonRpcServer(objectMapper, service);
jsonRpcServer.setErrorResolver(errorResolver);
jsonRpcServer.setBackwardsComaptible(backwardsComaptible);
jsonRpcServer.setRethrowExceptions(rethrowExceptions);
jsonRpcServer.setAllowExtraParams(allowExtraParams);
jsonRpcServer.setAllowLessParams(allowLessParams);
// export
exportService();
}
示例13: afterPropertiesSet
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
public void afterPropertiesSet()
throws Exception {
// find the ObjectMapper
if (objectMapper == null
&& applicationContext != null
&& applicationContext.containsBean("objectMapper")) {
objectMapper = (ObjectMapper) applicationContext.getBean("objectMapper");
}
if (objectMapper == null && applicationContext != null) {
try {
objectMapper = (ObjectMapper)BeanFactoryUtils
.beanOfTypeIncludingAncestors(applicationContext, ObjectMapper.class);
} catch (Exception e) { /* no-op */ }
}
if (objectMapper==null) {
objectMapper = new ObjectMapper();
}
// create the server
jsonRpcServer = new JsonRpcServer(
objectMapper, getService(), getServiceInterface());
jsonRpcServer.setErrorResolver(errorResolver);
jsonRpcServer.setBackwardsComaptible(backwardsComaptible);
jsonRpcServer.setRethrowExceptions(rethrowExceptions);
jsonRpcServer.setAllowExtraParams(allowExtraParams);
jsonRpcServer.setAllowLessParams(allowLessParams);
// export
exportService();
}
示例14: createImportDeclaration
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
@Override
protected <T> ImportDeclaration createImportDeclaration(String endpointId, Class<T> klass, T object) {
//A JsonServlet must be registered
final JsonRpcServer jsonRpcServer = new JsonRpcServer(object, klass);
httpServer.createContext(SERVLET_NAME + "/" + endpointId, new HttpHandler() {
public void handle(HttpExchange httpExchange) throws IOException {
// Get InputStream for reading the request body.
// After reading the request body, the stream is close.
InputStream is = httpExchange.getRequestBody();
// Get OutputStream to send the response body.
// When the response body has been written, the stream must be closed to terminate the exchange.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
jsonRpcServer.handle(is, byteArrayOutputStream);
byteArrayOutputStream.close();
int size = byteArrayOutputStream.size();
// send response header
httpExchange.sendResponseHeaders(HttpStatus.SC_OK, size);
// write response to real outputStream
OutputStream realOs = httpExchange.getResponseBody();
realOs.write(byteArrayOutputStream.toByteArray(), 0, size);
realOs.close();
}
});
// Build associated ImportDeclaration
Map<String, Object> props = new HashMap<String, Object>();
props.put(ID, endpointId);
props.put(URL, "http://localhost:" + HTTP_PORT + SERVLET_NAME + "/" + endpointId);
props.put(SERVICE_CLASS, klass.getName());
props.put(CONFIGS, "jsonrpc");
return ImportDeclarationBuilder.fromMetadata(props).build();
}
示例15: setUp
import com.googlecode.jsonrpc4j.JsonRpcServer; //导入依赖的package包/类
@Override
protected void setUp() throws Exception {
super.setUp();
server = new AutomatorHttpServer(PORT);
server.route("/jsonrpc/0", new JsonRpcServer(new ObjectMapper(),
new AutomatorServiceImpl(), AutomatorService.class));
server.start();
}