Skip to content

Commit 4835ab3

Browse files
Merge pull request #74 from aquality-automation/feature/attachments
Update backend attachments
2 parents 1d8e049 + 7aea571 commit 4835ab3

File tree

10 files changed

+248
-60
lines changed

10 files changed

+248
-60
lines changed

src/main/java/main/World.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main;
2+
3+
import lombok.Getter;
4+
import main.exceptions.AqualityException;
5+
import main.model.db.dao.settings.EmailSettingsDao;
6+
import main.model.dto.settings.EmailSettingsDto;
7+
8+
public class World {
9+
10+
private static World instance;
11+
12+
@Getter
13+
private String baseURL;
14+
15+
16+
private World() {
17+
EmailSettingsDao emailSettingsDao = new EmailSettingsDao();
18+
EmailSettingsDto settings;
19+
try {
20+
settings = emailSettingsDao.getEntityById(1);
21+
baseURL = settings.getBase_url();
22+
} catch (AqualityException e) {
23+
e.printStackTrace();
24+
System.out.println("World cannot be initialized!");
25+
}
26+
}
27+
28+
public static World getInstance() {
29+
if (instance == null) {
30+
instance = new World();
31+
}
32+
return instance;
33+
}
34+
35+
public static World updateInstance() {
36+
instance = new World();
37+
return instance;
38+
}
39+
}

src/main/java/main/controllers/Administration/AppSettingsController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package main.controllers.Administration;
22

3+
import main.World;
34
import main.controllers.BaseController;
45
import main.exceptions.AqualityException;
56
import main.exceptions.AqualityPermissionsException;
@@ -52,7 +53,9 @@ public LdapDto create(LdapDto template) throws AqualityException {
5253
@Override
5354
public AppSettingsDto create(AppSettingsDto template) throws AqualityException {
5455
if (baseUser.isAdmin()) {
55-
return appSettingsDao.create(template);
56+
AppSettingsDto appSettingsDto = appSettingsDao.create(template);
57+
World.updateInstance();
58+
return appSettingsDto;
5659
} else {
5760
throw new AqualityPermissionsException("Account is not allowed to update Application Settings", baseUser);
5861
}

src/main/java/main/controllers/Project/ResultController.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.util.ArrayList;
1313
import java.util.List;
14+
import java.util.stream.Collectors;
1415

1516
public class ResultController extends BaseController<TestResultDto> {
1617
private final TestResultDao testResultDao;
@@ -153,20 +154,24 @@ private List<TestResultDto> fillResults(List<TestResultDto> results) throws Aqua
153154
testTemplate.setProject_id(projectId);
154155
List<TestDto> tests = testController.get(testTemplate);
155156

157+
TestResultAttachmentDto testResultAttachmentTemplate = new TestResultAttachmentDto();
158+
testResultAttachmentTemplate.setProject_id(projectId);
159+
List<TestResultAttachmentDto> testResultAttachments = get(testResultAttachmentTemplate);
160+
156161
ProjectUserDto projectUserDto = new ProjectUserDto();
157162
projectUserDto.setProject_id(projectId);
158163

159164
boolean isStepsEnabled = projectController.isStepsEnabled(projectId);
160165

161166
for (TestResultDto result : results) {
162-
fillResult(result, finalResults, tests, issues, isStepsEnabled);
167+
fillResult(result, finalResults, tests, issues, testResultAttachments, isStepsEnabled);
163168
}
164169
}
165170

166171
return results;
167172
}
168173

169-
private void fillResult(TestResultDto result, List<FinalResultDto> finalResults, List<TestDto> tests, List<IssueDto> issues, boolean isStepsEnabled) throws AqualityException {
174+
private void fillResult(TestResultDto result, List<FinalResultDto> finalResults, List<TestDto> tests, List<IssueDto> issues, List<TestResultAttachmentDto> attachments, boolean isStepsEnabled) throws AqualityException {
170175
if (isStepsEnabled) {
171176
fillResultSteps(result);
172177
}
@@ -176,6 +181,7 @@ private void fillResult(TestResultDto result, List<FinalResultDto> finalResults,
176181
if(result.getIssue_id() != null) {
177182
result.setIssue(issues.stream().filter(x -> x.getId().equals(result.getIssue_id())).findFirst().orElse(null));
178183
}
184+
result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())).collect(Collectors.toList()));
179185
}
180186

