add "Allow unsigned requests"
[idea/community.git] / platform / built-in-server / src / org / jetbrains / builtInWebServer / BuiltInServerOptions.java
1 package org.jetbrains.builtInWebServer;
2
3 import com.intellij.notification.NotificationType;
4 import com.intellij.openapi.application.ApplicationNamesInfo;
5 import com.intellij.openapi.components.PersistentStateComponent;
6 import com.intellij.openapi.components.ServiceManager;
7 import com.intellij.openapi.components.State;
8 import com.intellij.openapi.components.Storage;
9 import com.intellij.openapi.options.Configurable;
10 import com.intellij.openapi.options.SimpleConfigurable;
11 import com.intellij.openapi.util.Getter;
12 import com.intellij.util.xmlb.XmlSerializerUtil;
13 import com.intellij.util.xmlb.annotations.Attribute;
14 import com.intellij.xdebugger.settings.DebuggerConfigurableProvider;
15 import com.intellij.xdebugger.settings.DebuggerSettingsCategory;
16 import com.intellij.xml.XmlBundle;
17 import org.jetbrains.annotations.NotNull;
18 import org.jetbrains.annotations.Nullable;
19 import org.jetbrains.ide.BuiltInServerManager;
20 import org.jetbrains.ide.BuiltInServerManagerImpl;
21 import org.jetbrains.ide.CustomPortServerManager;
22 import org.jetbrains.io.CustomPortServerManagerBase;
23
24 import java.util.Collection;
25 import java.util.Collections;
26
27 @State(
28   name = "BuiltInServerOptions",
29   storages = @Storage("other.xml")
30 )
31 public class BuiltInServerOptions implements PersistentStateComponent<BuiltInServerOptions>, Getter<BuiltInServerOptions> {
32   private static final int DEFAULT_PORT = 63342;
33
34   @Attribute
35   public int builtInServerPort = DEFAULT_PORT;
36   @Attribute
37   public boolean builtInServerAvailableExternally = false;
38
39   @Attribute
40   public boolean allowUnsignedRequests = false;
41
42   public static BuiltInServerOptions getInstance() {
43     return ServiceManager.getService(BuiltInServerOptions.class);
44   }
45
46   @Override
47   public BuiltInServerOptions get() {
48     return this;
49   }
50
51   static final class BuiltInServerDebuggerConfigurableProvider extends DebuggerConfigurableProvider {
52     @NotNull
53     @Override
54     public Collection<? extends Configurable> getConfigurables(@NotNull DebuggerSettingsCategory category) {
55       if (category == DebuggerSettingsCategory.GENERAL) {
56         return Collections.singletonList(SimpleConfigurable.create("builtInServer", XmlBundle
57           .message("setting.builtin.server.category.label"), BuiltInServerConfigurableUi.class, getInstance()));
58       }
59       return Collections.emptyList();
60     }
61   }
62
63   @Nullable
64   @Override
65   public BuiltInServerOptions getState() {
66     return this;
67   }
68
69   @Override
70   public void loadState(BuiltInServerOptions state) {
71     XmlSerializerUtil.copyBean(state, this);
72   }
73
74   public int getEffectiveBuiltInServerPort() {
75     MyCustomPortServerManager portServerManager = CustomPortServerManager.EP_NAME.findExtension(MyCustomPortServerManager.class);
76     if (!portServerManager.isBound()) {
77       return BuiltInServerManager.getInstance().getPort();
78     }
79     return builtInServerPort;
80   }
81
82   public static final class MyCustomPortServerManager extends CustomPortServerManagerBase {
83     @Override
84     public void cannotBind(Exception e, int port) {
85       BuiltInServerManagerImpl.NOTIFICATION_GROUP.getValue().createNotification("Cannot start built-in HTTP server on custom port " +
86                                                                                 port + ". " +
87                                                                                 "Please ensure that port is free (or check your firewall settings) and restart " +
88                                                                                 ApplicationNamesInfo.getInstance().getFullProductName(),
89                                                                                 NotificationType.ERROR).notify(null);
90     }
91
92     @Override
93     public int getPort() {
94       int port = getInstance().builtInServerPort;
95       return port == DEFAULT_PORT ? -1 : port;
96     }
97
98     @Override
99     public boolean isAvailableExternally() {
100       return getInstance().builtInServerAvailableExternally;
101     }
102   }
103
104   public static void onBuiltInServerPortChanged() {
105     CustomPortServerManager.EP_NAME.findExtension(MyCustomPortServerManager.class).portChanged();
106   }
107 }