Skip to content

Commit

Permalink
Address sonar cloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Jun 17, 2024
1 parent cd3d602 commit b9d27b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
28 changes: 16 additions & 12 deletions native/src/main/java/io/ballerina/stdlib/http/api/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,29 @@ public URITemplate<Resource, HttpCarbonMessage> getUriTemplate() throws URITempl
public static HttpService buildHttpService(BObject service, String basePath) {
HttpService httpService = new HttpService(service, basePath);
BMap serviceConfig = getHttpServiceConfigAnnotation(service);
httpService.populateServiceConfig(serviceConfig);
return httpService;
}

protected void populateServiceConfig(BMap serviceConfig) {
if (checkConfigAnnotationAvailability(serviceConfig)) {
httpService.setCompressionConfig(
this.setCompressionConfig(
(BMap<BString, Object>) serviceConfig.get(HttpConstants.ANN_CONFIG_ATTR_COMPRESSION));
httpService.setChunkingConfig(serviceConfig.get(HttpConstants.ANN_CONFIG_ATTR_CHUNKING).toString());
httpService.setCorsHeaders(CorsHeaders.buildCorsHeaders(serviceConfig.getMapValue(CORS_FIELD)));
httpService.setHostName(serviceConfig.getStringValue(HOST_FIELD).getValue().trim());
httpService.setIntrospectionPayload(serviceConfig.getArrayValue(OPENAPI_DEF_FIELD).getByteArray());
this.setChunkingConfig(serviceConfig.get(HttpConstants.ANN_CONFIG_ATTR_CHUNKING).toString());
this.setCorsHeaders(CorsHeaders.buildCorsHeaders(serviceConfig.getMapValue(CORS_FIELD)));
this.setHostName(serviceConfig.getStringValue(HOST_FIELD).getValue().trim());
this.setIntrospectionPayload(serviceConfig.getArrayValue(OPENAPI_DEF_FIELD).getByteArray());
if (serviceConfig.containsKey(MEDIA_TYPE_SUBTYPE_PREFIX)) {
httpService.setMediaTypeSubtypePrefix(serviceConfig.getStringValue(MEDIA_TYPE_SUBTYPE_PREFIX)
this.setMediaTypeSubtypePrefix(serviceConfig.getStringValue(MEDIA_TYPE_SUBTYPE_PREFIX)
.getValue().trim());
}
httpService.setTreatNilableAsOptional(serviceConfig.getBooleanValue(TREAT_NILABLE_AS_OPTIONAL));
httpService.setConstraintValidation(serviceConfig.getBooleanValue(DATA_VALIDATION));
this.setTreatNilableAsOptional(serviceConfig.getBooleanValue(TREAT_NILABLE_AS_OPTIONAL));
this.setConstraintValidation(serviceConfig.getBooleanValue(DATA_VALIDATION));
} else {
httpService.setHostName(HttpConstants.DEFAULT_HOST);
this.setHostName(HttpConstants.DEFAULT_HOST);
}
processResources(httpService);
httpService.setAllAllowedMethods(DispatcherUtil.getAllResourceMethods(httpService));
return httpService;
processResources(this);
this.setAllAllowedMethods(DispatcherUtil.getAllResourceMethods(this));
}

protected static void processResources(HttpService httpService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.stdlib.http.api.nativeimpl.ModuleUtils;
import io.ballerina.stdlib.http.uri.DispatcherUtil;

import java.util.Objects;

Expand All @@ -40,7 +38,7 @@
*/
public class HttpServiceFromContract extends HttpService {

private ReferenceType serviceContractType;
private final ReferenceType serviceContractType;

protected HttpServiceFromContract(BObject service, String basePath, ReferenceType httpServiceContractType) {
super(service, basePath);
Expand All @@ -51,29 +49,19 @@ public static HttpService buildHttpService(BObject service, String basePath,
ReferenceType serviceContractType) {
HttpService httpService = new HttpServiceFromContract(service, basePath, serviceContractType);
BMap serviceConfig = getHttpServiceConfigAnnotation(serviceContractType);
httpService.populateServiceConfig(serviceConfig);
return httpService;
}

@Override
protected void populateServiceConfig(BMap serviceConfig) {
if (checkConfigAnnotationAvailability(serviceConfig)) {
Object basePathFromAnnotation = serviceConfig.get(HttpConstants.ANN_CONFIG_BASE_PATH);
if (Objects.nonNull(basePathFromAnnotation)) {
httpService.setBasePath(basePathFromAnnotation.toString());
}
httpService.setCompressionConfig(
(BMap<BString, Object>) serviceConfig.get(HttpConstants.ANN_CONFIG_ATTR_COMPRESSION));
httpService.setChunkingConfig(serviceConfig.get(HttpConstants.ANN_CONFIG_ATTR_CHUNKING).toString());
httpService.setCorsHeaders(CorsHeaders.buildCorsHeaders(serviceConfig.getMapValue(CORS_FIELD)));
httpService.setHostName(serviceConfig.getStringValue(HOST_FIELD).getValue().trim());
httpService.setIntrospectionPayload(serviceConfig.getArrayValue(OPENAPI_DEF_FIELD).getByteArray());
if (serviceConfig.containsKey(MEDIA_TYPE_SUBTYPE_PREFIX)) {
httpService.setMediaTypeSubtypePrefix(serviceConfig.getStringValue(MEDIA_TYPE_SUBTYPE_PREFIX)
.getValue().trim());
this.setBasePath(basePathFromAnnotation.toString());
}
httpService.setTreatNilableAsOptional(serviceConfig.getBooleanValue(TREAT_NILABLE_AS_OPTIONAL));
httpService.setConstraintValidation(serviceConfig.getBooleanValue(DATA_VALIDATION));
} else {
httpService.setHostName(HttpConstants.DEFAULT_HOST);
}
processResources(httpService);
httpService.setAllAllowedMethods(DispatcherUtil.getAllResourceMethods(httpService));
return httpService;
super.populateServiceConfig(serviceConfig);
}

public static BMap getHttpServiceConfigAnnotation(ReferenceType serviceContractType) {
Expand Down

0 comments on commit b9d27b3

Please sign in to comment.