protected boolean exists(final Location location, final String uri) { try { final ClientResponse response = getNexusClient().uri(uri).head(); if (!ClientResponse.Status.OK.equals(response.getClientResponseStatus())) { if (ClientResponse.Status.NOT_FOUND.equals(response.getClientResponseStatus())) { return false; } throw getNexusClient() .convert( new ContextAwareUniformInterfaceException(response) { @Override public String getMessage(final int status) { if (status == Response.Status.NOT_FOUND.getStatusCode()) { return String.format("Inexistent path: %s", location); } return null; } }); } return true; } catch (ClientHandlerException e) { throw getNexusClient().convert(e); } }
// verify the exception object default format is JSON @Test public void testNodeAppsStateInvalidDefault() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp("foo", 1234, 2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { r.path("ws") .path("v1") .path("node") .path("apps") .queryParam("state", "FOO_STATE") .get(JSONObject.class); fail("should have thrown exception on invalid user query"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); verifyStateInvalidException(message, type, classname); } }
protected void download(final Location location, final String uri, final OutputStream target) throws IOException { try { final ClientResponse response = getNexusClient().uri(uri).get(ClientResponse.class); if (!ClientResponse.Status.OK.equals(response.getClientResponseStatus())) { throw getNexusClient() .convert( new ContextAwareUniformInterfaceException(response) { @Override public String getMessage(final int status) { if (status == Response.Status.NOT_FOUND.getStatusCode()) { return String.format("Inexistent path: %s", location); } return null; } }); } try { IOUtil.copy(response.getEntityInputStream(), target); } finally { response.close(); } } catch (ClientHandlerException e) { throw getNexusClient().convert(e); } }
private void checkResponse(ClientResponse response, String requestType) { Status status = response.getClientResponseStatus(); if (status != ClientResponse.Status.OK && status != ClientResponse.Status.ACCEPTED && status != ClientResponse.Status.NO_CONTENT) { // error if (status == ClientResponse.Status.UNAUTHORIZED) { throw VNXeException.exceptions.authenticationFailure(_url.toString()); } String code = null; code = Integer.toString(response.getStatus()); StringBuilder errorBuilder = new StringBuilder(); errorBuilder.append(requestType).append(" request to:"); errorBuilder.append(_url); errorBuilder.append(" failed with status code: "); errorBuilder.append(code); errorBuilder.append(" "); errorBuilder.append("message: "); String msg = response.getEntity(String.class); errorBuilder.append(msg); _logger.error(errorBuilder.toString()); throw VNXeException.exceptions.vnxeCommandFailed(_url, code, msg); } }
/** * @param path * @param expectedValue */ private void shouldRetrieveResourceWithValue(final String path, final String expectedValue) { final ClientResponse res = root().path(path).accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); assertEquals(ClientResponse.Status.OK, res.getClientResponseStatus()); assertEquals(expectedValue, res.getEntity(String.class)); }
/** * Constructor. * * @param message Context message * @param response from the server */ public ROSRSException(String message, ClientResponse response) { this( message, response.getStatus(), response.getClientResponseStatus().getReasonPhrase(), response.getEntity(String.class)); }
@Override protected int checkResponse(URI uri, ClientResponse response) { ClientResponse.Status status = response.getClientResponseStatus(); int errorCode = status.getStatusCode(); if (errorCode >= 300) { JSONObject obj = null; int code = 0; try { obj = response.getEntity(JSONObject.class); code = obj.getInt(ScaleIOConstants.ERROR_CODE); } catch (Exception e) { log.error("Parsing the failure response object failed", e); } if (code == 404 || code == 410) { throw ScaleIOException.exceptions.resourceNotFound(uri.toString()); } else if (code == 401) { throw ScaleIOException.exceptions.authenticationFailure(uri.toString()); } else { throw ScaleIOException.exceptions.internalError(uri.toString(), obj.toString()); } } else { return errorCode; } }
@Test public void shouldReturn400OnNotFoundException() { String todo = "test-todo"; Mockito.doThrow(new TodoNotFoundException()).when(todoServiceMock).removeTodo(todo); ClientResponse response = resource().path("todo/" + todo).delete(ClientResponse.class); Assert.assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus()); Assert.assertEquals("TodoNotFoundException", response.getEntity(String.class)); }
protected void checkStatus(ClientResponse response, Status... status) { if (status == null || status.length == 0) { return; } if (Arrays.<Status>asList(status).contains(response.getClientResponseStatus())) { return; } throw new UniformInterfaceException(response); }
@Test public void testInvalidUri2() throws JSONException, Exception { WebResource r = resource(); String responseStr = ""; try { responseStr = r.accept(MediaType.APPLICATION_JSON).get(String.class); fail("should have thrown exception on invalid uri"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.NOT_FOUND, response.getClientResponseStatus()); WebServicesTestUtils.checkStringMatch("error string exists and shouldn't", "", responseStr); } }
@Test public void testInvalidAccept() throws JSONException, Exception { WebResource r = resource(); String responseStr = ""; try { responseStr = r.path("ws").path("v1").path("cluster").accept(MediaType.TEXT_PLAIN).get(String.class); fail("should have thrown exception on invalid uri"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.INTERNAL_SERVER_ERROR, response.getClientResponseStatus()); WebServicesTestUtils.checkStringMatch("error string exists and shouldn't", "", responseStr); } }
private void throwIf400WithErrorMessage(final ClientResponse response) { if (ClientResponse.Status.BAD_REQUEST.equals(response.getClientResponseStatus())) { final String body = getResponseBody(response); response.bufferEntity(); ErrorResponse errorResponse = null; try { errorResponse = response.getEntity(ErrorResponse.class); } catch (Exception ignore) { // most probably we do not have an error response } if (errorResponse != null) { // convert them to hide stupid "old" REST model, and not have it leak out final List<ErrorMessage> errors = Lists.newArrayList(); for (org.sonatype.nexus.client.internal.msg.ErrorMessage message : errorResponse.getErrors()) { errors.add( new NexusClientErrorResponseException.ErrorMessage( message.getId(), message.getMsg())); } throw new NexusClientErrorResponseException( response.getClientResponseStatus().getReasonPhrase(), body, errors); } } }
@Override protected void authenticate() { log.info("Authenticating"); _client.removeAllFilters(); _client.addFilter(new HTTPBasicAuthFilter(_username, _password)); if (log.isDebugEnabled()) { _client.addFilter(new LoggingFilter(System.out)); } URI requestURI = _base.resolve(URI.create(ScaleIOConstants.API_LOGIN)); ClientResponse response = _client.resource(requestURI).type(MediaType.APPLICATION_JSON).get(ClientResponse.class); if (response.getClientResponseStatus() != ClientResponse.Status.OK && response.getClientResponseStatus() != ClientResponse.Status.CREATED) { throw ScaleIOException.exceptions.authenticationFailure(_base.toString()); } _authToken = response.getEntity(String.class).replace("\"", ""); _client.removeAllFilters(); _client.addFilter(new HTTPBasicAuthFilter(_username, _authToken)); if (log.isDebugEnabled()) { _client.addFilter(new LoggingFilter(System.out)); } }
/* * Send DELETE request to KittyHawk server, and handle redirect/cookies * * @param resource webResource * * @param param parameters for delete * * @throws VNXeException */ public ClientResponse deleteRequest(Object param) throws VNXeException { _logger.debug("delete data: " + _url); ClientResponse response = sendDeleteRequest(param); Status statusCode = response.getClientResponseStatus(); if (statusCode == ClientResponse.Status.OK || statusCode == ClientResponse.Status.ACCEPTED || statusCode == ClientResponse.Status.NO_CONTENT) { return response; } else if (response.getClientResponseStatus() == ClientResponse.Status.UNAUTHORIZED) { authenticate(); response = sendDeleteRequest(param); statusCode = response.getClientResponseStatus(); if (statusCode == ClientResponse.Status.OK || statusCode == ClientResponse.Status.ACCEPTED || statusCode == ClientResponse.Status.NO_CONTENT) { return response; } } int redirectTimes = 1; // handle redirect while (response.getClientResponseStatus() == ClientResponse.Status.FOUND && redirectTimes < VNXeConstants.REDIRECT_MAX) { String code = response.getClientResponseStatus().toString(); String data = response.getEntity(String.class); _logger.debug("Returned code: {} returned data: ", code, data); WebResource resource = handelRedirect(response); if (resource != null) { response = buildRequest(resource.getRequestBuilder()).entity(param).delete(ClientResponse.class); redirectTimes++; } else { // could not find the redirect url, return _logger.error( String.format( "The post request to: %s failed with: %s %s", _url, response.getClientResponseStatus().toString(), response.getEntity(String.class))); throw VNXeException.exceptions.unexpectedDataError( "Got redirect status code, but could not get redirected URL"); } } if (redirectTimes >= VNXeConstants.REDIRECT_MAX) { _logger.error("redirected too many times for the request {} ", _url); throw VNXeException.exceptions.unexpectedDataError( "Redirected too many times while sending the request for " + _url); } checkResponse(response, DELETE_REQUEST); return response; }
@Ignore public void a3_dotUrlRelativeUrl() { // login("admin", "duff", true); final String text = "URL=repo/files/public/children"; stubGetFile(repo, "/public/relUrlTest.url"); stubGetData(repo, "/public/relUrlTest.url", text); stubGetTree(repo, "/public", "relUrlTest.url", "hello/file.txt", "hello/file2.txt", "hello2/"); WebResource webResource = resource(); String response = webResource.path("repos/:public:relUrlTest.url/content").get(String.class); assertEquals("contents of file incorrect/missing", text, response); ClientResponse getResponse = webResource.path("repos/:public:relUrlTest.url/generatedContent").get(ClientResponse.class); assertEquals(ClientResponse.Status.OK, getResponse.getClientResponseStatus()); logout(); }
@Override public CoverageRecord getCoverage( ExtendedFileInfo file, String chromosome, int start, int end, int zoomLevel) { try { MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); queryParams.add("startPos", String.valueOf(start)); queryParams.add("endPos", String.valueOf(end)); queryParams.add("zoomLevel", String.valueOf(zoomLevel)); WebResource resource = getClient() .resource( UriBuilder.fromUri(configuration.getApiRootUri()) .path(file.getHrefCoverage()) .path(chromosome) .build()) .queryParams(queryParams); ClientResponse response = getClient() .resource(resource.getURI()) .accept(MediaType.APPLICATION_XHTML_XML, MediaType.APPLICATION_JSON) .get(ClientResponse.class); switch (response.getClientResponseStatus()) { case OK: String data = response.getEntity(String.class); logger.info(data); CoverageRecord record = mapper.readValue( mapper.readValue(data, JsonNode.class).findPath(RESPONSE).toString(), CoverageRecord.class); logger.info(record.toString()); return record; default: data = response.getEntity(String.class); BasicResponse status = mapper.readValue(data, BasicResponse.class); throw new IllegalArgumentException(status.getResponseStatus().getMessage()); } } catch (BaseSpaceException bs) { throw bs; } catch (Throwable t) { throw new RuntimeException(t); } }
public VNXeCommandResult postRequestSync(ParamBase param) { ClientResponse response = postRequest(param); if (response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) { VNXeCommandResult result = new VNXeCommandResult(); result.setSuccess(true); return result; } String resString = response.getEntity(String.class); _logger.debug("KH API returned: {} ", resString); JSONObject res; String objectString = null; VNXeCommandResult returnedObject = null; try { res = new JSONObject(resString); if (res != null) { JSONObject object = (JSONObject) res.get(VNXeConstants.CONTENT); if (object != null) { objectString = object.toString(); ObjectMapper mapper = new ObjectMapper(); try { returnedObject = mapper.readValue(objectString, VNXeCommandResult.class); returnedObject.setSuccess(true); } catch (JsonParseException e) { _logger.error(String.format("unexpected data returned: %s", objectString), e); throw VNXeException.exceptions.unexpectedDataError( String.format("unexpected data returned: %s", objectString), e); } catch (JsonMappingException e) { _logger.error(String.format("unexpected data returned: %s", objectString), e); throw VNXeException.exceptions.unexpectedDataError( String.format("unexpected data returned: %s", objectString), e); } catch (IOException e) { _logger.error(String.format("unexpected data returned: %s", objectString), e); throw VNXeException.exceptions.unexpectedDataError( String.format("unexpected data returned: %s", objectString), e); } } } } catch (JSONException e) { _logger.error(String.format("unexpected data returned: %s from: %s", resString, _url), e); throw VNXeException.exceptions.unexpectedDataError( String.format("unexpected data returned: %s", objectString), e); } return returnedObject; }
@Override public void waitForAllTasksToStop(final Time timeout, final Time window) { try { Time actualTimeout = timeout; if (actualTimeout == null) { actualTimeout = Time.minutes(1); } Time actualWindow = window; if (actualWindow == null) { actualWindow = Time.seconds(10); } LOG.info( "Waiting for Nexus to not execute any task for {} (timeouts in {})", actualWindow.toString(), actualTimeout.toString()); final MultivaluedMap<String, String> params = new MultivaluedMapImpl(); params.add("timeout", String.valueOf(actualTimeout.toMillis())); params.add("window", String.valueOf(actualWindow.toMillis())); final ClientResponse response = getNexusClient().serviceResource("tasks/waitFor", params).get(ClientResponse.class); response.close(); if (Response.Status.ACCEPTED.getStatusCode() == response.getStatus()) { throw new TasksAreStillRunningException(actualTimeout); } else if (!response .getClientResponseStatus() .getFamily() .equals(Response.Status.Family.SUCCESSFUL)) { throw getNexusClient().convert(new UniformInterfaceException(response)); } } catch (ClientHandlerException e) { throw getNexusClient().convert(e); } catch (UniformInterfaceException e) { throw getNexusClient().convert(e); } }
// test that the exception output also returns XML @Test public void testNodeAppsStateInvalidXML() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp("foo", 1234, 2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { r.path("ws") .path("v1") .path("node") .path("apps") .queryParam("state", "FOO_STATE") .accept(MediaType.APPLICATION_XML) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType()); String msg = response.getEntity(String.class); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(msg)); Document dom = db.parse(is); NodeList nodes = dom.getElementsByTagName("RemoteException"); Element element = (Element) nodes.item(0); String message = WebServicesTestUtils.getXmlString(element, "message"); String type = WebServicesTestUtils.getXmlString(element, "exception"); String classname = WebServicesTestUtils.getXmlString(element, "javaClassName"); verifyStateInvalidException(message, type, classname); } }
@Test public void testTasksQueryInvalid() throws JSONException, Exception { WebResource r = resource(); Map<JobId, Job> jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); // tasktype must be exactly either "m" or "r" String tasktype = "reduce"; try { r.path("ws") .path("v1") .path("history") .path("mapreduce") .path("jobs") .path(jobId) .path("tasks") .queryParam("type", tasktype) .accept(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid uri"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); WebServicesTestUtils.checkStringMatch( "exception message", "java.lang.Exception: tasktype must be either m or r", message); WebServicesTestUtils.checkStringMatch("exception type", "BadRequestException", type); WebServicesTestUtils.checkStringMatch( "exception classname", "org.apache.hadoop.yarn.webapp.BadRequestException", classname); } } }
protected String queryVariantsInternal( ExtendedFileInfo file, String chromosome, VariantFetchParams params) { if (file == null || file.getHrefVariants() == null) { throw new IllegalArgumentException( "null VariantFile or hrefVariants property for VariantFile object"); } try { MultivaluedMap<String, String> queryParams = params == null ? new MultivaluedMapImpl() : params.toMap(); WebResource resource = getClient() .resource( UriBuilder.fromUri(configuration.getApiRootUri()) .path(file.getHrefVariants()) .path("variants") .path(chromosome) .build()) .queryParams(queryParams); ClientResponse response = getClient() .resource(resource.getURI()) .accept(MediaType.APPLICATION_XHTML_XML, MediaType.APPLICATION_JSON) .get(ClientResponse.class); switch (response.getClientResponseStatus()) { case OK: return response.getEntity(String.class); default: BasicResponse status = mapper.readValue(response.getEntity(String.class), BasicResponse.class); throw new IllegalArgumentException(status.getResponseStatus().getMessage()); } } catch (BaseSpaceException bs) { throw bs; } catch (IllegalArgumentException iae) { throw iae; } catch (Throwable t) { throw new RuntimeException(t); } }
@Test public void testTaskIdInvalid() throws JSONException, Exception { WebResource r = resource(); Map<JobId, Job> jobsMap = appContext.getAllJobs(); for (JobId id : jobsMap.keySet()) { String jobId = MRApps.toString(id); String tid = "task_0_0000_d_000000"; try { r.path("ws") .path("v1") .path("history") .path("mapreduce") .path("jobs") .path(jobId) .path("tasks") .path(tid) .get(JSONObject.class); fail("should have thrown exception on invalid uri"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.NOT_FOUND, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); WebServicesTestUtils.checkStringMatch( "exception message", "java.lang.Exception: Bad TaskType identifier. TaskId string : " + "task_0_0000_d_000000 is not properly formed.", message); WebServicesTestUtils.checkStringMatch("exception type", "NotFoundException", type); WebServicesTestUtils.checkStringMatch( "exception classname", "org.apache.hadoop.yarn.webapp.NotFoundException", classname); } } }
@Ignore public void a3_dotUrl() { loginAsRepositoryAdmin(); ITenant systemTenant = tenantManager.createTenant( null, ServerRepositoryPaths.getPentahoRootFolderName(), adminAuthorityName, authenticatedAuthorityName, "Anonymous"); userRoleDao.createUser( systemTenant, sysAdminUserName, "password", "", new String[] {adminAuthorityName}); ITenant mainTenant_1 = tenantManager.createTenant( systemTenant, MAIN_TENANT_1, adminAuthorityName, authenticatedAuthorityName, "Anonymous"); userRoleDao.createUser( mainTenant_1, "admin", "password", "", new String[] {adminAuthorityName}); mp.defineInstance(IUnifiedRepository.class, repo); login("admin", mainTenant_1, new String[] {authenticatedAuthorityName}); String publicFolderPath = ClientRepositoryPaths.getPublicFolderPath(); final String text = "URL=http://google.com"; createTestFile(publicFolderPath.replaceAll("/", ":") + ":" + "test.url", text); WebResource webResource = resource(); String response = webResource.path("repos/:public:test.url/content").get(String.class); assertEquals("contents of file incorrect/missing", text, response); ClientResponse getResponse = webResource.path("repos/:public:test.url/generatedContent").get(ClientResponse.class); assertEquals(ClientResponse.Status.OK, getResponse.getClientResponseStatus()); }
@Test public void testNodeAppsUserEmpty() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp("foo", 1234, 2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { r.path("ws") .path("v1") .path("node") .path("apps") .queryParam("user", "") .accept(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); WebServicesTestUtils.checkStringMatch( "exception message", "java.lang.Exception: Error: You must specify a non-empty string for the user", message); WebServicesTestUtils.checkStringMatch("exception type", "BadRequestException", type); WebServicesTestUtils.checkStringMatch( "exception classname", "org.apache.hadoop.yarn.webapp.BadRequestException", classname); } }
@Test public void testNodeSingleAppsMissing() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp(2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { r.path("ws") .path("v1") .path("node") .path("apps") .path("application_1234_0009") .accept(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.NOT_FOUND, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); WebServicesTestUtils.checkStringMatch( "exception message", "java.lang.Exception: app with id application_1234_0009 not found", message); WebServicesTestUtils.checkStringMatch("exception type", "NotFoundException", type); WebServicesTestUtils.checkStringMatch( "exception classname", "org.apache.hadoop.yarn.webapp.NotFoundException", classname); } }
@Override public CoverageMetaData getCoverageMetaData(ExtendedFileInfo file, String chromosome) { try { WebResource resource = getClient() .resource( UriBuilder.fromUri(configuration.getApiRootUri()) .path(file.getHrefCoverage()) .path(chromosome) .path("meta") .build()); ClientResponse response = getClient() .resource(resource.getURI()) .accept(MediaType.APPLICATION_XHTML_XML, MediaType.APPLICATION_JSON) .get(ClientResponse.class); switch (response.getClientResponseStatus()) { case OK: String data = response.getEntity(String.class); logger.info(data); CoverageMetaData metaData = mapper.readValue( mapper.readValue(data, JsonNode.class).findPath(RESPONSE).toString(), CoverageMetaData.class); logger.info(metaData.toString()); return metaData; default: data = response.getEntity(String.class); BasicResponse status = mapper.readValue(data, BasicResponse.class); throw new IllegalArgumentException(status.getResponseStatus().getMessage()); } } catch (BaseSpaceException bs) { throw bs; } catch (Throwable t) { throw new RuntimeException(t); } }
@Test public void testNodeSingleAppsInvalid() throws JSONException, Exception { WebResource r = resource(); Application app = new MockApp(1); nmContext.getApplications().put(app.getAppId(), app); addAppContainers(app); Application app2 = new MockApp(2); nmContext.getApplications().put(app2.getAppId(), app2); addAppContainers(app2); try { r.path("ws") .path("v1") .path("node") .path("apps") .path("app_foo_0000") .accept(MediaType.APPLICATION_JSON) .get(JSONObject.class); fail("should have thrown exception on invalid user query"); } catch (UniformInterfaceException ue) { ClientResponse response = ue.getResponse(); assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus()); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); JSONObject msg = response.getEntity(JSONObject.class); JSONObject exception = msg.getJSONObject("RemoteException"); assertEquals("incorrect number of elements", 3, exception.length()); String message = exception.getString("message"); String type = exception.getString("exception"); String classname = exception.getString("javaClassName"); WebServicesTestUtils.checkStringMatch( "exception message", "For input string: \"foo\"", message); WebServicesTestUtils.checkStringMatch("exception type", "NumberFormatException", type); WebServicesTestUtils.checkStringMatch( "exception classname", "java.lang.NumberFormatException", classname); } }
/** * IHTSDO authenticate. * * @param username the username * @param password the password * @return the string * @throws Exception the exception */ @SuppressWarnings("unchecked") private String ihtsdoAuthenticate(String username, String password) throws Exception { String ihtsdoSecurityUrl = config.getProperty("ihtsdo.security.url"); // set up request to be posted to ihtsdo security service Form form = new Form(); form.add("username", username); form.add("password", password); form.add("queryName", "getUserByNameAuth"); Client client = Client.create(); WebResource resource = client.resource(ihtsdoSecurityUrl); resource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE); ClientResponse response = resource.post(ClientResponse.class, form); String resultString = ""; if (response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) { resultString = response.getEntity(String.class); } else { // TODO Differentiate error messages with NO RESPONSE and // Authentication Failed (Check text) Logger.getLogger(this.getClass()).info("ERROR! " + response.getStatus()); resultString = response.getEntity(String.class); Logger.getLogger(this.getClass()).info(resultString); throw new LocalException("Incorrect user name or password."); } /* * Synchronize the information sent back from ITHSDO with the MapUser * object. Add a new map user if there isn't one matching the username If * there is, load and update that map user and save the changes */ String ihtsdoUserName = ""; String ihtsdoEmail = ""; String ihtsdoGivenName = ""; String ihtsdoSurname = ""; // converting json to Map byte[] mapData = resultString.getBytes(); Map<String, HashMap<String, String>> jsonMap = new HashMap<>(); // parse username from json object ObjectMapper objectMapper = new ObjectMapper(); jsonMap = objectMapper.readValue(mapData, HashMap.class); for (Entry<String, HashMap<String, String>> entrySet : jsonMap.entrySet()) { if (entrySet.getKey().equals("user")) { HashMap<String, String> innerMap = entrySet.getValue(); for (Entry<String, String> innerEntrySet : innerMap.entrySet()) { if (innerEntrySet.getKey().equals("name")) { ihtsdoUserName = innerEntrySet.getValue(); } else if (innerEntrySet.getKey().equals("email")) { ihtsdoEmail = innerEntrySet.getValue(); } else if (innerEntrySet.getKey().equals("givenName")) { ihtsdoGivenName = innerEntrySet.getValue(); } else if (innerEntrySet.getKey().equals("surname")) { ihtsdoSurname = innerEntrySet.getValue(); } } } } // check if ihtsdo user matches one of our MapUsers MappingService mappingService = new MappingServiceJpa(); MapUserList userList = mappingService.getMapUsers(); MapUser userFound = null; for (MapUser user : userList.getMapUsers()) { if (user.getUserName().equals(ihtsdoUserName)) { userFound = user; break; } } // if MapUser was found, update to match ihtsdo settings if (userFound != null) { userFound.setEmail(ihtsdoEmail); userFound.setName(ihtsdoGivenName + " " + ihtsdoSurname); userFound.setUserName(ihtsdoUserName); mappingService.updateMapUser(userFound); // if MapUser not found, create one for our use } else { MapUser newMapUser = new MapUserJpa(); newMapUser.setName(ihtsdoGivenName + " " + ihtsdoSurname); newMapUser.setUserName(ihtsdoUserName); newMapUser.setEmail(ihtsdoEmail); newMapUser.setApplicationRole(MapUserRole.VIEWER); mappingService.addMapUser(newMapUser); } mappingService.close(); // Generate application-managed token String token = UUID.randomUUID().toString(); tokenUsernameMap.put(token, ihtsdoUserName); tokenLoginMap.put(token, new Date()); Logger.getLogger(this.getClass()).info("User = " + resultString); return token; }
@Override public PublisherConfig process(final PublishStatus status) throws DotPublishingException { if (LicenseUtil.getLevel() < 400) throw new RuntimeException("need an enterprise prime license to run this bundler"); PublishAuditHistory currentStatusHistory = null; try { // Compressing bundle File bundleRoot = BundlerUtil.getBundleRoot(config); ArrayList<File> list = new ArrayList<File>(1); list.add(bundleRoot); File bundle = new File( bundleRoot + File.separator + ".." + File.separator + config.getId() + ".tar.gz"); PushUtils.compressFiles(list, bundle, bundleRoot.getAbsolutePath()); // Retriving enpoints and init client List<PublishingEndPoint> endpoints = ((PushPublisherConfig) config).getEndpoints(); Map<String, List<PublishingEndPoint>> endpointsMap = new HashMap<String, List<PublishingEndPoint>>(); List<PublishingEndPoint> buffer = null; // Organize the endpoints grouping them by groupId for (PublishingEndPoint pEndPoint : endpoints) { String gid = UtilMethods.isSet(pEndPoint.getGroupId()) ? pEndPoint.getGroupId() : pEndPoint.getId(); if (endpointsMap.get(gid) == null) buffer = new ArrayList<PublishingEndPoint>(); else buffer = endpointsMap.get(gid); buffer.add(pEndPoint); // put in map with either the group key or the id if no group is set endpointsMap.put(gid, buffer); } ClientConfig cc = new DefaultClientConfig(); if (Config.getStringProperty("TRUSTSTORE_PATH") != null && !Config.getStringProperty("TRUSTSTORE_PATH").trim().equals("")) cc.getProperties() .put( HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(tFactory.getHostnameVerifier(), tFactory.getSSLContext())); Client client = Client.create(cc); // Updating audit table currentStatusHistory = pubAuditAPI.getPublishAuditStatus(config.getId()).getStatusPojo(); currentStatusHistory.setPublishStart(new Date()); pubAuditAPI.updatePublishAuditStatus( config.getId(), PublishAuditStatus.Status.SENDING_TO_ENDPOINTS, currentStatusHistory); // Increment numTries currentStatusHistory.addNumTries(); boolean hasError = false; int errorCounter = 0; for (String group : endpointsMap.keySet()) { List<PublishingEndPoint> groupList = endpointsMap.get(group); boolean sent = false; for (PublishingEndPoint endpoint : groupList) { EndpointDetail detail = new EndpointDetail(); try { FormDataMultiPart form = new FormDataMultiPart(); form.field( "AUTH_TOKEN", retriveKeyString( PublicEncryptionFactory.decryptString(endpoint.getAuthKey().toString()))); form.field( "GROUP_ID", UtilMethods.isSet(endpoint.getGroupId()) ? endpoint.getGroupId() : endpoint.getId()); form.field("ENDPOINT_ID", endpoint.getId()); form.bodyPart( new FileDataBodyPart("bundle", bundle, MediaType.MULTIPART_FORM_DATA_TYPE)); // Sending bundle to endpoint WebResource resource = client.resource(endpoint.toURL() + "/api/bundlePublisher/publish"); ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); if (response.getClientResponseStatus().getStatusCode() == HttpStatus.SC_OK) { detail.setStatus(PublishAuditStatus.Status.BUNDLE_SENT_SUCCESSFULLY.getCode()); detail.setInfo("Everything ok"); sent = true; } else { detail.setStatus(PublishAuditStatus.Status.FAILED_TO_SENT.getCode()); detail.setInfo( "Returned " + response.getClientResponseStatus().getStatusCode() + " status code " + "for the endpoint " + endpoint.getId() + "with address " + endpoint.getAddress()); } } catch (Exception e) { hasError = true; detail.setStatus(PublishAuditStatus.Status.FAILED_TO_SENT.getCode()); String error = "An error occured for the endpoint " + endpoint.getId() + " with address " + endpoint.getAddress() + ". Error: " + e.getMessage(); detail.setInfo(error); Logger.error(this.getClass(), error); } currentStatusHistory.addOrUpdateEndpoint(group, endpoint.getId(), detail); if (sent) break; } if (!sent) { hasError = true; errorCounter++; } } if (!hasError) { // Updating audit table currentStatusHistory.setPublishEnd(new Date()); pubAuditAPI.updatePublishAuditStatus( config.getId(), PublishAuditStatus.Status.BUNDLE_SENT_SUCCESSFULLY, currentStatusHistory); // Deleting queue records // pubAPI.deleteElementsFromPublishQueueTable(config.getId()); } else { if (errorCounter == endpointsMap.size()) { pubAuditAPI.updatePublishAuditStatus( config.getId(), PublishAuditStatus.Status.FAILED_TO_SEND_TO_ALL_GROUPS, currentStatusHistory); } else { pubAuditAPI.updatePublishAuditStatus( config.getId(), PublishAuditStatus.Status.FAILED_TO_SEND_TO_SOME_GROUPS, currentStatusHistory); } } return config; } catch (Exception e) { // Updating audit table try { pubAuditAPI.updatePublishAuditStatus( config.getId(), PublishAuditStatus.Status.FAILED_TO_PUBLISH, currentStatusHistory); } catch (DotPublisherException e1) { throw new DotPublishingException(e.getMessage()); } Logger.error(this.getClass(), e.getMessage(), e); throw new DotPublishingException(e.getMessage()); } }
private void throwIf404(final ClientResponse response) { if (ClientResponse.Status.NOT_FOUND.equals(response.getClientResponseStatus())) { throw new NexusClientNotFoundException( response.getClientResponseStatus().getReasonPhrase(), getResponseBody(response)); } }