-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #791 from akto-api-security/feature/postman-har-files
Added changes to save files
- Loading branch information
Showing
5 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
apps/dashboard/src/main/java/com/akto/utils/GzipUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.akto.utils; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.zip.GZIPInputStream; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
public class GzipUtils { | ||
|
||
public static String zipString(String input) { | ||
if (input == null || input.isEmpty()) { | ||
return input; | ||
} | ||
|
||
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) { | ||
gzipOutputStream.write(input.getBytes(StandardCharsets.UTF_8)); | ||
gzipOutputStream.close(); | ||
return Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to zip content", e); | ||
} | ||
} | ||
|
||
public static String unzipString(String zippedBase64Str) { | ||
if (zippedBase64Str == null || zippedBase64Str.isEmpty()) { | ||
return zippedBase64Str; | ||
} | ||
|
||
byte[] decodedBytes = Base64.getDecoder().decode(zippedBase64Str); | ||
|
||
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decodedBytes); | ||
GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream); | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { | ||
|
||
byte[] buffer = new byte[1024]; | ||
int len; | ||
while ((len = gzipInputStream.read(buffer)) != -1) { | ||
byteArrayOutputStream.write(buffer, 0, len); | ||
} | ||
return byteArrayOutputStream.toString(StandardCharsets.UTF_8.name()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to unzip content", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.akto.dao.file; | ||
|
||
import com.akto.dao.AccountsContextDao; | ||
import com.akto.dto.files.File; | ||
|
||
public class FilesDao extends AccountsContextDao<File> { | ||
|
||
private FilesDao() { | ||
} | ||
|
||
public static final FilesDao instance = new FilesDao(); | ||
@Override | ||
public String getCollName() { | ||
return "files"; | ||
} | ||
|
||
@Override | ||
public Class<File> getClassT() { | ||
return File.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.akto.dto.files; | ||
|
||
import com.akto.dao.context.Context; | ||
import com.akto.dto.HttpResponseParams; | ||
import org.bson.codecs.pojo.annotations.BsonId; | ||
import org.bson.types.ObjectId; | ||
|
||
public class File { | ||
|
||
@BsonId | ||
private ObjectId id; | ||
|
||
private HttpResponseParams.Source source; | ||
|
||
private int uploadTimestamp; | ||
|
||
private String compressedContent; | ||
|
||
public File(HttpResponseParams.Source source, String compressedContent) { | ||
this.source = source; | ||
this.compressedContent = compressedContent; | ||
this.uploadTimestamp = Context.now(); | ||
} | ||
|
||
public File(ObjectId id, HttpResponseParams.Source source, int uploadTimestamp, String compressedContent) { | ||
this.id = id; | ||
this.source = source; | ||
this.uploadTimestamp = uploadTimestamp; | ||
this.compressedContent = compressedContent; | ||
} | ||
|
||
public ObjectId getId() { | ||
return id; | ||
} | ||
|
||
public void setId(ObjectId id) { | ||
this.id = id; | ||
} | ||
|
||
public int getUploadTimestamp() { | ||
return uploadTimestamp; | ||
} | ||
|
||
public void setUploadTimestamp(int uploadTimestamp) { | ||
this.uploadTimestamp = uploadTimestamp; | ||
} | ||
|
||
public String getCompressedContent() { | ||
return compressedContent; | ||
} | ||
|
||
public void setCompressedContent(String compressedContent) { | ||
this.compressedContent = compressedContent; | ||
} | ||
|
||
public HttpResponseParams.Source getSource() { | ||
return source; | ||
} | ||
|
||
public void setSource(HttpResponseParams.Source source) { | ||
this.source = source; | ||
} | ||
} |