package ru.atom.http.server;

import com.sun.org.apache.regexp.internal.RE;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.jetty.util.ConcurrentArrayQueue;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.Date;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Path("/")
public class ChatResource {
    private static final Logger log = LogManager.getLogger(ChatResource.class);

    private static final LinkedList<String> logined = new LinkedList<>();
    private static final ConcurrentArrayQueue<String> chat = new ConcurrentArrayQueue<>();

    @POST
    @Consumes("application/x-www-form-urlencoded")
    @Path("/login")
    public Response login(@QueryParam("name") String name) {
        if (name.length() > 30) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Too long name, sorry :(").build();
        }
        if (logined.contains(name)) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Already logined").build();
        }
        log.info("[" + name + "] logined");
        logined.add(name);
        chat.add("[<span style=\"color:red\">" + name +
                 "</span> <span style=\"color:green\">" + new Date(System.currentTimeMillis()) + "</span>]: joined");
        return Response.ok().build();
    }

    @GET
    @Produces("text/plain")
    @Path("/online")
    public Response online() {
        return Response.ok(String.join("\n ", logined)).build();
    }

    @POST
    @Consumes("application/x-www-form-urlencoded")
    @Path("/say")
    public Response say(@QueryParam("name") String name, @FormParam("msg") String msg) {
        if (!logined.contains(name)) {
            return Response.status(Response.Status.UNAUTHORIZED).entity("Not logined").build();
        }
        if (msg == null) {
            return Response.status(Response.Status.BAD_REQUEST).entity("No message provided").build();
        }
        if (msg.length() > 130) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Too long message (longer than 140" +
                    " symbols)").build();
        }
        log.info("[" + name + "]: " + msg);
        Pattern p = Pattern.compile("(http|ftp|https)://([\\w_-]+(?:(?:\\.[\\w_-]+)+))([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])");
        Matcher m = p.matcher(msg);
        while(m.find()) {
            msg = m.replaceFirst("<a href=\"" + msg.substring(m.start(), m.end()) + "\">" + msg.substring(m.start(), m.end()) + "</a>");
        }
        chat.add("[<span style=\"color:red\">" + name
                + "</span> <span style=\"color:green\">" + new Date(System.currentTimeMillis()) + "</span>]: " + msg);
        return Response.status(Response.Status.OK).build();
    }

    @GET
    @Produces("text/plain")
    @Path("/chat")
    public Response chat() {
        return Response.ok(String.join("\n ", chat)).build();
    }

    @POST
    @Consumes("application/x-www-form-urlencoded")
    @Path("/logout")
    public Response logout(@QueryParam("name") String name) {
        if (!logined.contains(name)) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Not logined").build();
        }
        logined.remove(name);
        log.info("[" + name + "]: logged out");
        chat.add("[<span style=\"color:red\">" + name +
                "</span> <span style=\"color:green\">" + new Date(System.currentTimeMillis()) + "</span>]: logged out");
        return Response.status(Response.Status.OK).build();
    }
}

@@ -27,54 +27,8 @@
 r;%0A%0A
-import com.sun.org.apache.regexp.internal.RE;%0A
 impo
@@ -2390,16 +2390,22 @@
 attern p
+attern
  = Patte
@@ -2415,16 +2415,33 @@
 compile(
+%0A                
 %22(http%7Cf
@@ -2545,12 +2545,22 @@
 er m
- = p
+atch = pattern
 .mat
@@ -2583,18 +2583,23 @@
    while
+ 
 (m
+atch
 .find())
@@ -2620,16 +2620,20 @@
  msg = m
+atch
 .replace
@@ -2660,32 +2660,36 @@
  msg.substring(m
+atch
 .start(), m.end(
@@ -2675,32 +2675,36 @@
 match.start(), m
+atch
 .end()) + %22%5C%22%3E%22 
@@ -2704,16 +2704,36 @@
  %22%5C%22%3E%22 +
+%0A                   
  msg.sub
@@ -2740,16 +2740,20 @@
 string(m
+atch
 .start()
@@ -2755,16 +2755,20 @@
 art(), m
+atch
 .end()) 

Fix code style