3434
3535import java .io .IOException ;
3636import java .io .InputStream ;
37- import java .io .UnsupportedEncodingException ;
3837import java .net .URLEncoder ;
3938import java .nio .charset .StandardCharsets ;
4039import java .util .List ;
4443import java .util .regex .Pattern ;
4544
4645public class PreviewStaticServer extends HttpRequestHandler {
47- private Logger log = Logger .getInstance (PreviewStaticServer .class );
46+ private final Logger log = Logger .getInstance (PreviewStaticServer .class );
4847
4948 private static final Logger LOG = Logger .getInstance (PreviewStaticServer .class );
5049 private static final String PREFIX = "/ead61b63-b0a6-4ff2-a49a-86be75ccfd1a/" ;
@@ -126,23 +125,19 @@ public static String getStyleUrl(@NotNull String scriptFileName) {
126125
127126 public static Url getFileUrl (OpenInBrowserRequest request , VirtualFile file ) {
128127 Url url ;
129- try {
130- StringBuilder sb = new StringBuilder ();
131- sb .append ("http://localhost:" ).append (BuiltInServerManager .getInstance ().getPort ()).append (PREFIX );
132- if (file instanceof LightVirtualFile ) {
133- throw new IllegalStateException ("unable to create a URL from a in-memory file" );
134- }
135- String mac = getBrowserPanel ().signFile (file .getPath ()).replaceAll ("&" , "&" );
136- sb .append ("source?file=" ).append (mac );
137- if (request .getProject ().getPresentableUrl () != null ) {
138- sb .append ("&projectUrl=" ).append (URLEncoder .encode (request .getProject ().getPresentableUrl (), StandardCharsets .UTF_8 .toString ()));
139- } else {
140- sb .append ("&projectName=" ).append (URLEncoder .encode (request .getProject ().getName (), StandardCharsets .UTF_8 .toString ()));
141- }
142- url = Urls .parseEncoded (sb .toString ());
143- } catch (UnsupportedEncodingException e ) {
144- throw new IllegalStateException ("can't encode" );
128+ StringBuilder sb = new StringBuilder ();
129+ sb .append ("http://localhost:" ).append (BuiltInServerManager .getInstance ().getPort ()).append (PREFIX );
130+ if (file instanceof LightVirtualFile ) {
131+ throw new IllegalStateException ("unable to create a URL from a in-memory file" );
132+ }
133+ String mac = getBrowserPanel ().signFile (file .getPath ()).replaceAll ("&" , "&" );
134+ sb .append ("source?file=" ).append (mac );
135+ if (request .getProject ().getPresentableUrl () != null ) {
136+ sb .append ("&projectUrl=" ).append (URLEncoder .encode (request .getProject ().getPresentableUrl (), StandardCharsets .UTF_8 ));
137+ } else {
138+ sb .append ("&projectName=" ).append (URLEncoder .encode (request .getProject ().getName (), StandardCharsets .UTF_8 ));
145139 }
140+ url = Urls .parseEncoded (sb .toString ());
146141 if (request .isAppendAccessToken ()) {
147142 url = BuiltInServerManager .getInstance ().addAuthToken (Objects .requireNonNull (url ));
148143 }
@@ -232,8 +227,8 @@ public boolean process(@NotNull QueryStringDecoder urlDecoder,
232227 }
233228 sendDocument (request , virtualFile , project , context .channel ());
234229 } else if ("image" .equals (action ) && urlDecoder .parameters ().get ("file" ) != null && urlDecoder .parameters ().get ("mac" ) != null ) {
235- String file = urlDecoder .parameters ().get ("file" ).get ( 0 );
236- String mac = urlDecoder .parameters ().get ("mac" ).get ( 0 );
230+ String file = urlDecoder .parameters ().get ("file" ).getFirst ( );
231+ String mac = urlDecoder .parameters ().get ("mac" ).getFirst ( );
237232 return sendImage (request , file , mac , context .channel ());
238233 } else {
239234 return false ;
@@ -248,7 +243,7 @@ private String getParameter(@NotNull QueryStringDecoder urlDecoder, @NotNull Str
248243 if (parameters == null || parameters .size () != 1 ) {
249244 return null ;
250245 }
251- return parameters .get ( 0 );
246+ return parameters .getFirst ( );
252247 }
253248
254249 private boolean sendImage (FullHttpRequest request , String file , String mac , Channel channel ) {
0 commit comments