diff --git a/modules/rest-api/src/com/haulmont/addon/restapi/api/controllers/VersionController.java b/modules/rest-api/src/com/haulmont/addon/restapi/api/controllers/VersionController.java new file mode 100644 index 0000000..e3472c6 --- /dev/null +++ b/modules/rest-api/src/com/haulmont/addon/restapi/api/controllers/VersionController.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2008-2019 Haulmont. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.haulmont.addon.restapi.api.controllers; + +import com.haulmont.addon.restapi.api.service.VersionControllerManager; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.inject.Inject; + +/** + * REST controller that is used for getting REST API version + */ +@RestController("restapi_VersionController") +@RequestMapping(path = "/v2/version") +public class VersionController { + + @Inject + protected VersionControllerManager versionControllerManager; + + @GetMapping + public String getApiVersion() { + return versionControllerManager.getApiVersion(); + } +} diff --git a/modules/rest-api/src/com/haulmont/addon/restapi/api/service/VersionControllerManager.java b/modules/rest-api/src/com/haulmont/addon/restapi/api/service/VersionControllerManager.java new file mode 100644 index 0000000..f880232 --- /dev/null +++ b/modules/rest-api/src/com/haulmont/addon/restapi/api/service/VersionControllerManager.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2008-2019 Haulmont. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.haulmont.addon.restapi.api.service; + +import com.haulmont.addon.restapi.api.controllers.VersionController; +import com.haulmont.addon.restapi.api.exception.RestAPIException; +import com.haulmont.cuba.core.global.BuildInfo; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; + +import javax.inject.Inject; +import java.io.*; +import java.util.Properties; + +/** + * Class that is used by {@link VersionController} for getting REST API version + */ +@Component("restapi_VersionControllerManager") +public class VersionControllerManager { + + @Inject + protected BuildInfo buildInfo; + + public String getApiVersion() { + BuildInfo.Content content = buildInfo.getContent(); + + String version = null; + + if (content.getAppName().equals("restapi") && content.getArtifactGroup().equals("com.haulmont.addon.restapi")) { + // Standalone REST API + version = content.getVersion(); + } else { + // REST API as part of a Cuba application + for (String component : content.getAppComponents()) { + if (component.trim().startsWith("com.haulmont.addon.restapi:")) { + version = component.split(":")[1]; + break; + } + } + } + + if (version != null && version.length() > 0) { + return version; + } else { + throw new RestAPIException("Could not determine REST API version", null, HttpStatus.INTERNAL_SERVER_ERROR); + } + + } +}