|
| 1 | +package com.shuzijun.leetcode.plugin.editor; |
| 2 | + |
| 3 | +import com.intellij.openapi.project.Project; |
| 4 | +import com.intellij.openapi.project.ProjectManager; |
| 5 | +import io.netty.buffer.Unpooled; |
| 6 | +import io.netty.channel.ChannelHandlerContext; |
| 7 | +import io.netty.handler.codec.http.*; |
| 8 | +import org.jetbrains.annotations.NotNull; |
| 9 | +import org.jetbrains.io.Responses; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.net.URLDecoder; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author shuzijun |
| 19 | + */ |
| 20 | +public abstract class BaseController { |
| 21 | + |
| 22 | + // every time the plugin starts up, assume resources could have been modified |
| 23 | + protected static final long LAST_MODIFIED = System.currentTimeMillis(); |
| 24 | + |
| 25 | + public final void process(@NotNull QueryStringDecoder urlDecoder, @NotNull FullHttpRequest request, @NotNull ChannelHandlerContext context) throws IOException { |
| 26 | + FullHttpResponse response; |
| 27 | + try { |
| 28 | + if (request.method() == HttpMethod.POST) { |
| 29 | + response = post(urlDecoder, request, context); |
| 30 | + } else if (request.method() == HttpMethod.GET) { |
| 31 | + response = get(urlDecoder, request, context); |
| 32 | + } else { |
| 33 | + response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); |
| 34 | + } |
| 35 | + } catch (Throwable t) { |
| 36 | + response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(t.getMessage().getBytes(StandardCharsets.UTF_8))); |
| 37 | + } |
| 38 | + Responses.send(response, context.channel(), request); |
| 39 | + if (response.content() != Unpooled.EMPTY_BUFFER) { |
| 40 | + try { |
| 41 | + response.release(); |
| 42 | + } catch (Exception ignore) { |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public FullHttpResponse get(@NotNull QueryStringDecoder urlDecoder, @NotNull FullHttpRequest request, @NotNull ChannelHandlerContext context) throws IOException { |
| 48 | + return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); |
| 49 | + } |
| 50 | + |
| 51 | + public FullHttpResponse post(@NotNull QueryStringDecoder urlDecoder, @NotNull FullHttpRequest request, @NotNull ChannelHandlerContext context) throws IOException { |
| 52 | + return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); |
| 53 | + } |
| 54 | + |
| 55 | + public abstract String getControllerPath(); |
| 56 | + |
| 57 | + protected String getResourceName(QueryStringDecoder urlDecoder) { |
| 58 | + return urlDecoder.path().substring(PreviewStaticServer.PREFIX.length() + getControllerPath().length()); |
| 59 | + } |
| 60 | + |
| 61 | + protected String getParameter(@NotNull QueryStringDecoder urlDecoder, @NotNull String parameter) { |
| 62 | + List<String> parameters = urlDecoder.parameters().get(parameter); |
| 63 | + if (parameters == null || parameters.size() != 1) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + return URLDecoder.decode(parameters.get(0), StandardCharsets.UTF_8); |
| 67 | + } |
| 68 | + |
| 69 | + protected FullHttpResponse fillHtmlResponse(String content) { |
| 70 | + FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(content.getBytes(StandardCharsets.UTF_8))); |
| 71 | + response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain;charset=UTF-8"); |
| 72 | + response.headers().set(HttpHeaderNames.CACHE_CONTROL, "max-age=5, private, must-revalidate"); |
| 73 | + response.headers().set("Referrer-Policy", "no-referrer"); |
| 74 | + return response; |
| 75 | + } |
| 76 | + |
| 77 | + protected FullHttpResponse fillJsonResponse(String content) { |
| 78 | + FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(content.getBytes(StandardCharsets.UTF_8))); |
| 79 | + response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json;charset=UTF-8"); |
| 80 | + response.headers().set(HttpHeaderNames.CACHE_CONTROL, "max-age=5, private, must-revalidate"); |
| 81 | + response.headers().set("Referrer-Policy", "no-referrer"); |
| 82 | + return response; |
| 83 | + } |
| 84 | + |
| 85 | + public void addRoute(Map<String, BaseController> route) { |
| 86 | + route.put(PreviewStaticServer.PREFIX + getControllerPath(), this); |
| 87 | + } |
| 88 | + |
| 89 | + protected Project getProject(String projectNameParameter, String projectUrlParameter) { |
| 90 | + Project project = null; |
| 91 | + for (Project p : ProjectManager.getInstance().getOpenProjects()) { |
| 92 | + if ((projectNameParameter != null && projectNameParameter.equals(p.getName())) |
| 93 | + || (projectUrlParameter != null && projectUrlParameter.equals(p.getPresentableUrl()))) { |
| 94 | + project = p; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return project; |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | +} |
0 commit comments