use of com.datatorrent.stram.util.WebServicesClient in project apex-core by apache.
the class WebServicesVersionConversionTest method testVersioning.
@Test
public void testVersioning() throws Exception {
WebServicesClient wsClient = new WebServicesClient();
Client client = wsClient.getClient();
WebResource ws = client.resource("http://localhost:" + port).path("/new_path");
WebServicesVersionConversion.Converter versionConverter = new WebServicesVersionConversion.Converter() {
@Override
public String convertCommandPath(String path) {
if (path.equals("/new_path")) {
return "/old_path";
}
return path;
}
@Override
public String convertResponse(String path, String response) {
if (path.equals("/new_path")) {
try {
JSONObject json = new JSONObject(response);
json.put("new_key", json.get("old_key"));
json.remove("old_key");
return json.toString();
} catch (JSONException ex) {
throw new RuntimeException(ex);
}
}
return response;
}
};
VersionConversionFilter versionConversionFilter = new VersionConversionFilter(versionConverter);
client.addFilter(versionConversionFilter);
JSONObject result = new JSONObject(ws.get(String.class));
Assert.assertEquals(result.getString("url"), "/old_path");
Assert.assertEquals(result.getString("new_key"), "value");
Assert.assertEquals(result.getString("other_key"), "other_value");
}
use of com.datatorrent.stram.util.WebServicesClient in project apex-core by apache.
the class RecordingsAgent method startRecording.
public JSONObject startRecording(String appId, String opId, String portName, long numWindows) throws IncompatibleVersionException {
LOG.debug("Start recording requested for {}.{} ({} windows)", opId, portName, numWindows);
try {
final JSONObject request = new JSONObject();
StramAgent.StramUriSpec uriSpec = new StramAgent.StramUriSpec();
uriSpec = uriSpec.path(StramWebServices.PATH_PHYSICAL_PLAN_OPERATORS).path(opId);
if (!StringUtils.isBlank(portName)) {
uriSpec = uriSpec.path("ports").path(portName);
}
uriSpec = uriSpec.path(StramWebServices.PATH_RECORDINGS_START);
request.put("numWindows", numWindows);
WebServicesClient webServicesClient = new WebServicesClient();
return stramAgent.issueStramWebRequest(webServicesClient, appId, uriSpec, new WebServicesClient.WebServicesHandler<JSONObject>() {
@Override
public JSONObject process(WebResource.Builder webResource, Class<JSONObject> clazz) {
return webResource.type(MediaType.APPLICATION_JSON).post(clazz, request);
}
});
} catch (Exception ex) {
LOG.error("Cannot start recording: ", ex);
return null;
}
}
use of com.datatorrent.stram.util.WebServicesClient in project apex-core by apache.
the class RecordingsAgent method stopRecording.
public JSONObject stopRecording(String appId, String opId, String portName) throws IncompatibleVersionException {
try {
final JSONObject request = new JSONObject();
StramAgent.StramUriSpec uriSpec = new StramAgent.StramUriSpec();
uriSpec = uriSpec.path(StramWebServices.PATH_PHYSICAL_PLAN_OPERATORS).path(opId);
if (!StringUtils.isBlank(portName)) {
uriSpec = uriSpec.path("ports").path(portName);
}
uriSpec = uriSpec.path(StramWebServices.PATH_RECORDINGS_STOP);
WebServicesClient webServicesClient = new WebServicesClient();
return stramAgent.issueStramWebRequest(webServicesClient, appId, uriSpec, new WebServicesClient.WebServicesHandler<JSONObject>() {
@Override
public JSONObject process(WebResource.Builder webResource, Class<JSONObject> clazz) {
return webResource.type(MediaType.APPLICATION_JSON).post(clazz, request);
}
});
} catch (Exception ex) {
LOG.error("Cannot stop recording: ", ex);
return null;
}
}
use of com.datatorrent.stram.util.WebServicesClient in project apex-core by apache.
the class RecordingsAgent method getRunningContainerIds.
private Set<String> getRunningContainerIds(String appId) {
Set<String> result = new HashSet<>();
try {
WebServicesClient webServicesClient = new WebServicesClient();
JSONObject response = stramAgent.issueStramWebGetRequest(webServicesClient, appId, StramWebServices.PATH_PHYSICAL_PLAN_CONTAINERS);
Object containersObj = response.get("containers");
JSONArray containers;
if (containersObj instanceof JSONArray) {
containers = (JSONArray) containersObj;
} else {
containers = new JSONArray();
containers.put(containersObj);
}
int len = containers.length();
for (int i = 0; i < len; i++) {
JSONObject container = containers.getJSONObject(i);
if (container.getString("state").equals("ACTIVE")) {
result.add(container.getString("id"));
}
}
} catch (Exception ex) {
LOG.warn("Error {} getting running containers for {}. Assuming no containers are running.", ex.getMessage(), appId);
}
return result;
}
use of com.datatorrent.stram.util.WebServicesClient in project apex-core by apache.
the class StramAgent method retrieveWebServicesInfo.
private StramWebServicesInfo retrieveWebServicesInfo(String appId) {
String url;
try (YarnClient yarnClient = StramClientUtils.createYarnClient(conf)) {
ApplicationReport ar = yarnClient.getApplicationReport(ConverterUtils.toApplicationId(appId));
if (ar == null) {
LOG.warn("YARN does not have record for this application {}", appId);
return null;
} else if (ar.getYarnApplicationState() != YarnApplicationState.RUNNING) {
LOG.debug("Application {} is not running (state: {})", appId, ar.getYarnApplicationState());
return null;
}
String trackingUrl = ar.getTrackingUrl();
if (!trackingUrl.startsWith("http://") && !trackingUrl.startsWith("https://")) {
url = "http://" + trackingUrl;
} else {
url = trackingUrl;
}
if (StringUtils.isBlank(url)) {
LOG.error("Cannot get tracking url from YARN");
return null;
}
if (url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
}
url += WebServices.PATH;
} catch (Exception ex) {
LOG.error("Cannot retrieve web services info", ex);
return null;
}
WebServicesClient webServicesClient = new WebServicesClient();
try {
JSONObject response;
String secToken = null;
ClientResponse clientResponse;
int i = 0;
while (true) {
LOG.debug("Accessing url {}", url);
clientResponse = webServicesClient.process(url, ClientResponse.class, new WebServicesClient.GetWebServicesHandler<ClientResponse>());
String val = clientResponse.getHeaders().getFirst("Refresh");
if (val == null) {
break;
}
int index = val.indexOf("url=");
if (index < 0) {
break;
}
url = val.substring(index + 4);
if (i++ > MAX_REDIRECTS) {
LOG.error("Cannot get web service info -- exceeded the max number of redirects");
return null;
}
}
if (!UserGroupInformation.isSecurityEnabled()) {
response = new JSONObject(clientResponse.getEntity(String.class));
} else {
if (UserGroupInformation.isSecurityEnabled()) {
for (NewCookie nc : clientResponse.getCookies()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Cookie " + nc.getName() + " " + nc.getValue());
}
if (nc.getName().equals(StramWSFilter.CLIENT_COOKIE)) {
secToken = nc.getValue();
}
}
}
response = new JSONObject(clientResponse.getEntity(String.class));
}
String version = response.getString("version");
response = webServicesClient.process(url + "/" + version + "/stram/info", JSONObject.class, new WebServicesClient.GetWebServicesHandler<JSONObject>());
String appMasterUrl = response.getString("appMasterTrackingUrl");
String appPath = response.getString("appPath");
String user = response.getString("user");
JSONObject permissionsInfo = null;
Path permissionsPath = new Path(appPath, "permissions.json");
LOG.debug("Checking for permission information in file {}", permissionsPath);
try {
if (fileSystem.exists(permissionsPath)) {
LOG.info("Loading permission information");
try (FSDataInputStream is = fileSystem.open(permissionsPath)) {
permissionsInfo = new JSONObject(IOUtils.toString(is));
}
LOG.debug("Loaded permission file successfully");
} else {
// ignore and log messages if file is not found
LOG.info("Permission information is not available as the application is not configured with it");
}
} catch (IOException ex) {
// ignore and log message when unable to read the file
LOG.info("Permission information is not available", ex);
}
return new StramWebServicesInfo(appMasterUrl, version, appPath, user, secToken, permissionsInfo);
} catch (Exception ex) {
LOG.warn("Cannot retrieve web service info for app {}", appId, ex);
return null;
}
}
Aggregations