bstlboard-back/src/main/java/de/bstly/board/i18n/controller/I18nController.java

115 lines
2.9 KiB
Java

/**
*
*/
package de.bstly.board.i18n.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import de.bstly.board.controller.BaseController;
import de.bstly.board.i18n.businesslogic.I18nManager;
/**
* The Class I18nController.
*/
@RestController
@RequestMapping("/i18n")
public class I18nController extends BaseController {
@Autowired
private I18nManager i18nManager;
private Gson gson = new Gson();
/**
* Gets the locales.
*
* @return the locales
*/
@GetMapping
public List<String> getLocales() {
return i18nManager.getLocales();
}
/**
* Gets the label.
*
* @param locale the locale
* @param response the response
* @return the label
* @throws JsonIOException the json IO exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@GetMapping("/{locale}")
public void getLabel(@PathVariable("locale") String locale, HttpServletResponse response)
throws JsonIOException, IOException {
JsonObject label = i18nManager.getLabel(locale);
if (label != null) {
response.setCharacterEncoding("utf-8");
gson.toJson(label, response.getWriter());
}
}
/**
* Sets the label.
*
* @param locale the locale
* @param label the label
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PostMapping("/{locale}")
public void setLabel(@PathVariable("locale") String locale, @RequestBody Object label) {
JsonElement element = gson.toJsonTree(label);
if (element != null && element.isJsonObject()) {
i18nManager.setLabel(locale, element.getAsJsonObject());
}
}
/**
* Adds the label.
*
* @param locale the locale
* @param label the label
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PutMapping("/{locale}")
public void addLabel(@PathVariable("locale") String locale, @RequestBody Object label) {
JsonElement element = gson.toJsonTree(label);
if (element != null && element.isJsonObject()) {
i18nManager.addLabel(locale, element.getAsJsonObject());
}
}
/**
* Delete locale.
*
* @param locale the locale
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("/{locale}")
public void deleteLocale(@PathVariable("locale") String locale) {
i18nManager.deleteLabel(locale);
}
}