Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WebAPI issues #469

Merged
merged 14 commits into from
Feb 20, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to AET will be documented in this file.
## Unreleased

**List of changes that are finished but not yet released in any final version.**
- [PR-469](https://github.com/Cognifide/aet/pull/469) Fix WebAPI issues([#425](https://github.com/Cognifide/aet/issues/425))
- [PR-439](https://github.com/Cognifide/aet/pull/439) Fixed duplicating line and column numbers in accessibility tab
- [PR-360](https://github.com/Cognifide/aet/pull/360) Keyboard shortcuts for 'Accept/Revert url/test' buttons ([#317](https://github.com/Cognifide/aet/issues/317))
- [PR-432](https://github.com/Cognifide/aet/pull/432) Fixed issue with lack of clear message for erroneous suite definition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ public Artifact getArtifact(DBKey dbKey, String objectID) {

GridFS gfs = getGridFS(dbKey);
BasicDBObject query = new BasicDBObject();
query.put(ID_FIELD_NAME, new ObjectId(objectID));
GridFSDBFile file = gfs.findOne(query);
if (file != null) {
artifact = new Artifact(file.getInputStream(), file.getContentType());
if (ObjectId.isValid(objectID)) {
query.put(ID_FIELD_NAME, new ObjectId(objectID));
GridFSDBFile file = gfs.findOne(query);
if (file != null) {
artifact = new Artifact(file.getInputStream(), file.getContentType());
}
}
return artifact;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ protected void process(DBKey dbKey, HttpServletRequest req, HttpServletResponse
String id = req.getParameter(Helper.ID_PARAM);
resp.setCharacterEncoding("UTF-8");
Artifact artifact = artifactsDAO.getArtifact(dbKey, id);
artifactsDAO.getArtifact(dbKey, id);
if (artifact != null) {
sendArtifact(req, resp, artifact);
} else {
sendErrorMessage(dbKey, resp, id);
sendNotFoundMessage(dbKey, resp, id);
}
}

Expand All @@ -70,12 +69,12 @@ private void sendArtifact(HttpServletRequest req, HttpServletResponse resp, Arti
output.flush();
}

private void sendErrorMessage(DBKey dbKey, HttpServletResponse resp, String id)
private void sendNotFoundMessage(DBKey dbKey, HttpServletResponse resp, String id)
throws IOException {
resp.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
resp.setContentType("application/json");
resp.getWriter().write(
responseAsJson(GSON, "Unable to get artifact with id : %s form %s", id, dbKey.toString()));
responseAsJson(GSON, "Unable to get artifact with id: %s for %s", id, dbKey.toString()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class ConfigsServlet extends HttpServlet {

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigsServlet.class);

private static final Gson GSON = new Gson();

public static final String LOCKS_PARAM = "locks";

public static final String LIST_PARAM = "list";
Expand Down Expand Up @@ -82,7 +84,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) {

if (COMMUNICATION_SETTINGS_PARAM.equals(configType)) {
CommunicationSettings communicationSettings = new CommunicationSettings(reportDomain);
responseWriter.write(new Gson().toJson(communicationSettings));
responseWriter.write(GSON.toJson(communicationSettings));
} else if (LIST_PARAM.equals(configType)) {
resp.setContentType("text/html; charset=utf-8");
resp.setHeader("User-Agent",
Expand Down Expand Up @@ -121,7 +123,7 @@ private void flushResponseBuffer(HttpServletRequest req, HttpServletResponse res
}

private String getLocks() {
return lockService.getAllLocks().toString();
return GSON.toJson(lockService.getAllLocks());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ protected void process(DBKey dbKey, HttpServletRequest req, HttpServletResponse
resp.setContentType("application/json");
resp.getWriter().write(result);
} else {
resp.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
resp.getWriter()
.write(responseAsJson(GSON,"Unable to get Suite Metadata for %s", dbKey.toString()));
createNotFoundResponse(resp, correlationId, suiteName, suiteVersion, dbKey);
}
}

Expand Down Expand Up @@ -192,6 +190,21 @@ private void checkLock(String suiteIdentifier) throws AETException {
}
}

private void createNotFoundResponse(HttpServletResponse response, String correlationId, String suiteName, String suiteVersion, DBKey dbKey) throws IOException {
response.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
String paramsValuesMessage = "";
if (correlationId != null) {
paramsValuesMessage = String.format("correlationId: %s", correlationId);
} else if (suiteName != null) {
paramsValuesMessage = String.format("suite name: %s", suiteName);
if (suiteVersion != null) {
paramsValuesMessage = String.format("%s, version: %s", paramsValuesMessage, suiteVersion);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is everything ok with formatting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me, these are nested if statements. It's OK according to Google Style

}
}
response.getWriter()
.write(responseAsJson(GSON, "Unable to get Suite Metadata with %s for %s", paramsValuesMessage, dbKey.toString()));
}

@Activate
public void start() {
register(Helper.getMetadataPath());
Expand Down