info.baseinsight.plugin.qrcode.QrcodeController Maven / Gradle / Ivy
The newest version!
package info.baseinsight.plugin.qrcode;
import info.baseinsight.core.SpringUtils;
import info.baseinsight.core.annotation.FullRestController;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;
import java.util.Map;
@FullRestController
public class QrcodeController {
public static final String BACKUP_TEXT = "This qrcode was rendered by baseInsight";
@Autowired
QrcodeService qrcodeService;
/**
* Renders a QRCode that contains the Referer URL that you just came from.
*/
public void index(String url, String text, HttpServletRequest request, HttpServletResponse response) {
renderPng(url!=null ?url : (text!=null ?text : (request.getHeader("REFERER")!=null ?request.getHeader("REFERER"): BACKUP_TEXT)),request,response);
}
/**
* Renders a QRCode with the URL specified.
*/
public void url(String u, String id, HttpServletRequest request, HttpServletResponse response) {
renderPng(u!=null ?u : (id!=null ?id: request.getHeader("REFERER")),request,response);
}
/**
* Renders a QRCode containing arbitrary text. This can
* include iCal or vCard data or whatever you can come up with.
*/
public void text(String text, String id, HttpServletRequest request, HttpServletResponse response) {
renderPng(text!=null ?text : id,request,response);
}
private int getSize(Map params) {
String size = (String) ((String)params.get("s")!=null ? params.get("s") : (params.get("size")!=null ? params.get("size"): params.get("w")!=null ?params.get("w"): params.get("width")));
if (size==null) {
size = "300";
}
int wantedSize = Integer.parseInt(size);
String configSize= SpringUtils.getConfiginfo("base.qrcode.size.max")!=null ? SpringUtils.getConfiginfo("base.qrcode.size.max") :"1024";
int maxSize = Integer.parseInt(configSize);
// make sure the specified size is within reasonable limits, to avoid DoS attacks.
if (wantedSize > maxSize) {
throw new RuntimeException("invalid size: " + wantedSize);
}
return wantedSize;
}
protected void renderPng(String data, HttpServletRequest request, HttpServletResponse response) {
Map params=new HashMap();
params.put("s",request.getParameter("s"));
params.put("size",request.getParameter("size"));
params.put("w",request.getParameter("w"));
params.put("width",request.getParameter("width"));
qrcodeService.renderPng(response, data, getSize(params));
}
}