@Test public void testSendHeartbeat() throws InterruptedException { ZSession session = createSession("2"); Thread.sleep(1000); WebResource wr = sessionsr.path(session.id); Builder b = wr.accept(MediaType.APPLICATION_JSON); ClientResponse cr = b.put(ClientResponse.class, null); assertEquals(ClientResponse.Status.OK, cr.getClientResponseStatus()); Thread.sleep(1500); assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); Thread.sleep(1000); assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); }
@Test public void testCreate() throws Exception { String path = "/"; String name = "roottest-create"; byte[] data = "foo".getBytes(); WebResource wr = znodesr.path(path).queryParam("dataformat", "utf8") .queryParam("name", name); Builder builder = wr.accept(MediaType.APPLICATION_JSON); ClientResponse cr; cr = builder.post(ClientResponse.class, data); Assert.assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus()); ZPath zpath = cr.getEntity(ZPath.class); Assert.assertEquals(new ZPath(path + name), zpath); Assert.assertEquals(znodesr.path(path).toString(), zpath.uri); // use out-of-band method to verify byte[] rdata = zk.getData(zpath.path, false, new Stat()); Assert.assertTrue(new String(rdata) + " == " + new String(data), Arrays.equals(rdata, data)); }
@Test public void testCreate() throws Exception { LOG.info("STARTING " + getName()); String path = "/"; String name = "roottest-create"; byte[] data = "foo".getBytes(); WebResource wr = znodesr.path(path).queryParam("dataformat", "utf8") .queryParam("name", name); Builder builder = wr.accept(MediaType.APPLICATION_JSON); ClientResponse cr; cr = builder.post(ClientResponse.class, data); assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus()); ZPath zpath = cr.getEntity(ZPath.class); assertEquals(new ZPath(path + name), zpath); assertEquals(znodesr.path(path).toString(), zpath.uri); // use out-of-band method to verify byte[] rdata = zk.getData(zpath.path, false, new Stat()); assertTrue(new String(rdata) + " == " + new String(data), Arrays.equals(rdata, data)); }
@Test public void testCreateEphemeralZNode() throws KeeperException, InterruptedException, IOException { ZSession session = createSession("30"); WebResource wr = znodesr.path("/") .queryParam("op", "create") .queryParam("name", "ephemeral-test") .queryParam("ephemeral", "true") .queryParam("session", session.id) .queryParam("null", "true"); Builder b = wr.accept(MediaType.APPLICATION_JSON); ClientResponse cr = b.post(ClientResponse.class); assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus()); Stat stat = new Stat(); zk.getData("/ephemeral-test", false, stat); ZooKeeper sessionZK = ZooKeeperService.getClient(CONTEXT_PATH, session.id); assertEquals(stat.getEphemeralOwner(), sessionZK.getSessionId()); }
@Test public void testSendHeartbeat() throws InterruptedException { ZSession session = createSession("2"); Thread.sleep(1000); WebResource wr = sessionsr.path(session.id); Builder b = wr.accept(MediaType.APPLICATION_JSON); ClientResponse cr = b.put(ClientResponse.class, null); Assert.assertEquals(ClientResponse.Status.OK, cr.getClientResponseStatus()); Thread.sleep(1500); Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); Thread.sleep(1000); Assert.assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); }
@Test public void testCreateEphemeralZNode() throws KeeperException, InterruptedException, IOException { ZSession session = createSession("30"); WebResource wr = znodesr.path("/") .queryParam("op", "create") .queryParam("name", "ephemeral-test") .queryParam("ephemeral", "true") .queryParam("session", session.id) .queryParam("null", "true"); Builder b = wr.accept(MediaType.APPLICATION_JSON); ClientResponse cr = b.post(ClientResponse.class); Assert.assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus()); Stat stat = new Stat(); zk.getData("/ephemeral-test", false, stat); ZooKeeper sessionZK = ZooKeeperService.getClient(CONTEXT_PATH, session.id); Assert.assertEquals(stat.getEphemeralOwner(), sessionZK.getSessionId()); }
public static <T> T get(RestUrl ru, Class<T> clz, Class<?>... clzs) { if (null == ru) { return null; } LOGGER.info("Rest url: " + NFVOUtils.getRealUrl(ru)); WebResource webResource = CLIENT.resource(NFVOUtils.getRealUrl(ru)); Builder builder = webResource.type(SysConst.MEDIA_JSON_UTF_8); if (ru.hasToken()) { if (NFVOUtils.isEmpty(ru.getPairs().getPairVal(RestUrl.TOKEN_KEY))) { ru.addPair(RestUrl.TOKEN_KEY, getTokenId()); } builder = builder.header(RestUrl.TOKEN_KEY, ru.getPairs().getPairVal(RestUrl.TOKEN_KEY)); } String resp = builder.get(ClientResponse.class).getEntity(String.class); LOGGER.info("Rest response from unicom cloud: " + resp); return jsonToBean(clz, resp, clzs); }
public static <T> T put(RestUrl ru, Class<T> clz, Class<?>... clzs) { if (null == ru) { return null; } LOGGER.info("Rest url: " + NFVOUtils.getRealUrl(ru)); WebResource webResource = CLIENT.resource(NFVOUtils.getRealUrl(ru)); Builder builder = webResource.type(SysConst.MEDIA_JSON_UTF_8); if (ru.hasToken()) { if (NFVOUtils.isEmpty(ru.getPairs().getPairVal(RestUrl.TOKEN_KEY))) { ru.addPair(RestUrl.TOKEN_KEY, getTokenId()); } builder = builder.header(RestUrl.TOKEN_KEY, ru.getPairs().getPairVal(RestUrl.TOKEN_KEY)); } Object postjson = ru.getReqjson(); String resp = builder.put(ClientResponse.class, postjson.getClass() == String.class ? postjson : JSONObject.fromObject(postjson).toString()).getEntity( String.class); LOGGER.info("Rest response from unicom cloud: " + resp); return jsonToBean(clz, resp, clzs); }
public static <T> T post(RestUrl ru, Class<T> clz, Class<?>... clzs) { if (null == ru) { return null; } LOGGER.info("Rest url: " + NFVOUtils.getRealUrl(ru)); WebResource webResource = CLIENT.resource(NFVOUtils.getRealUrl(ru)); Builder builder = webResource.type(SysConst.MEDIA_JSON_UTF_8); if (ru.hasToken()) { if (NFVOUtils.isEmpty(ru.getPairs().getPairVal(RestUrl.TOKEN_KEY))) { ru.addPair(RestUrl.TOKEN_KEY, getTokenId()); } builder = builder.header(RestUrl.TOKEN_KEY, ru.getPairs().getPairVal(RestUrl.TOKEN_KEY)); } Object postjson = ru.getReqjson(); String resp = builder.post(ClientResponse.class, postjson.getClass() == String.class ? postjson : JSONObject.fromObject(postjson).toString()).getEntity( String.class); LOGGER.info("Rest response from unicom cloud: " + resp); return jsonToBean(clz, resp, clzs); }
public static <T> T delete(RestUrl ru, Class<T> clz, Class<?>... clzs) { if (null == ru) { return null; } LOGGER.info("Rest url: " + NFVOUtils.getRealUrl(ru)); WebResource webResource = CLIENT.resource(NFVOUtils.getRealUrl(ru)); Builder builder = webResource.type(SysConst.MEDIA_JSON_UTF_8); if (ru.hasToken()) { if (NFVOUtils.isEmpty(ru.getPairs().getPairVal(RestUrl.TOKEN_KEY))) { ru.addPair(RestUrl.TOKEN_KEY, getTokenId()); } builder = builder.header(RestUrl.TOKEN_KEY, ru.getPairs().getPairVal(RestUrl.TOKEN_KEY)); } Object postjson = ru.getReqjson(); String resp = builder.delete(ClientResponse.class, postjson.getClass() == String.class ? postjson : JSONObject.fromObject(postjson).toString()).getEntity( String.class); LOGGER.info("Rest response from unicom cloud: " + resp); return jsonToBean(clz, resp, clzs); }
public static Result deleteForNoResp(RestUrl ru) { if (null == ru) { return new Result(400, "RestUrl can not be empty!"); } LOGGER.info("Rest url: " + NFVOUtils.getRealUrl(ru)); WebResource webResource = CLIENT.resource(NFVOUtils.getRealUrl(ru)); Builder builder = webResource.type(SysConst.MEDIA_JSON_UTF_8); if (ru.hasToken()) { if (NFVOUtils.isEmpty(ru.getPairs().getPairVal(RestUrl.TOKEN_KEY))) { ru.addPair(RestUrl.TOKEN_KEY, getTokenId()); } builder = builder.header(RestUrl.TOKEN_KEY, ru.getPairs().getPairVal(RestUrl.TOKEN_KEY)); } Object postjson = ru.getReqjson(); ClientResponse clientResponse = builder.delete(ClientResponse.class, postjson.getClass() == String.class ? postjson : JSONObject.fromObject(postjson).toString()); return new Result(clientResponse.getStatus(), clientResponse.getStatusInfo().getReasonPhrase()); }
/** * get a response for the given url and method * * @param url * @param method * @return * @throws Exception */ public ClientResponse getResponse(String url, Method method) throws Exception { Builder resource = getResource(url); ClientResponse response = null; switch (method) { case Get: response = resource.get(ClientResponse.class); break; case Post: response = resource.post(ClientResponse.class); break; case Head: response = resource.head(); break; case Put: response = resource.put(ClientResponse.class); break; } return response; }
public <T> ClientResponse<T> invoke(RestMethod method, String url, Class<T> expectedResponse, BasicAuth basicAuth, Headers... headers) { Builder builder = createBuilder(url, headers, basicAuth); com.sun.jersey.api.client.ClientResponse clienteResponse = null; switch (method) { case GET: clienteResponse = builder.get(com.sun.jersey.api.client.ClientResponse.class); break; case DELETE: clienteResponse = builder.delete(com.sun.jersey.api.client.ClientResponse.class); break; case HEAD: clienteResponse = builder.head(); break; default: throw new UnsupportedOperationException(); } return new ClientResponse<T>(clienteResponse, expectedResponse); }
public <T> ClientResponse<T> invoke(RestMethod method, String url, GenericType<T> genericType, BasicAuth basicAuth, Headers... headers) { Builder builder = createBuilder(url, headers, basicAuth); com.sun.jersey.api.client.ClientResponse clienteResponse = null; switch (method) { case GET: clienteResponse = builder.get(com.sun.jersey.api.client.ClientResponse.class); break; case DELETE: clienteResponse = builder.delete(com.sun.jersey.api.client.ClientResponse.class); break; default: throw new UnsupportedOperationException(); } return new ClientResponse<T>(clienteResponse, genericType); }
public <T> ClientResponse<T> invoke(RestMethod method, String url, Object requestEntity, Class<T> expectedResponse, BasicAuth basicAuth, Headers... headers) { Builder builder = createBuilder(url, headers, basicAuth); com.sun.jersey.api.client.ClientResponse clienteResponse = null; switch (method) { case POST: clienteResponse = builder.post(com.sun.jersey.api.client.ClientResponse.class, requestEntity); break; case PUT: clienteResponse = builder.put(com.sun.jersey.api.client.ClientResponse.class, requestEntity); break; case DELETE: clienteResponse = builder.delete(com.sun.jersey.api.client.ClientResponse.class, requestEntity); break; default: throw new UnsupportedOperationException(); } return new ClientResponse<T>(clienteResponse, expectedResponse); }
public <T> ClientResponse<T> invoke(RestMethod method, String url, Object requestEntity, GenericType<T> genericType, BasicAuth basicAuth, Headers... headers) { Builder builder = createBuilder(url, headers, basicAuth); com.sun.jersey.api.client.ClientResponse clienteResponse = null; switch (method) { case POST: clienteResponse = builder.post(com.sun.jersey.api.client.ClientResponse.class, requestEntity); break; case PUT: clienteResponse = builder.put(com.sun.jersey.api.client.ClientResponse.class, requestEntity); break; case DELETE: clienteResponse = builder.delete(com.sun.jersey.api.client.ClientResponse.class, requestEntity); break; default: throw new UnsupportedOperationException(); } return new ClientResponse<T>(clienteResponse, genericType); }
private Builder createBuilder(String url, Headers[] headers, BasicAuth auth) { WebResource resource; if (null != auth) { resource = Client.create(config).resource(createURI(url)); resource.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(auth.getName(), auth .getPassword())); } else { resource = Client.create(config).resource(createURI(url)); } Builder builder = resource.accept(acceptMediaType).type(applicationType); for (Headers h : headers) { builder.header(h.getKey(), h.getValue()); } return builder; }
public void uploadVoid(String url, File f, String formName, Headers... headers) { FormDataMultiPart form = new FormDataMultiPart().field(formName, f, MediaType.MULTIPART_FORM_DATA_TYPE); String urlCreated = createURI(url); ClientConfig cc = new DefaultClientConfig(); cc.getClasses().add(MultiPartWriter.class); WebResource webResource = Client.create(cc).resource(urlCreated); Builder builder = webResource.type(MULTIPART_MEDIA).accept(MEDIA); for (Headers h : headers) { builder.header(h.getKey(), h.getValue()); } builder.post(form); }
public ClientResponse<File> upload(String url, File f, Headers... headers) { @SuppressWarnings("resource") FormDataMultiPart form = new FormDataMultiPart(); form.bodyPart(new FileDataBodyPart("file", f, MediaType.APPLICATION_OCTET_STREAM_TYPE)); String urlCreated = createURI(url); ClientConfig cc = new DefaultClientConfig(); cc.getClasses().add(MultiPartWriter.class); WebResource webResource = Client.create(cc).resource(urlCreated); Builder builder = webResource.type(MULTIPART_MEDIA).accept(MEDIA).accept("text/plain"); for (Headers h : headers) { builder.header(h.getKey(), h.getValue()); } com.sun.jersey.api.client.ClientResponse clienteResponse = null; clienteResponse = builder.post(com.sun.jersey.api.client.ClientResponse.class, form); return new ClientResponse<File>(clienteResponse, File.class); }