Skip to content

Commit

Permalink
O3-310: simple code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsereko committed Jan 4, 2024
1 parent 04da905 commit bfe52a7
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.openmrs.util.OpenmrsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.openmrs.module.ModuleException;
Expand All @@ -44,11 +44,11 @@
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/frontend/config.json")
public class FrontendJsonConfigController2_4 extends BaseRestController {

public static final String DEFAULT_FRONTEND_DIRECTORY = "frontend";
public static final String GP_LOCAL_DIRECTORY = "spa.local.directory";
private static final String DEFAULT_FRONTEND_DIRECTORY = "frontend";
private static final String GP_LOCAL_DIRECTORY = "spa.local.directory";
private static final String JSON_CONFIG_FILE_NAME = "config.json";

private Log log = LogFactory.getLog(this.getClass());
private static final Logger log = LoggerFactory.getLogger(FrontendJsonConfigController2_4.class);

@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
Expand Down Expand Up @@ -83,14 +83,15 @@ private void saveJsonConfigFile(HttpServletRequest request, HttpServletResponse
}
String requestBody = stringBuilder.toString();

new ObjectMapper().readTree(requestBody); // verify that is in a valid JSON format
// verify that is in a valid JSON format
new ObjectMapper().readTree(requestBody);

InputStream inputStream = new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8));
OutputStream outStream = Files.newOutputStream(jsonConfigFile.toPath());
OpenmrsUtil.copyFile(inputStream, outStream);

if (jsonConfigFile.exists()) {
log.debug("file: '{" + jsonConfigFile.getAbsolutePath() + "}' written successfully");
log.debug("file: '{}' written successfully", jsonConfigFile.getAbsolutePath());
response.setStatus(HttpServletResponse.SC_OK);
}
} catch (JsonProcessingException e) {
Expand All @@ -115,10 +116,8 @@ private boolean isValidAuthFormat(HttpServletResponse response, String basicAuth

String[] userAndPass = decoded.split(":");
Context.authenticate(new UsernamePasswordCredentials(userAndPass[0], userAndPass[1]));
log.debug("authenticated [{" + userAndPass[0] + "}]");
log.debug("authenticated [{}]", userAndPass[0]);
} catch (Exception ex) {
// This filter never stops execution. If the user failed to
// authenticate, that will be caught later.
log.debug("authentication exception ", ex);
}
}
Expand All @@ -128,9 +127,9 @@ private boolean isValidAuthFormat(HttpServletResponse response, String basicAuth
private File getJsonConfigFile() {
File folder = getSpaStaticFilesDir();
if (!folder.isDirectory()) {
log.warn("SPA frontend repository is not a directory hence creating it at: " + folder.getAbsolutePath());
log.warn("Unable to find the OpenMRS SPA module frontend directory hence creating it at: " + folder.getAbsolutePath());
if (!folder.mkdirs()) {
throw new ModuleException("Failed to create the SPA frontend repository folder at: " + folder.getAbsolutePath());
throw new ModuleException("Failed to create the OpenMRS SPA module frontend directory at: " + folder.getAbsolutePath());
}
}
return new File(folder.getAbsolutePath(), JSON_CONFIG_FILE_NAME);
Expand All @@ -144,8 +143,7 @@ private File getSpaStaticFilesDir() {
File folder = new File(folderName);

// if the property wasn't a full path already, assume it was intended to be a
// folder in the
// application directory
// folder in the application directory
if (!folder.exists()) {
folder = new File(OpenmrsUtil.getApplicationDataDirectory(), folderName);
}
Expand Down

0 comments on commit bfe52a7

Please sign in to comment.