181187
private void fillResultSteps(TestResultDto result) throws AqualityException {

src/main/java/main/model/db/dao/project/TestResultAttachmentDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class TestResultAttachmentDao extends DAO<TestResultAttachmentDto> {
77
public TestResultAttachmentDao() {
88
super(TestResultAttachmentDto.class);
9-
select = "{call SELECT_TEST_RESULT_ATTACH(?,?)}";
9+
select = "{call SELECT_TEST_RESULT_ATTACH(?,?,?,?)}";
1010
insert = "{call INSERT_TEST_RESULT_ATTACH(?,?,?)}";
1111
remove = "{call REMOVE_TEST_RESULT_ATTACH(?)}";
1212
}

src/main/java/main/model/dto/project/TestResultAttachmentDto.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.Data;
44
import lombok.EqualsAndHashCode;
5+
import main.annotations.DataBaseID;
56
import main.annotations.DataBaseInsert;
67
import main.annotations.DataBaseName;
78
import main.annotations.DataBaseSearchable;
@@ -15,6 +16,7 @@ public class TestResultAttachmentDto extends AttachmentDto {
1516
@DataBaseInsert
1617
private Integer test_result_id;
1718

19+
@DataBaseID
1820
@DataBaseName(name = "request_project_id")
1921
@DataBaseSearchable
2022
@DataBaseInsert
@@ -23,4 +25,10 @@ public class TestResultAttachmentDto extends AttachmentDto {
2325
@DataBaseName(name = "request_test_run_id")
2426
@DataBaseSearchable
2527
private Integer test_run_id;
28+
29+
@DataBaseID
30+
@DataBaseName(name = "request_id")
31+
private Integer id;
32+
33+
private String url;
2634
}

src/main/java/main/model/dto/project/TestResultDto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@ public class TestResultDto extends BaseDto {
7474
@DataBaseInsert
7575
private Integer issue_id;
7676
private IssueDto issue;
77+
private List<TestResultAttachmentDto> attachments;
7778
}

src/main/java/main/view/BaseServlet.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main.view;
22

33
import main.Session;
4+
import main.World;
45
import main.exceptions.AqualityException;
56
import main.exceptions.AqualityQueryParameterException;
67
import main.model.dto.DtoMapperGeneral;
@@ -9,11 +10,13 @@
910

1011
import javax.naming.AuthenticationException;
1112
import javax.servlet.ServletContext;
13+
import javax.servlet.http.Cookie;
1214
import javax.servlet.http.HttpServlet;
1315
import javax.servlet.http.HttpServletRequest;
1416
import javax.servlet.http.HttpServletResponse;
1517
import java.io.*;
1618
import java.net.URLDecoder;
19+
import java.util.Arrays;
1720
import java.util.Objects;
1821
import java.util.logging.Logger;
1922

@@ -25,6 +28,7 @@ public class BaseServlet extends HttpServlet {
2528
protected DtoMapperGeneral mapper = new DtoMapperGeneral();
2629
protected static final String PROJECT_ID_KEY = "project_id";
2730

31+
2832
protected Session createSession(HttpServletRequest req) throws AqualityException, AuthenticationException {
2933
return new Session(getSessionId(req));
3034
}
@@ -82,7 +86,7 @@ protected Boolean getBooleanQueryParameter(@NotNull HttpServletRequest req, Stri
8286

8387
protected void setPostResponseHeaders(@NotNull HttpServletResponse resp) {
8488
resp.addHeader("Access-Control-Allow-Methods", "POST");
85-
resp.addHeader("Access-Control-Allow-Origin", "*");
89+
resp.addHeader("Access-Control-Allow-Origin", getOrigin());
8690
resp.addHeader("Access-Control-Allow-Headers", "Authorization");
8791
}
8892

@@ -96,23 +100,30 @@ protected void setJSONContentType(@NotNull HttpServletResponse resp) {
96100

97101
protected void setDeleteResponseHeaders(@NotNull HttpServletResponse resp) {
98102
resp.addHeader("Access-Control-Allow-Methods", "DELETE, POST");
99-
resp.addHeader("Access-Control-Allow-Origin", "*");
103+
resp.addHeader("Access-Control-Allow-Credentials", "true");
104+
resp.addHeader("Access-Control-Allow-Origin", getOrigin());
100105
resp.addHeader("Access-Control-Allow-Headers", "Authorization");
101106
}
102107

103108
protected void setGetResponseHeaders(@NotNull HttpServletResponse resp) {
104109
resp.addHeader("Access-Control-Allow-Methods", "GET");
105-
resp.addHeader("Access-Control-Allow-Origin", "*");
110+
resp.addHeader("Access-Control-Allow-Credentials", "true");
111+
resp.addHeader("Access-Control-Allow-Origin", getOrigin());
106112
resp.addHeader("Access-Control-Allow-Headers", "Authorization");
107113
}
108114

109115
protected void setOptionsResponseHeaders(@NotNull HttpServletResponse resp) {
116+
resp.addHeader("Access-Control-Allow-Credentials", "true");
110117
resp.addHeader("Access-Control-Allow-Methods", "OPTIONS, DELETE, POST, GET, PUT");
111-
resp.addHeader("Access-Control-Allow-Origin", "*");
118+
resp.addHeader("Access-Control-Allow-Origin",getOrigin());
112119
resp.addHeader("Access-Control-Allow-Headers", "Authorization, content-type");
113120
resp.setStatus(204);
114121
}
115122

123+
private String getOrigin(){
124+
return World.getInstance().getBaseURL() != null ? World.getInstance().getBaseURL() : "*";
125+
}
126+
116127
private void setAuthorizationProblem(@NotNull HttpServletResponse resp, @NotNull Exception e) throws AqualityException {
117128
resp.setStatus(401);
118129
setResponseBody(resp, !Objects.equals(e.getMessage(), "") ? e.getMessage() : "Are you sure you logged in?");
@@ -124,10 +135,20 @@ protected void setAuthorizationProblem(@NotNull HttpServletResponse resp) throws
124135

125136
private String getSessionId(@NotNull HttpServletRequest req) throws AqualityException, AuthenticationException {
126137
String header = req.getHeader("Authorization");
138+
Cookie[] cookies = req.getCookies();
127139
if (header != null) {
128140
validateAuthHeader(header);
129141
String[] strings = header.split(" ");
130142
return strings[1];
143+
} else if(cookies != null){
144+
Cookie iio78 = Arrays.stream(cookies).filter(x -> x.getName().equals("iio78")).findFirst().orElse(null);
145+
if(iio78 != null) {
146+
try{
147+
return URLDecoder.decode(iio78.getValue(), "utf-8");
148+
} catch (UnsupportedEncodingException e){
149+
throw new AuthenticationException("Your cookie is wrong!");
150+
}
151+
}
131152
}
132153
throw new AuthenticationException("You've missed your authorization header!");
133154
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package main.view.Project;
2+
3+
import main.Session;
4+
import main.exceptions.AqualityParametersException;
5+
import main.model.dto.project.TestResultAttachmentDto;
6+
import main.model.dto.project.TestResultDto;
7+
import main.utils.FileUtils;
8+
import main.utils.PathUtils;
9+
import main.view.BaseServlet;
10+
11+
import javax.servlet.ServletContext;
12+
import javax.servlet.annotation.MultipartConfig;
13+
import javax.servlet.annotation.WebServlet;
14+
import javax.servlet.http.HttpServletRequest;
15+
import javax.servlet.http.HttpServletResponse;
16+
import java.io.File;
17+
import java.io.FileInputStream;
18+
import java.io.OutputStream;
19+
import java.nio.charset.StandardCharsets;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
@WebServlet("/testresult/attachment")
24+
@MultipartConfig
25+
public class TestResultAttachmentServlet extends BaseServlet {
26+
27+
@Override
28+
public void doGet(HttpServletRequest req, HttpServletResponse resp){
29+
setGetResponseHeaders(resp);
30+
ServletContext context= req.getServletContext();
31+
try {
32+
Session session = createSession(req);
33+
TestResultAttachmentDto testResultAttachmentDto = new TestResultAttachmentDto();
34+
testResultAttachmentDto.getSearchTemplateFromRequestParameters(req);
35+
List<TestResultAttachmentDto> attachments = session.controllerFactory.getHandler(new TestResultDto()).get(testResultAttachmentDto);
36+
if (attachments.size() > 0) {
37+
TestResultAttachmentDto attachment = attachments.get(0);
38+
String mime = context.getMimeType(attachment.getPath());
39+
File file = new File(attachment.getPath());
40+
resp.setContentLength((int)file.length());
41+
resp.setContentType(mime);
42+
43+
FileInputStream in = new FileInputStream(file);
44+
OutputStream out = resp.getOutputStream();
45+
byte[] buf = new byte[1024];
46+
int count;
47+
while ((count = in.read(buf)) >= 0) {
48+
out.write(buf, 0, count);
49+
}
50+
out.close();
51+
in.close();
52+
}
53+
}catch (Exception e) {
54+
handleException(resp, e);
55+
}
56+
}
57+
58+
@Override
59+
public void doDelete(HttpServletRequest req, HttpServletResponse resp) {
60+
setDeleteResponseHeaders(resp);
61+
try {
62+
Session session = createSession(req);
63+
TestResultAttachmentDto testResultAttachmentDto = new TestResultAttachmentDto();
64+
testResultAttachmentDto.getIDTemplateFromRequestParameters(req);
65+
validateDelete(testResultAttachmentDto);
66+
session.controllerFactory.getHandler(new TestResultDto()).delete(testResultAttachmentDto);
67+
} catch (Exception e) {
68+
handleException(resp, e);
69+
}
70+
}
71+
72+
73+
@Override
74+
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
75+
try {
76+
req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
77+
setEncoding(resp);
78+
setPostResponseHeaders(resp);
79+
Session session = createSession(req);
80+
TestResultAttachmentDto attachment = new TestResultAttachmentDto();
81+
attachment.getSearchTemplateFromRequestParameters(req);
82+
validatePost(attachment);
83+
84+
FileUtils fileUtils = new FileUtils();
85+
List<String> filePaths = fileUtils.doUpload(req, resp, PathUtils.createPathToBin(
86+
"project",
87+
getStringQueryParameter(req, "project_id"),
88+
"result_attachments",
89+
getStringQueryParameter(req, "test_result_id")));
90+
List<TestResultAttachmentDto> listOfAttachments = new ArrayList<>();
91+
for (String path : filePaths) {
92+
TestResultAttachmentDto newAttachment = new TestResultAttachmentDto();
93+
newAttachment.getSearchTemplateFromRequestParameters(req);
94+
newAttachment.setPath(path);
95+
listOfAttachments.add(newAttachment);
96+
}
97+
98+
session.controllerFactory.getHandler(new TestResultDto()).createMultiple(listOfAttachments);
99+
} catch (Exception e) {
100+
handleException(resp, e);
101+
}
102+
}
103+
104+
private void validateGet(TestResultAttachmentDto attachment) throws AqualityParametersException {
105+
if(attachment.getId() == null) {
106+
throw new AqualityParametersException("You should specify 'id'!");
107+
}
108+
if(attachment.getProject_id() == null) {
109+
throw new AqualityParametersException("You should specify 'project_id'!");
110+
}
111+
}
112+
113+
private void validateDelete(TestResultAttachmentDto attachment) throws AqualityParametersException {
114+
if(attachment.getProject_id() == null) {
115+
throw new AqualityParametersException("You should specify 'project_id'!");
116+
}
117+
if(attachment.getId() == null ) {
118+
throw new AqualityParametersException("You should specify 'id!");
119+
}
120+
}
121+
122+
private void validatePost(TestResultAttachmentDto attachment) throws AqualityParametersException {
123+
if(attachment.getProject_id() == null) {
124+
throw new AqualityParametersException("You should specify 'project_id'!");
125+
}
126+
if(attachment.getTest_result_id() == null ) {
127+
throw new AqualityParametersException("You should specify 'test_result_id!");
128+
}
129+
}
130+
131+
@Override
132+
public void doOptions(HttpServletRequest req, HttpServletResponse resp){
133+
setOptionsResponseHeaders(resp);
134+
}
135+
}

0 commit comments

Comments
 (0)