Skip to content

Commit

Permalink
Feature detection mechanism cuba-platform/cuba-rest-js#20
Browse files Browse the repository at this point in the history
  • Loading branch information
vyacheslav-pushkin committed Nov 28, 2019
1 parent 1d84d8c commit cc5dcc9
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
}

0 comments on commit cc5dcc9

Please sign in to comment.