Skip to content

Commit e75a608

Browse files
committed
Add GSSUP-1114 Add Common HTTP request call
1 parent fcd8f0f commit e75a608

File tree

13 files changed

+617
-0
lines changed

13 files changed

+617
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.gradle/
2+
build/
3+
target/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Bundle-Name: common-http-request-api
2+
Bundle-SymbolicName: com.liferay.common.http.request
3+
Bundle-Version: 1.0.0
4+
Export-Package: com.liferay.common.http.request.api,\com.liferay.common.http.request.api.constants
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies {
2+
compileOnly group: "com.liferay.portal", name: "release.dxp.api"
3+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.liferay.common.http.request.api;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* @author Payal
7+
*/
8+
public interface CommonHttpRequestApi {
9+
10+
String invokeGetRestServiceHttps(String url, String requestBody, String urlParameters,
11+
Map<String, String> requestHeaderAttributes);
12+
<T> T invoketHttpsGet(String url, String urlParameters, Class<T> responseType,
13+
Map<String, String> requestHeaderAttributes);
14+
String invokePostRestService(String url, String requestBody,String authHeader, String urlParameters);
15+
16+
String invokePostRestServiceHttps(String url, String requestBody,String authHeader, String urlParameters, Map<String,String> requestHeaderAttributes);
17+
18+
String invokePutRestService(String url, String requestBody, String authHeader, String urlParameters);
19+
20+
String invokePutRestServiceHttps(String url, String requestBody, String authHeader, String urlParameters);
21+
22+
String invokeDeleteFileRestServiceHttps(String url, String queryString, String requestBody, String authHeader,
23+
Map<String, String> requestHeaderAttributes);
24+
25+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.liferay.common.http.request.api.constants;
2+
3+
import java.io.Serializable;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class ApiException extends RuntimeException implements Serializable {
9+
10+
// Predefined HTTP Status Codes
11+
public static final int BAD_REQUEST = 400;
12+
public static final int UNAUTHORIZED = 401;
13+
public static final int FORBIDDEN = 403;
14+
public static final int DATA_NOT_FOUND = 404;
15+
public static final int INTERNAL_SERVER_ERROR = 500;
16+
17+
private static final long serialVersionUID = 1L; // For serialization
18+
19+
private int statusCode;
20+
private Map<String, String> fieldErrors;
21+
private Throwable throwable; // New field for the throwable
22+
23+
public ApiException(String message, int statusCode) {
24+
super(message);
25+
this.statusCode = statusCode;
26+
this.fieldErrors = new HashMap<>();
27+
}
28+
29+
public ApiException(String message, int statusCode, Map<String, String> fieldErrors) {
30+
super(message);
31+
this.statusCode = statusCode;
32+
this.fieldErrors = new HashMap<>(fieldErrors); // Make a defensive copy
33+
}
34+
35+
public ApiException(String message, int statusCode, Throwable throwable) {
36+
super(message, throwable);
37+
this.statusCode = statusCode;
38+
this.fieldErrors = new HashMap<>();
39+
this.throwable = throwable;
40+
}
41+
42+
public ApiException(String message, int statusCode, Map<String, String> fieldErrors, Throwable throwable) {
43+
super(message, throwable);
44+
this.statusCode = statusCode;
45+
this.fieldErrors = new HashMap<>(fieldErrors); // Make a defensive copy
46+
this.throwable = throwable;
47+
}
48+
49+
public int getStatusCode() {
50+
return statusCode;
51+
}
52+
53+
public Map<String, String> getFieldErrors() {
54+
return Collections.unmodifiableMap(fieldErrors); // Return unmodifiable map
55+
}
56+
57+
public void addFieldError(String field, String errorMessage) {
58+
this.fieldErrors.put(field, errorMessage);
59+
}
60+
61+
public String getThrowableMessage() {
62+
return throwable != null ? throwable.getMessage() : null;
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return String.format("ApiException{message='%s', statusCode=%d, fieldErrors=%s, throwableMessage=%s}",
68+
getMessage(), statusCode, fieldErrors, getThrowableMessage());
69+
}
70+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.liferay.common.http.request.api.constants;
2+
3+
import java.util.Map;
4+
5+
public class DataNotFoundException extends ApiException {
6+
7+
public DataNotFoundException(String message) {
8+
super(message, DATA_NOT_FOUND); // Status code 404 for data not found
9+
}
10+
11+
public DataNotFoundException(String message, Map<String, String> fieldErrors) {
12+
super(message, DATA_NOT_FOUND, fieldErrors);
13+
}
14+
15+
public DataNotFoundException(String field, String errorMessage) {
16+
super("Data not found for the provided field(s)", DATA_NOT_FOUND);
17+
addFieldError(field, errorMessage); // Add a specific field error
18+
}
19+
}

modules/common-http-request-api/src/main/resources/com/liferay/common/http/request/.gitkeep

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version 1.0.0
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.gradle/
2+
build/
3+
target/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Bundle-Name: common-http-request-impl
2+
Bundle-SymbolicName: com.liferay.common.http.request.impl
3+
Bundle-Version: 1.0.0

0 commit comments

Comments
 (0)