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

Openrewrite/normalised whitespace #10740

Closed
wants to merge 13 commits into from
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public interface XMLExporter extends Exporter {
/**
* @return - should always be MediaType.APPLICATION_XML
*/
@Override
public default String getMediaType() {
return MediaType.APPLICATION_XML;
};
}
}
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,26 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.37.1</version>
<configuration>
<exportDatatables>true</exportDatatables>
<activeRecipes>
<recipe>org.openrewrite.java.format.Spaces</recipe>
<recipe>org.openrewrite.java.format.RemoveTrailingWhitespace</recipe>
<recipe>org.openrewrite.java.format.BlankLines</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-logging-frameworks</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,44 @@ public class ChunkingOutputStream extends FilterOutputStream {
private static final int BUFFER_SIZE = 8192;
private static final byte[] CHUNK_CLOSE = "\r\n".getBytes();
private static final String CHUNK_SIZE_FORMAT = "%x\r\n";

private byte[] buffer = new byte[BUFFER_SIZE];
private int position = 0;
private int position = 0;

public ChunkingOutputStream(OutputStream out) {
super(out);
}

@Override
public void write(byte[] data) throws IOException {
this.write(data, 0, data.length);
}

@Override
public void write(byte[] data, int offset, int length) throws IOException {

// is this going to fill the buffer?
if (position + length > BUFFER_SIZE) {
System.arraycopy(data, offset, buffer, position, BUFFER_SIZE - position);
offset += (BUFFER_SIZE - position);
length -= (BUFFER_SIZE - position);
dumpChunk(buffer, 0, BUFFER_SIZE);
position = 0;
position = 0;
}

// are there still multiple buffer-worths of bytes?
while (length > BUFFER_SIZE) {
dumpChunk(data, offset, BUFFER_SIZE);
offset += BUFFER_SIZE;
length -= BUFFER_SIZE;
}

// finally, buffer the leftover bytes:
System.arraycopy(data, offset, buffer, position, length);
position+=length;
position += length;

}

@Override
public void write(int i) throws IOException {
// Hopefully ZipOutputStream never writes single bytes into the stream?
Expand All @@ -80,24 +80,24 @@ public void write(int i) throws IOException {
dumpChunk(buffer, 0, position);
position = 0;
}
buffer[position++] = (byte)i;
buffer[position++] = (byte) i;
}

@Override
public void close() throws IOException {
if (position > 0) {
dumpChunk(buffer, 0, position);
}

// ... and the final, "zero chunk":
super.out.write('0');
super.out.write(CHUNK_CLOSE);
super.out.write(CHUNK_CLOSE);

super.out.close();
}


private void dumpChunk(byte[] data, int offset, int length) throws IOException {
String chunkSizeLine = String.format(CHUNK_SIZE_FORMAT, length);
super.out.write(chunkSizeLine.getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@
*
* @author Leonid Andreev
*/
public class ZipDownloadService {
public class ZipDownloadService {

private static String jobKey = null;
private List<String[]> jobFiles = null;
private boolean zipOnly = false;
private DirectAccessUtil directAccessUtil = null;
private boolean zipOnly = false;

private DirectAccessUtil directAccessUtil = null;
private ZipOutputStream zipOutputStream = null;

public static void main(String args[]) throws Exception {

ZipDownloadService zipperService = new ZipDownloadService();

if (!zipperService.parseArgs(args)) {
zipperService.usage();
return;
return;
}

zipperService.parseCgiQueryParameters();

zipperService.execute(jobKey);
}

Expand All @@ -72,87 +72,87 @@ private static void usage() {
public boolean parseArgs(String[] args) {

if (args == null || args.length == 0) {
return true;
return true;
} else if (args.length == 1) {
if (args[0].equals("-ziponly")) {
this.zipOnly = true;
return true;
}
}
return false;

return false;
}

// Does not support any parameters, except the job-identifying token key,
// supplied as the entire query string.
public void parseCgiQueryParameters() {
String queryString = System.getenv().get("QUERY_STRING");
if (queryString != null) {
jobKey = queryString;
jobKey = queryString;
}
}

public void print404() {
System.out.println("Status: 404 Not Found\r");
System.out.println("Content-Type: text/html\r");
System.out.println("\r");

System.out.println("<h1>404 No such download job!</h1>");
}

public void printZipHeader() {
System.out.println("Content-disposition: attachment; filename=\"dataverse_files.zip\"\r");
System.out.println("Content-Type: application/zip; name=\"dataverse_files.zip\"\r");
System.out.println("Transfer-Encoding: chunked\r");
System.out.println("\r");
System.out.flush();
}

public void execute(String key) {
jobFiles = lookupZipJob(key);

jobFiles = lookupZipJob(key);

if (jobFiles == null || jobFiles.size() == 0) {
this.print404();
System.exit(0);
}

this.processFiles();
}

public void processFiles() {

if (!this.zipOnly) {
this.printZipHeader();
}

Set<String> zippedFolders = new HashSet<>();
Set<String> fileNamesList = new HashSet<>();

for (String [] fileEntry : jobFiles) {
String storageLocation = fileEntry[0];
String fileName = fileEntry[1];

//System.out.println(storageLocation + ":" + fileName);

if (this.zipOutputStream == null) {
openZipStream();
}

if (this.directAccessUtil == null) {
this.directAccessUtil = new DirectAccessUtil();
}

InputStream inputStream = this.directAccessUtil.openDirectAccess(storageLocation);

String zipEntryName = checkZipEntryName(fileName, fileNamesList);
// this may not be needed anymore - some extra sanitizing of the file
// name we used to have to do - since all the values in a current Dataverse
// database may already be santized enough.
// (Edit: Yes, we still need this - there are still datasets with multiple
// files with duplicate names; this method takes care of that)
if (inputStream != null && this.zipOutputStream != null) {

ZipEntry entry = new ZipEntry(zipEntryName);

byte[] bytes = new byte[2 * 8192];
Expand All @@ -179,18 +179,18 @@ public void processFiles() {
throw new IOException("Byte size mismatch: expected " + fileSize + ", read: " + readSize);
}*/
} catch (IOException ioex) {
System.err.println("Failed to compress "+storageLocation);
System.err.println("Failed to compress " + storageLocation);
} finally {
try {
inputStream.close();
} catch (IOException ioexIgnore) {
System.err.println("Warning: IO exception trying to close input stream - "+storageLocation);
System.err.println("Warning: IO exception trying to close input stream - " + storageLocation);
}
}
} else {
System.err.println("Failed to access "+storageLocation);
System.err.println("Failed to access " + storageLocation);
}

}
try {
this.zipOutputStream.flush();
Expand All @@ -201,7 +201,7 @@ public void processFiles() {
} catch (Exception e) {
}
}

public void openZipStream() {
if (this.zipOutputStream == null) {
if (this.zipOnly) {
Expand All @@ -211,14 +211,14 @@ public void openZipStream() {
}
}
}

private boolean hasFolder(String fileName) {
if (fileName == null) {
return false;
}
return fileName.indexOf('/') >= 0;
}

private String getFolderName(String fileName) {
if (fileName == null) {
return "";
Expand All @@ -232,7 +232,7 @@ private String getFolderName(String fileName) {
}
return folderName;
}

private void addFolderToZipStream(String folderName, Set<String> zippedFolders) throws IOException {
// We don't want to create folders in the output Zip file that have
// already been added:
Expand All @@ -245,7 +245,7 @@ private void addFolderToZipStream(String folderName, Set<String> zippedFolders)
}
}
}

// check for and process duplicates:
private String checkZipEntryName(String originalName, Set<String> fileNames) {
String name = originalName;
Expand Down
Loading
Loading