Skip to content

Commit

Permalink
Fix a few Sonarcloud issues in AuditManager
Browse files Browse the repository at this point in the history
  • Loading branch information
cbosdo committed Oct 20, 2023
1 parent e7cdce6 commit 3960957
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 117 deletions.
64 changes: 40 additions & 24 deletions java/code/src/com/redhat/rhn/common/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package com.redhat.rhn.common.util;

import com.redhat.rhn.common.RhnRuntimeException;

import org.apache.commons.collections.buffer.CircularFifoBuffer;
import org.apache.commons.io.LineIterator;
import org.apache.logging.log4j.LogManager;
Expand All @@ -39,6 +41,8 @@
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.Arrays;
import java.util.List;
import java.util.Set;


Expand Down Expand Up @@ -66,7 +70,7 @@ public static void writeStringToFile(String contents, String path) {
try {
File file = new File(path);
if (file.exists()) {
file.delete();
Files.delete(file.toPath());
}
file.createNewFile();
try (FileOutputStream fos = new FileOutputStream(file);
Expand All @@ -80,7 +84,7 @@ public static void writeStringToFile(String contents, String path) {
}
catch (Exception e) {
log.error("Error trying to write file to disk: [{}]", path, e);
throw new RuntimeException(e);
throw new RhnRuntimeException(e);
}
}

Expand Down Expand Up @@ -145,10 +149,10 @@ public static String readStringFromFile(String path, boolean noLog) {
return contents;
}
catch (FileNotFoundException e) {
throw new RuntimeException("File not found: " + path);
throw new RhnRuntimeException("File not found: " + path);
}
catch (IOException e) {
throw new RuntimeException(e);
throw new RhnRuntimeException(e);
}
}

Expand All @@ -169,9 +173,7 @@ public static byte[] readByteArrayFromFile(File fileToRead, long start, long end
log.debug("size of array: {}", size);
// Create the byte array to hold the data
byte[] bytes = new byte[size];
InputStream is = null;
try {
is = new FileInputStream(fileToRead);
try (InputStream is = new FileInputStream(fileToRead)){
// Read in the bytes
int offset = 0;
int numRead = 0;
Expand All @@ -187,17 +189,7 @@ public static byte[] readByteArrayFromFile(File fileToRead, long start, long end
}
catch (IOException fnf) {
log.error("Could not read from: {}", fileToRead.getAbsolutePath());
throw new RuntimeException(fnf);
}
finally {
if (is != null) {
try {
is.close();
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
throw new RhnRuntimeException(fnf);
}
return bytes;
}
Expand All @@ -209,10 +201,8 @@ public static byte[] readByteArrayFromFile(File fileToRead, long start, long end
* @return tail of file as string
*/
public static String getTailOfFile(String pathToFile, Integer lines) {
InputStream fileStream = null;
CircularFifoBuffer buffer = new CircularFifoBuffer(lines);
try {
fileStream = new FileInputStream(pathToFile);
try (InputStream fileStream = new FileInputStream(pathToFile)) {
LineIterator it = org.apache.commons.io.IOUtils.lineIterator(fileStream,
(String) null);
while (it.hasNext()) {
Expand All @@ -221,10 +211,11 @@ public static String getTailOfFile(String pathToFile, Integer lines) {
}
catch (FileNotFoundException e) {
log.error("File not found: {}", pathToFile);
throw new RuntimeException(e);
throw new RhnRuntimeException(e);
}
finally {
org.apache.commons.io.IOUtils.closeQuietly(fileStream);
catch (IOException e) {
log.error(String.format("Failed to close file %s", pathToFile));
throw new RhnRuntimeException(e);
}
// Construct a string from the buffered lines
StringBuilder sb = new StringBuilder();
Expand All @@ -247,4 +238,29 @@ public static void deleteFile(Path path) {
log.warn("Could not delete file: {}", path, e);
}
}

/**
* Get the content of a folder
*
* @param path the path of the folder
* @return the content of the folder
*/
public static List<String> listFolder(Path path) {
try {
File dir = path.toFile().getCanonicalFile();

if (!dir.exists()) {
return List.of();
}

String[] files = dir.list();
if (files == null) {
throw new RhnRuntimeException("log path doesn't point to a directory: " + path);
}
return Arrays.asList(files);
}
catch (IOException e) {
throw new RhnRuntimeException("Invalid path " + path, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.redhat.rhn.common.util.DatePicker;
import com.redhat.rhn.frontend.action.common.DateRangePicker;
import com.redhat.rhn.frontend.dto.AuditDto;
import com.redhat.rhn.frontend.dto.AuditReviewDto;
import com.redhat.rhn.frontend.struts.RequestContext;
import com.redhat.rhn.frontend.struts.RhnAction;
Expand Down Expand Up @@ -98,7 +99,7 @@ private Long processEndMilli(DynaActionForm dform,
private DateRangePicker.DatePickerResults processTimeArgs(
DynaActionForm dform,
HttpServletRequest request,
Boolean processDates) {
boolean processDates) {
Date start, end;
DateRangePicker drp = new DateRangePicker(dform, request,
new Date(processStartMilli(dform, request)),
Expand All @@ -108,7 +109,7 @@ private DateRangePicker.DatePickerResults processTimeArgs(
DateRangePicker.DatePickerResults dpresults =
drp.processDatePickers(processDates, false);

if (processDates) { // we need to redo {start,end}{Disp,Milli}
if (processDates) {
start = dpresults.getStart().getDate();
end = dpresults.getEnd().getDate();
request.setAttribute("startDisp", start.toString());
Expand Down Expand Up @@ -162,13 +163,13 @@ public ActionForward execute(ActionMapping mapping,
HttpServletResponse response) {
ActionMessages amsgs;
AuditReviewDto aureview;
Boolean parseDates, submitted, unrev;
boolean parseDates, submitted, unrev;
DateRangePicker.DatePickerResults dpresults;
DynaActionForm dform = (DynaActionForm)form;
HttpSession session = request.getSession(true);
JSONWriter jsonwr = new JSONWriter();
List<String> result = null;
Long start, end, seqno, cacheSeqno;
List<AuditDto> result = null;
long start, end;
Map<String, String[]> typemap;
RequestContext requestContext = new RequestContext(request);
String machine;
Expand All @@ -182,13 +183,13 @@ public ActionForward execute(ActionMapping mapping,
// what machine are we looking at?
machine = dform.getString("machine");
// should we look at the DatePickers?
parseDates = (Boolean)dform.get("parseDates") != null;
parseDates = dform.get("parseDates") != null;
// did we receive a form with some checkboxes checked?
submitted = (autypes != null && autypes.length > 0);
// can we mark this section reviewed?
unrev = (Boolean)dform.get("unreviewable") != null;
unrev = dform.get("unreviewable") != null;
// get the "page creation time" to determine cache usage
seqno = (Long)dform.get("seqno");
Long seqno = (Long)dform.get("seqno");

if (seqno == null) {
log.debug("(re-)initializing cache");
Expand Down Expand Up @@ -223,7 +224,7 @@ else if (!submitted && request.getAttribute("machine") != null) {
start = dpresults.getStart().getDate().getTime();
end = dpresults.getEnd().getDate().getTime();

cacheSeqno = (Long)session.getAttribute("auditCacheSeqno");
Long cacheSeqno = (Long)session.getAttribute("auditCacheSeqno");

// if the cached seqno is greater or equal to the seqno the browser
// sent, we've seen it before; do a new search
Expand All @@ -237,7 +238,7 @@ else if (!submitted && request.getAttribute("machine") != null) {
else {
log.debug("using cached result");
// may be null (indicates the cached result was null)
result = (List)session.getAttribute("auditResultCache");
result = (List<AuditDto>)session.getAttribute("auditResultCache");
}

if (result == null) {
Expand Down
3 changes: 2 additions & 1 deletion java/code/src/com/redhat/rhn/frontend/dto/AuditDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
*/
package com.redhat.rhn.frontend.dto;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* AuditDto
*/
public class AuditDto extends BaseDto {
public class AuditDto extends BaseDto implements Serializable {
private Long id;
private int serial;
private Date time;
Expand Down
Loading

0 comments on commit 3960957

Please sign in to comment.