text:
package no.ntnu.okse.protocol.wsn;

import com.google.common.io.ByteStreams;
import no.ntnu.okse.core.messaging.Message;
import no.ntnu.okse.core.subscription.Subscriber;
import no.ntnu.okse.core.subscription.SubscriptionService;
import no.ntnu.okse.protocol.AbstractProtocolServer;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.util.InputStreamContentProvider;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.ntnunotif.wsnu.base.internal.ServiceConnection;
import org.ntnunotif.wsnu.base.net.NuNamespaceContextResolver;
import org.ntnunotif.wsnu.base.soap.Soap;
import org.ntnunotif.wsnu.base.util.InternalMessage;
import org.ntnunotif.wsnu.base.util.RequestInformation;
import org.ntnunotif.wsnu.services.general.WsnUtilities;
import org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType;
import org.oasis_open.docs.wsn.b_2.Notify;
import org.oasis_open.docs.wsn.b_2.TopicExpressionType;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import java.io.*;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class WSNotificationServer extends AbstractProtocolServer {

    // Runstate variables
    private boolean _running;

    // Path to internal configuration file on classpath
    private static final String wsnInternalConfigFile = "/config/wsnserver.xml";

    // Internal Default Values
    private static final String DEFAULT_HOST = "0.0.0.0";
    private static final int DEFAULT_PORT = 61000;
    private static final String DEFAULT_MESSAGE_CONTENT_WRAPPER_NAME = "Content";

    // Flag and defaults for operation behind NAT
    private boolean behindNAT;
    private String publicWANHost;
    private Integer publicWANPort;

    // HTTP Client fields
    private Long connectionTimeout;
    private Integer clientPoolSize;

    // Non-XMl Content Wrapper Name
    private String contentWrapperElementName;

    // Instance fields
    private Server _server;
    private WSNRequestParser _requestParser;
    private WSNCommandProxy _commandProxy;
    private final ArrayList<Connector> _connectors = new ArrayList();
    private HttpClient _client;
    private HashSet<ServiceConnection> _services;
    private ExecutorService clientPool;
    private TreeSet<String> relays = new TreeSet<>();

    /**
     * Constructor that takes in configuration options for the WSNotification
     * server.
     *

     *
     * @param host A String representing the host the WSNServer should bind to
     * @param port An int representing the port the WSNServer should bind to.
     * @param timeout A Long
     * @param pool_size An int
     * @param wrapper_name A String
     * @param nat A boolean
     * @param wan_host A string
     * @param wan_port An int
     */
    public WSNotificationServer(
            String host, int port, Long timeout,
            int pool_size, String wrapper_name,
            boolean nat, String wan_host, int wan_port) {
        this.host = host;
        this.port = port;
        connectionTimeout = timeout;
        clientPoolSize = pool_size;
        contentWrapperElementName = wrapper_name;
        behindNAT = nat;
        publicWANHost = wan_host;
        publicWANPort = wan_port;
        log = Logger.getLogger(WSNotificationServer.class.getName());
        init();
    }

    /**
     * Initialization method that reads the wsnserver.xml configuration file and constructs
     * a jetty server instance.
     */
    private void init() {
        log.warn("Logging!");

        // Set the servertype
        protocolServerType = "WSNotification";

        // Declare HttpClient field
        _client = null;

        clientPool = Executors.newFixedThreadPool(clientPoolSize);

        if (contentWrapperElementName.contains("<") || contentWrapperElementName.contains(">")) {
            log.warn("Non-XML message payload element wrapper name cannot contain XML element characters (< or >)," +
                    " using default: " + DEFAULT_MESSAGE_CONTENT_WRAPPER_NAME);
            contentWrapperElementName = DEFAULT_MESSAGE_CONTENT_WRAPPER_NAME;
        }

        // Declare configResource (Fetched from classpath as a Resource from system)
        Resource configResource;
        try {
            // Try to parse the configFile for WSNServer to set up the Server instance
            configResource = Resource.newSystemResource(wsnInternalConfigFile);
            XmlConfiguration config = new XmlConfiguration(configResource.getInputStream());
            this._server = (Server) config.configure();
            // Remove the xmlxonfig connector
            this._server.removeConnector(this._server.getConnectors()[0]);

            // Add a the serverconnector
            log.debug("Adding WSNServer connector");
            this.addStandardConnector(this.host, this.port);

            // Initialize the RequestParser for WSNotification
            this._requestParser = new WSNRequestParser(this);

            // Initialize the collection of ServiceConnections
            this._services = new HashSet<>();

            // Initialize and set the HTTPHandler for the Server instance
            HttpHandler handler = new WSNotificationServer.HttpHandler();
            this._server.setHandler(handler);

            log.debug("XMLConfig complete, server instanciated.");

        } catch (Exception e) {
            log.error("Unable to start WSNotificationServer: " + e.getMessage());
        }
     }

    /**
     * The primary boot method for starting a WSNServer instance. Will only perform actions if the
     * server instance is not already running.
     *

     * Initializes a HttpClient, and starts it. Also adds predefined connectors to the jetty server
     * instance. Constructs a new serverThread and starts the jetty server instance in this new thread.
     *

     */
    public void boot() {

        log.info("Booting WSNServer.");

        if (!_running) {
            try {
                // Initialize a plain HttpClient
                this._client = new HttpClient();
                // Turn off following HTTP 30x redirects for the client
                this._client.setFollowRedirects(false);
                this._client.start();
                log.info("Started WSNServer HTTPClient");

                // For all registered connectors in WSNotificationServer, add these to the Jetty Server
                this._connectors.stream().forEach(c -> this._server.addConnector(c));

                /* OKSE custom WS-Nu web services */

                // Initialize the CommandProxy
                WSNCommandProxy broker = new WSNCommandProxy(this);
                _commandProxy = broker;
                // Initialize the WSN SubscriptionManager and PublisherRegistrationManager
                WSNSubscriptionManager subscriptionManager = new WSNSubscriptionManager(this);
                WSNRegistrationManager registrationManager = new WSNRegistrationManager(this);
                // Add listener support from the OKSE SubscriptionService
                SubscriptionService.getInstance().addSubscriptionChangeListener(subscriptionManager);
                SubscriptionService.getInstance().addPublisherChangeListener(registrationManager);

                // QuickBuild the broker
                broker.quickBuild("broker", this._requestParser);
                // QuickBuild the WSN SubManager
                subscriptionManager.quickBuild("subscriptionManager", this._requestParser);
                subscriptionManager.initCoreSubscriptionService(SubscriptionService.getInstance());
                // QuickBuild the WSN PubRegManager
                registrationManager.quickBuild("registrationManager", this._requestParser);
                registrationManager.initCoreSubscriptionService(SubscriptionService.getInstance());
                // Register the WSN managers to the command proxy (proxied broker)
                broker.setSubscriptionManager(subscriptionManager);
                broker.setRegistrationManager(registrationManager);

                // Create a new thread for the Jetty Server to run in
                this._serverThread = new Thread(() -> {
                    this.run();
                });
                this._serverThread.setName("WSNServer");
                // Start the Jetty Server
                this._serverThread.start();
                Connector c = _connectors.get(0);
                while (!c.isStarted()) {
                    if (c.isFailed())
                        throw new BootErrorException("Unable to bind to " + host + ":" + port);
                }
                _running = true;
                log.info("WSNServer Thread started successfully.");
            } catch (BootErrorException e) {
                throw e;
            } catch (Exception e) {
                totalErrors.incrementAndGet();
                log.trace(e.getStackTrace());
            }
        }
    }

    /**
     * This interface method should contain the main run loop initialization
     */
    @Override
    public void run() {
        try {
            WSNotificationServer.this._server.start();
            WSNotificationServer.this._server.join();

        } catch (Exception serverError) {
            totalErrors.incrementAndGet();
            log.trace(serverError.getStackTrace());
        }
    }

    /**
     * Fetch the HashSet containing all WebServices registered to the protocol server
     *
     * @return A HashSet of ServiceConnections for all the registered web services.
     */
    public HashSet<ServiceConnection> getServices() {
        return _services;
    }

    /**
     * This method stops the execution of the WSNotificationServer instance.
     */
    @Override
    public void stopServer() {
        try {
            log.info("Stopping WSNServer...");
            // Removing all subscribers
            _commandProxy.getAllRecipients().forEach(s -> {
                _commandProxy.getProxySubscriptionManager().removeSubscriber(s);
            });
            // Removing all publishers
            _commandProxy.getProxyRegistrationManager().getAllPublishers().forEach(p -> {
                _commandProxy.getProxyRegistrationManager().removePublisher(p);
            });

            // Stop the HTTP Client
            this._client.stop();
            // Stop the ServerConnector
            this._server.stop();
            this._serverThread = null;
            // Reset flags
            log.info("WSNServer Client and ServerThread stopped");
        } catch (Exception e) {
            totalErrors.incrementAndGet();
            log.trace(e.getStackTrace());
        }
    }

    /**
     * Fetches the specified String representation of the Protocol that this ProtocolServer handles.
     *
     * @return A string representing the name of the protocol that this ProtocolServer handles.
     */
    @Override
    public String getProtocolServerType() {
        return protocolServerType;
    }

    /**
     * Retrieve the default element name for non-XML messages that are to be wrapped in a soap enveloped
     * WSNotification Notify element. This element will be the first and only child of the Message element.
     *
     * @return The default name of the content wrapper element
     */
    public String getMessageContentWrapperElementName() {
        return contentWrapperElementName;
    }

    /**
     * This interface method must take in an instance of Message, which contains the appropriate references
     * and flags needed to distribute the message to consumers. Implementation specific details can vary from
     * protocol to protocol, but the end result of a method call to sendMessage is that the message is delivered,
     * or an error is logged.
     *
     * @param message An instance of Message containing the required data to distribute a message.
     */
    @Override
    public void sendMessage(Message message) {
        log.debug("WSNServer received message for distribution");
        if (!message.getOriginProtocol().equals(protocolServerType) || message.getAttribute("duplicate") != null) {
            log.debug("The message originated from other protocol than WSNotification");

            WSNTools.NotifyWithContext notifywrapper = WSNTools.buildNotifyWithContext(getMessageContentWrapperElementName(), message.getMessage(), message.getTopic(), null, null);
            // If it contained XML, we need to create properly marshalled jaxb node structure
            if (message.getMessage().contains("<") || message.getMessage().contains(">")) {
                // Unmarshal from raw XML
                Notify notify = WSNTools.createNotify(message);
                // If it was malformed, or maybe just a message containing < or >, build it as generic content element
                if (notify == null) {
                    WSNTools.injectMessageContentIntoNotify(WSNTools.buildGenericContentElement(getMessageContentWrapperElementName(), message.getMessage()), notifywrapper.notify);
                    // Else inject the unmarshalled XML nodes into the Notify message attribute
                } else {
                    WSNTools.injectMessageContentIntoNotify(WSNTools.extractMessageContentFromNotify(notify), notifywrapper.notify);
                }
            }

            /*
                Start to resolve recipients. The reason we cannot re-use the WSNCommandProxy's
                sendNotification method is that it will inject the message to the MessageService for relay
                thus creating duplicate messages.
             */

            NuNamespaceContextResolver namespaceContextResolver = notifywrapper.nuNamespaceContextResolver;

            // bind namespaces to topics
            for (NotificationMessageHolderType holderType : notifywrapper.notify.getNotificationMessage()) {

                // Extract the topic
                TopicExpressionType topic = holderType.getTopic();

                if (holderType.getTopic() != null) {
                    NuNamespaceContextResolver.NuResolvedNamespaceContext context = namespaceContextResolver.resolveNamespaceContext(topic);

                    if (context == null) {
                        continue;
                    }

                    context.getAllPrefixes().forEach(prefix -> {
                        // check if this is the default xmlns attribute
                        if (!prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
                            // add namespace context to the expression node
                            topic.getOtherAttributes().put(new QName("xmlns:" + prefix), context.getNamespaceURI(prefix));
                        }
                    });
                }
            }

            // For all valid recipients
            for (String recipient : _commandProxy.getAllRecipients()) {

                // If the subscription has expired, continue
                if (_commandProxy.getProxySubscriptionManager().getSubscriber(recipient).hasExpired()) continue;

                // Filter do filter handling, if any
                Notify toSend = _commandProxy.getRecipientFilteredNotify(recipient, notifywrapper.notify, namespaceContextResolver);

                // If any message was left to send, send it
                if (toSend != null) {
                    InternalMessage outMessage = new InternalMessage(
                            InternalMessage.STATUS_OK |
                                    InternalMessage.STATUS_HAS_MESSAGE |
                                    InternalMessage.STATUS_ENDPOINTREF_IS_SET,
                            toSend
                    );
                    // Update the requestinformation
                    outMessage.getRequestInformation().setEndpointReference(_commandProxy.getEndpointReferenceOfRecipient(recipient));

                    // Check if the subscriber has requested raw message format
                    // If the recipient has requested UseRaw, remove Notify payload wrapping
                    if (_commandProxy
                            .getProxySubscriptionManager()
                            .getSubscriber(recipient)
                            .getAttribute(WSNSubscriptionManager.WSN_USERAW_TOKEN) != null) {

                        Object content = WSNTools.extractMessageContentFromNotify(toSend);
                        // Update the InternalMessage with the content of the NotificationMessage
                        outMessage.setMessage(content);
                    }

                    // Use the correct SOAP version for this subscriber
                    Subscriber subscriber =_commandProxy.getProxySubscriptionManager().getSubscriber(recipient);
                    String version = subscriber.getAttribute("soap_version");
                    if(version == null) {
                        outMessage.setVersion(Soap.SoapVersion.SOAP_1_1);
                    } else {
                        switch(version) {
                            default:
                            case "soap11":
                                outMessage.setVersion(Soap.SoapVersion.SOAP_1_1);
                                break;
                            case "soap12D":
                                outMessage.setVersion(Soap.SoapVersion.SOAP_1_2_2001);
                                break;
                            case "soap12F":
                                outMessage.setVersion(Soap.SoapVersion.SOAP_1_2_2003);
                                break;
                        }
                    }

                    // Pass it along to the request parser wrapped as a thread pool executed job
                    clientPool.execute(() -> _requestParser.acceptLocalMessage(outMessage));
                }
            }
        } else {
            log.debug("Message originated from WSN protocol, already processed");
        }
    }

    /**
     * Fetches the complete URI of this ProtocolServer
     *
     * @return A string representing the complete URI of this ProtocolServer
     */
    public String getURI() {
        // Check if we are behind NAT
        if (behindNAT) {
            return "http://" + publicWANHost + ":" + publicWANPort;
        }
        // If somehow URI could not be retrieved
        if (_server.getURI() == null) {
            log.warn("Failed to fetch URI of server");
            return "http://" + DEFAULT_HOST + ":" + DEFAULT_PORT;
        }
        // Return the server connectors registered host and port
        //return "http://" + _server.getURI().getHost() + ":" + (_server.getURI().getPort() > -1 ? _server.getURI().getPort() : DEFAULT_PORT);
        // TODO: getURI.getPort() returns -1 (because the server hasn't started yet?), we should find a better fix for this
        ServerConnector c = (ServerConnector) _server.getConnectors()[0];
        return "http://" + _server.getURI().getHost() + ":" + (_server.getURI().getPort() > -1 ? _server.getURI().getPort() : c.getPort());
    }

    /**
     * Returns the public WAN Host if behindNAT is true. If behindNAT is false, the value of host is returned.
     *
     * @return The public WAN Host
     */
    public String getPublicWANHost() {
        if (behindNAT) return publicWANHost;
        return host;
    }

    /**
     * Returns the public WAN Port if behindNAT is true. If behindNAT is false, the value of port is returned.
     *
     * @return The public WAN Port
     */
    public Integer getPublicWANPort() {
        if (behindNAT) return publicWANPort;
        return port;
    }

    /**
     * Registers the specified ServiceConnection to the ProtocolServer
     *
     * @param webServiceConnector: The ServiceConnection you wish to register.
     */
    public synchronized void registerService(ServiceConnection webServiceConnector) {
        _services.add(webServiceConnector);
    }

    /**
     * Unregisters the specified ServiceConnection from the ProtocolServer
     *
     * @param webServiceConnector: The ServiceConnection you wish to remove.
     */
    public synchronized void removeService(ServiceConnection webServiceConnector) {
        _services.remove(webServiceConnector);
    }

    /**
     * Add a standard serverconnector to the server instance.
     *
     * @param address The IP address you wish to bind the serverconnector to
     * @param port    The port you with to bind the serverconnector to
     */
    public void addStandardConnector(String address, int port) {
        ServerConnector connector = new ServerConnector(_server);
        connector.setHost(address);
        if (port == 80) {
            log.warn("You have requested to use port 80. This will not work unless you are running as root." +
                    "Are you running as root? You shouldn't. Reroute port 80 to 8080 instead.");
        }
        connector.setPort(port);
        _connectors.add(connector);
        _server.addConnector(connector);
    }

    /**
     * Add a predefined serverconnector to the server instance.
     *
     * @param connector A jetty ServerConnector
     */
    public void addConnector(Connector connector) {
        _connectors.add(connector);
        this._server.addConnector(connector);
    }

    /**
     * Fetch the WSNRequestParser object
     *
     * @return WSNRequestParser
     */
    public WSNRequestParser getRequestParser() {
        return this._requestParser;
    }

    // This is the HTTP Handler that the WSNServer uses to process all incoming requests
    private class HttpHandler extends AbstractHandler {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                           HttpServletResponse response) throws IOException, ServletException {

            log.debug("HttpHandle invoked on target: " + target);

            // Do some stats.
            totalRequests.incrementAndGet();

            boolean isChunked = false;

            Enumeration headerNames = request.getHeaderNames();

            log.debug("Checking headers...");

            // Check the request headers, check for chunked encoding
            while (headerNames.hasMoreElements()) {
                String outMessage = (String) headerNames.nextElement();
                Enumeration returnMessage = request.getHeaders(outMessage);

                while (returnMessage.hasMoreElements()) {
                    String inputStream = (String) returnMessage.nextElement();
                    if (outMessage.equals("Transfer-Encoding") && inputStream.equals("chunked")) {
                        log.debug("Found Transfer-Encoding was chunked.");
                        isChunked = true;
                    }
                }
            }

            log.debug("Accepted message, trying to instantiate WSNu InternalMessage");

            // Get message content, if any
            InternalMessage outgoingMessage;
            if (request.getContentLength() > 0) {
                InputStream inputStream = request.getInputStream();
                outgoingMessage = new InternalMessage(InternalMessage.STATUS_OK | InternalMessage.STATUS_HAS_MESSAGE, inputStream);
            } else if (isChunked) {
                InputStream chunkedInputStream = request.getInputStream();
                StringWriter swriter = new StringWriter();
                IOUtils.copy(chunkedInputStream, swriter);
                String rawRequest = swriter.toString();
                log.debug(rawRequest);
                outgoingMessage = new InternalMessage(
                        InternalMessage.STATUS_OK | InternalMessage.STATUS_HAS_MESSAGE,
                        new ByteArrayInputStream(rawRequest.getBytes()));
            } else {
                outgoingMessage = new InternalMessage(InternalMessage.STATUS_OK, null);
            }

            log.debug("WSNInternalMessage: " + outgoingMessage);

            // Update the request information object
            outgoingMessage.getRequestInformation().setEndpointReference(request.getRemoteHost());
            outgoingMessage.getRequestInformation().setRequestURL(request.getRequestURI());
            outgoingMessage.getRequestInformation().setParameters(request.getParameterMap());

            log.debug("EndpointReference: " + outgoingMessage.getRequestInformation().getEndpointReference());
            log.debug("Request URI: " + outgoingMessage.getRequestInformation().getRequestURL());

            log.debug("Forwarding message to requestParser...");

            // Push the outgoingMessage to the request parser. Based on the status flags of the return message
            // we should know what has happened, and which response we should send.
            InternalMessage returnMessage = null;
            try {
                returnMessage = WSNotificationServer.this._requestParser.parseMessage(outgoingMessage, response.getOutputStream());
            } catch (Exception e) {
                log.error("Uncaught exception: " + e.getMessage());
                log.trace(e.getStackTrace());
            }

            // Improper response from WSNRequestParser! FC WHAT DO?
            if (returnMessage == null) {
                response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
                totalErrors.incrementAndGet();
                baseRequest.setHandled(true);
                returnMessage = new InternalMessage(InternalMessage.STATUS_FAULT_INTERNAL_ERROR, null);
            }

            /* Handle possible errors */
            if ((returnMessage.statusCode & InternalMessage.STATUS_FAULT) > 0) {

                /* Have we got an error message to return? */
                if ((returnMessage.statusCode & InternalMessage.STATUS_HAS_MESSAGE) > 0) {
                    response.setContentType("application/soap+xml;charset=utf-8");

                    // Declare input and output streams
                    InputStream inputStream = (InputStream) returnMessage.getMessage();
                    OutputStream outputStream = response.getOutputStream();

                    // Pipe the data from input to output stream
                    ByteStreams.copy(inputStream, outputStream);

                    // Set proper HTTP status, flush the output stream and set the handled flag
                    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
                    outputStream.flush();
                    baseRequest.setHandled(true);
                    totalErrors.incrementAndGet();

                    return;
                }

                /* If no valid destination was found for the request (Endpoint non-existant) */
                if ((returnMessage.statusCode & InternalMessage.STATUS_FAULT_INVALID_DESTINATION) > 0) {
                    response.setStatus(HttpStatus.NOT_FOUND_404);
                    baseRequest.setHandled(true);
                    totalBadRequests.incrementAndGet();

                    return;

                /* If there was an internal server error */
                } else if ((returnMessage.statusCode & InternalMessage.STATUS_FAULT_INTERNAL_ERROR) > 0) {
                    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
                    baseRequest.setHandled(true);
                    totalErrors.incrementAndGet();

                    return;

                /* If there was syntactical errors or otherwise malformed request content */
                } else if ((returnMessage.statusCode & InternalMessage.STATUS_FAULT_INVALID_PAYLOAD) > 0) {
                    response.setStatus(HttpStatus.BAD_REQUEST_400);
                    baseRequest.setHandled(true);
                    totalBadRequests.incrementAndGet();

                    return;

                /* If the requested method or access to endpoint is forbidden */
                } else if ((returnMessage.statusCode & InternalMessage.STATUS_FAULT_ACCESS_NOT_ALLOWED) > 0) {
                    response.setStatus(HttpStatus.FORBIDDEN_403);
                    baseRequest.setHandled(true);
                    totalBadRequests.incrementAndGet();

                    return;
                }

                /*
                    Otherwise, there has been an exception of some sort with no message attached,
                    and we will reply with a server error
                */
                response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
                baseRequest.setHandled(true);
                totalErrors.incrementAndGet();

                // Check if we have status=OK and also we have a message
            } else if (((InternalMessage.STATUS_OK & returnMessage.statusCode) > 0) &&
                    (InternalMessage.STATUS_HAS_MESSAGE & returnMessage.statusCode) > 0) {

                /* Liar liar pants on fire */
                if (returnMessage.getMessage() == null) {

                    log.error("The HAS_RETURNING_MESSAGE flag was checked, but there was no returning message content");
                    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
                    baseRequest.setHandled(true);
                    totalErrors.incrementAndGet();

                    return;
                }

                // Prepare the response content type
                response.setContentType("application/soap+xml;charset=utf-8");

                // Allocate the input and output streams
                InputStream inputStream = (InputStream) returnMessage.getMessage();
                OutputStream outputStream = response.getOutputStream();

                /* Copy the contents of the input stream into the output stream */
                ByteStreams.copy(inputStream, outputStream);

                /* Set proper OK status and flush out the stream for response to be sent */
                response.setStatus(HttpStatus.OK_200);
                outputStream.flush();

                baseRequest.setHandled(true);

            /* Everything is fine, and nothing is expected */
            } else if ((InternalMessage.STATUS_OK & returnMessage.statusCode) > 0) {

                response.setStatus(HttpStatus.OK_200);
                baseRequest.setHandled(true);

            } else {
                // We obviously should never land in this block, hence we set the 500 status.
                log.error("HandleMessage: The message returned to the WSNotificationServer was not flagged with either STATUS_OK or" +
                        "STATUS_FAULT. Please set either of these flags at all points");

                response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
                baseRequest.setHandled(true);
                totalErrors.incrementAndGet();

            }
        }
    }

    public InternalMessage sendMessage(InternalMessage message) {

        // Fetch the requestInformation from the message, and extract the endpoint
        RequestInformation requestInformation = message.getRequestInformation();
        String endpoint = requestInformation.getEndpointReference();

        /* If we have nowhere to send the message */
        if (endpoint == null) {
            log.error("Endpoint reference not set");
            totalErrors.incrementAndGet();
            return new InternalMessage(InternalMessage.STATUS_FAULT, null);
        }

        /* Create the actual http-request*/
        org.eclipse.jetty.client.api.Request request = _client.newRequest(requestInformation.getEndpointReference());
        request.timeout(connectionTimeout, TimeUnit.SECONDS);

        /* Try to send the message */
        try {
            /* Raw request */
            if ((message.statusCode & InternalMessage.STATUS_HAS_MESSAGE) == 0) {

                request.method(HttpMethod.GET);

                log.debug("Sending message without content to " + requestInformation.getEndpointReference());
                ContentResponse response = request.send();
                totalRequests.incrementAndGet();

                return new InternalMessage(InternalMessage.STATUS_OK | InternalMessage.STATUS_HAS_MESSAGE, response.getContentAsString());
            /* Request with message */
            } else {

                // Set proper request method
                request.method(HttpMethod.POST);

                // If the statusflag has set a message and it is not an input stream
                if ((message.statusCode & InternalMessage.STATUS_MESSAGE_IS_INPUTSTREAM) == 0) {
                    log.error("sendMessage(): " + "The message contained something else than an inputStream." +
                            "Please convert your message to an InputStream before calling this methbod.");

                    return new InternalMessage(InternalMessage.STATUS_FAULT | InternalMessage.STATUS_FAULT_INVALID_PAYLOAD, null);

                } else {

                    // Check if we should have had a message, but there was none
                    if (message.getMessage() == null) {
                        log.error("No content was found to send");
                        totalErrors.incrementAndGet();
                        return new InternalMessage(InternalMessage.STATUS_FAULT | InternalMessage.STATUS_FAULT_INVALID_PAYLOAD, null);
                    }

                    // Send the request to the specified endpoint reference
                    log.info("Sending message with content to " + requestInformation.getEndpointReference());
                    InputStream msg = (InputStream) message.getMessage();
                    request.content(new InputStreamContentProvider(msg), "application/soap+xml; charset=utf-8");

                    ContentResponse response = request.send();
                    totalMessagesSent.incrementAndGet();

                    // Check what HTTP status we received, if is not A-OK, flag the internalmessage as fault
                    // and make the response content the message of the InternalMessage returned
                    if (!HttpStatus.isSuccess(response.getStatus())) {
                        totalBadRequests.incrementAndGet();
                        return new InternalMessage(InternalMessage.STATUS_FAULT | InternalMessage.STATUS_HAS_MESSAGE, response.getContentAsString());
                    } else {
                        return new InternalMessage(InternalMessage.STATUS_OK | InternalMessage.STATUS_HAS_MESSAGE, response.getContentAsString());
                    }
                }
            }
        } catch (ClassCastException e) {
            log.error("sendMessage(): The message contained something else than an inputStream." +
                    "Please convert your message to an InputStream before calling this method.");
            totalErrors.incrementAndGet();

            return new InternalMessage(InternalMessage.STATUS_FAULT | InternalMessage.STATUS_FAULT_INVALID_PAYLOAD, null);

        } catch (Exception e) {
            totalErrors.incrementAndGet();
            e.printStackTrace();
            log.error("sendMessage(): Unable to establish connection: " + e.getMessage());
            return new InternalMessage(InternalMessage.STATUS_FAULT_INTERNAL_ERROR, null);
        }
    }

    public boolean addRelay(String relay, String host, Integer port, String topic, Soap.SoapVersion version) {
        final Set<String> localRelays = new HashSet<String>() {{
            add("127.0.0.1");
            add("0.0.0.0");
            add("localhost");
        }};

        if(relays.contains(relay)) return false;

        // if relay.host == 0.0.0 etc sjekk port
        if (localRelays.contains(host)) {
            log.debug("Same host, need to check port");
            if (getPort() == port) {
                log.debug("Same port, invalid relay command");
                return false;
            }
            // else sjekk relay.host mot publicWANHost, så sjekk port
        } else if (host.equals(getPublicWANHost())) {
            log.info("Same host (WAN), need to check port");
            if (getPublicWANPort() == port) {
                log.info("Same port (WAN), invalid relay command");
                return false;
            }
        }

        if (!relay.startsWith("http://") && !relay.startsWith("https://")) {
            relay = "http://" + relay;
        }

        String subscriptionReference = WSNTools.extractSubscriptionReferenceFromRawXmlResponse(
                sendMessage(
                        WSNTools.generateSubscriptionRequestWithTopic(
                                relay,
                                topic, // Topic
                                getURI(),
                                null, // Termination time
                                version
                        )
                )
        );

        if (subscriptionReference == null) {
            log.debug("Relay could not be created");
            return false;

        }
        relays.add(subscriptionReference);
        log.debug("Relay added");
        return true;
    }

    public boolean deleteRelay(String relay) {
        if (relays.contains(relay)) {
            WsnUtilities.sendUnsubscribeRequest(relay, getRequestParser());
            relays.remove(relay);
            log.debug("Removed relay: " + relay);
            return true;
        } else {
            log.debug("Unable to remove relay: " + relay);
            return false;
        }
    }

    public void deleteAllRelays() {
        relays.forEach((v) -> {
            WsnUtilities.sendUnsubscribeRequest(v, getRequestParser());
            log.debug("Removed relay: " + v);
        });

        relays.clear();
    }

    public Set<String> getRelays() {
        return relays;
    }
}


text:
ACCEPTED

#### According to
The Catalogue of Life, 3rd January 2011

#### Published in
Anal. Soc. cient. argent. 19: 92 (1885)

#### Original name
Phyllachora amphigena Speg., 1885

### Remarks
null

redpajama_set_name: RedPajamaGithub


text:
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/web_applications/face_ml_system_web_app_info.h"

#include <memory>

#include "ash/constants/ash_features.h"
#include "ash/webui/face_ml_app_ui/url_constants.h"
#include "ash/webui/grit/ash_face_ml_app_resources.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/apps/app_service/app_launch_params.h"
#include "chrome/browser/ash/web_applications/system_web_app_install_utils.h"
#include "chrome/browser/web_applications/user_display_mode.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "third_party/blink/public/mojom/manifest/display_mode.mojom.h"
#include "ui/chromeos/styles/cros_styles.h"
#include "ui/display/screen.h"

namespace {
constexpr gfx::Size DEFAULT_SIZE(800, 600);
}  // namespace

std::unique_ptr<WebAppInstallInfo> CreateWebAppInfoForFaceMLApp() {
  std::unique_ptr<WebAppInstallInfo> info =
      std::make_unique<WebAppInstallInfo>();
  info->start_url = GURL(ash::kChromeUIFaceMLAppURL);
  info->scope = GURL(ash::kChromeUIFaceMLAppURL);
  // TODO(b/239374316): Convert the title to a localized string
  info->title = u"Face ML";
  web_app::CreateIconInfoForSystemWebApp(
      info->start_url,
      {
          {"app_icon_192.png", 192, IDR_ASH_FACE_ML_APP_APP_ICON_192_PNG},
          {"app_icon_512.png", 512, IDR_ASH_FACE_ML_APP_APP_ICON_512_PNG},
      },
      *info);

  // Support Dark/Light mode.
  if (chromeos::features::IsDarkLightModeEnabled()) {
    info->theme_color = cros_styles::ResolveColor(
        cros_styles::ColorName::kBgColor, /*is_dark_mode=*/false);
    info->dark_mode_theme_color =
        cros_styles::ResolveColor(cros_styles::ColorName::kBgColor,
                                  /*is_dark_mode=*/true);
    info->background_color = info->theme_color;
    info->dark_mode_background_color = info->dark_mode_theme_color;
  } else {
    info->theme_color = 0xffffffff;
    info->background_color = 0xffffffff;
  }

  info->display_mode = blink::mojom::DisplayMode::kStandalone;
  info->user_display_mode = web_app::UserDisplayMode::kStandalone;
  return info;
}

FaceMLSystemAppDelegate::FaceMLSystemAppDelegate(Profile* profile)
    : ash::SystemWebAppDelegate(ash::SystemWebAppType::FACE_ML,
                                "FaceML",
                                GURL(ash::kChromeUIFaceMLAppURL),
                                profile) {}

std::unique_ptr<WebAppInstallInfo> FaceMLSystemAppDelegate::GetWebAppInfo()
    const {
  return CreateWebAppInfoForFaceMLApp();
}

gfx::Rect FaceMLSystemAppDelegate::GetDefaultBounds(Browser* browser) const {
  gfx::Rect bounds =
      display::Screen::GetScreen()->GetDisplayForNewWindows().work_area();
  bounds.ClampToCenteredSize(DEFAULT_SIZE);
  return bounds;
}

bool FaceMLSystemAppDelegate::IsAppEnabled() const {
  return base::FeatureList::IsEnabled(ash::features::kFaceMLApp);
}

bool FaceMLSystemAppDelegate::ShouldCaptureNavigations() const {
  return true;
}

bool FaceMLSystemAppDelegate::ShouldShowNewWindowMenuOption() const {
  return false;
}

bool FaceMLSystemAppDelegate::ShouldReuseExistingWindow() const {
  return true;
}


text:
package org.jabref.logic.exporter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StringSaveSession extends SaveSession {

    private static final Log LOGGER = LogFactory.getLog(StringSaveSession.class);

    private final ByteArrayOutputStream outputStream;

    public StringSaveSession(Charset encoding, boolean backup) {
        this(encoding, backup, new ByteArrayOutputStream());
    }

    private StringSaveSession(Charset encoding, boolean backup, ByteArrayOutputStream outputStream) {
        super(encoding, backup, new VerifyingWriter(outputStream, encoding));
        this.outputStream = outputStream;
    }

    public String getStringValue() {
        try {
            return outputStream.toString(encoding.name());
        } catch (UnsupportedEncodingException e) {
            LOGGER.warn(e);
            return "";
        }
    }

    @Override
    public void commit(Path file) throws SaveException {
        try {
            Files.write(file, outputStream.toByteArray());
        } catch (IOException e) {
            throw new SaveException(e);
        }
    }

    @Override
    public void cancel() {
        try {
            outputStream.close();
        } catch (IOException e) {
            LOGGER.warn(e);
        }
    }
}


text:
    <title>CasePro - Problem - Uh oh!</title>

Sometimes bad things happen to good people.

While we do our best to write bug-free code, every now and again things do go wrong. Good news though!
We've already been notified of the problem.

{% if request.sentry.id %}

        If you'd like to contact us about this error, please be sure to include your reference number
        {{ request.sentry.id }}

{% endif %}


text:
@interface TVGShowListViewController : TVGBaseViewController <UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong) BKTableViewDatasource *datasource;
@property (strong, nonatomic) NSIndexPath *selectedIndexPath;

@end


text:
class XlsxWriter
  class Workbook < Xml
    def relative_path
      "xl/workbook.xml"
    end
  end
end


text:
package monix.eval

import monix.execution.exceptions.DummyException
import monix.execution.internal.Platform
import scala.concurrent.duration._
import scala.util.{Failure, Success}

object TaskParSequenceSuite extends BaseTestSuite {
  test("Task.parSequence should execute in parallel for async tasks") { implicit s =>
    val seq = Seq(
      Task.evalAsync(1).delayExecution(2.seconds),
      Task.evalAsync(2).delayExecution(1.second),
      Task.evalAsync(3).delayExecution(3.seconds))
    val f = Task.parSequence(seq).runToFuture

    s.tick()
    assertEquals(f.value, None)
    s.tick(2.seconds)
    assertEquals(f.value, None)
    s.tick(1.second)
    assertEquals(f.value, Some(Success(Seq(1, 2, 3))))
  }

  test("Task.parSequence should onError if one of the tasks terminates in error") { implicit s =>
    val ex = DummyException("dummy")
    val seq = Seq(
      Task.evalAsync(3).delayExecution(3.seconds),
      Task.evalAsync(2).delayExecution(1.second),
      Task.evalAsync(throw ex).delayExecution(2.seconds),
      Task.evalAsync(3).delayExecution(1.seconds)
    )

    val f = Task.parSequence(seq).runToFuture

    s.tick()
    assertEquals(f.value, None)
    s.tick(2.seconds)
    assertEquals(f.value, Some(Failure(ex)))
  }

  test("Task.parSequence should be canceled") { implicit s =>
    val seq = Seq(
      Task.evalAsync(1).delayExecution(2.seconds),
      Task.evalAsync(2).delayExecution(1.second),
      Task.evalAsync(3).delayExecution(3.seconds))
    val f = Task.parSequence(seq).runToFuture

    s.tick()
    assertEquals(f.value, None)
    s.tick(2.seconds)
    assertEquals(f.value, None)

    f.cancel()
    s.tick(1.second)
    assertEquals(f.value, None)
  }

  test("Task.parSequence should be stack safe for synchronous tasks") { implicit s =>
    val count = if (Platform.isJVM) 200000 else 5000
    val tasks = for (_ <- 0 until count) yield Task.now(1)
    val composite = Task.parSequence(tasks).map(_.sum)
    val result = composite.runToFuture
    s.tick()
    assertEquals(result.value, Some(Success(count)))
  }

  test("Task.parSequence runAsync multiple times") { implicit s =>
    var effect = 0
    val task1 = Task.evalAsync { effect += 1; 3 }.memoize
    val task2 = task1 map { x =>
      effect += 1; x + 1
    }
    val task3 = Task.parSequence(List(task2, task2, task2))

    val result1 = task3.runToFuture; s.tick()
    assertEquals(result1.value, Some(Success(List(4, 4, 4))))
    assertEquals(effect, 1 + 3)

    val result2 = task3.runToFuture; s.tick()
    assertEquals(result2.value, Some(Success(List(4, 4, 4))))
    assertEquals(effect, 1 + 3 + 3)
  }
}


text:
import logging
import os
import numpy as np
import torch
from tqdm import tqdm

from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture
from diptest import diptest as dp

import yass
from yass import read_config
from yass.reader import READER
from yass.residual.residual_gpu import RESIDUAL_GPU2
from yass.deconvolve.utils import shift_svd_denoise
from yass import postprocess

def run_post_deconv_split(output_directory,
                          fname_templates,
                          fname_spike_train,
                          fname_shifts,
                          fname_scales,
                          fname_raw,
                          raw_dtype,
                          fname_residual,
                          residual_dtype,
                          residual_offset=0,
                          initial_batch=False):

    CONFIG = read_config()

    # output folder
    if not os.path.exists(output_directory):
        os.makedirs(output_directory)

    # reader
    if CONFIG.deconvolution.deconv_gpu:
        n_sec_chunk = CONFIG.resources.n_sec_chunk_gpu_deconv
    else:
        n_sec_chunk = CONFIG.resources.n_sec_chunk
    reader_residual = READER(fname_residual,
                             residual_dtype,
                             CONFIG,
                             n_sec_chunk,
                             offset=residual_offset)

    reader_raw = READER(fname_raw,
                        raw_dtype,
                        CONFIG)

    # load input data
    templates = np.load(fname_templates)
    spike_train = np.load(fname_spike_train)
    shifts = np.load(fname_shifts)
    scales = np.load(fname_scales)

    # get cleaned ptp
    fname_cleaned_ptp = os.path.join(output_directory, 'cleaned_ptp.npy')
    fname_spike_times = os.path.join(output_directory, 'spike_times.npy')
    fname_shifts = os.path.join(output_directory, 'shifts_list.npy')
    fname_scales = os.path.join(output_directory, 'scales_list.npy')
    fname_vis_chans = os.path.join(output_directory, 'vis_chans.npy')
    if os.path.exists(fname_cleaned_ptp) and os.path.exists(fname_spike_times):

        cleaned_ptp = np.load(fname_cleaned_ptp, allow_pickle=True)
        spike_times_list = np.load(fname_spike_times, allow_pickle=True)
        shifts_list = np.load(fname_shifts, allow_pickle=True)
        scales_list = np.load(fname_scales, allow_pickle=True)
        vis_chans = np.load(fname_vis_chans, allow_pickle=True)
    else:
        print('get cleaned ptp')
        (cleaned_ptp, spike_times_list,
         shifts_list, scales_list, vis_chans) = get_cleaned_ptp(
            templates, spike_train, shifts, scales,
            reader_residual, fname_templates, CONFIG)

        np.save(fname_shifts, shifts_list, allow_pickle=True)
        np.save(fname_scales, scales_list, allow_pickle=True)
        np.save(fname_vis_chans, vis_chans, allow_pickle=True)
        np.save(fname_cleaned_ptp, cleaned_ptp, allow_pickle=True)
        np.save(fname_spike_times, spike_times_list, allow_pickle=True)

    # split units
    fname_templates_updated = os.path.join(
        output_directory, 'templated_updated.npy')
    fname_spike_train_updated = os.path.join(
        output_directory, 'spike_train_updated.npy')
    fname_shifts_updated = os.path.join(
        output_directory, 'shifts_updated.npy')
    fname_scales_updated = os.path.join(
        output_directory, 'scales_updated.npy')
    if not(os.path.exists(fname_templates_updated) and
           os.path.exists(fname_spike_train_updated)):

        print('run split')
        if initial_batch:
            update_original_templates = True
            min_fr_accept = 0
            min_ptp_accept = 0
            min_fraction_accept = 0
        else:
            update_original_templates = False
            min_fr_accept = 0.5
            min_ptp_accept = 10000
            min_fraction_accept = 0.15
        (templates_updated, spike_train_updated,
         shifts_updated, scales_updated) = run_split(
            cleaned_ptp, spike_times_list,
            shifts_list, scales_list, vis_chans,
            templates, spike_train, reader_raw,
            CONFIG,
            update_original_templates=update_original_templates,
            min_ptp_accept=min_ptp_accept,
            min_fr_accept=min_fr_accept,
            min_fraction_accept=min_fraction_accept)

        # denoise split units
        n_splits = templates_updated.shape[0] - templates.shape[0]
        vis_threshold_strong = 1.
        vis_threshold_weak = 0.5
        rank = 5
        pad_len = int(1.5 * CONFIG.recordings.sampling_rate / 1000.)
        jitter_len = pad_len
        split_templates_denoised = shift_svd_denoise(
            templates_updated[-n_splits:], CONFIG,
            vis_threshold_strong, vis_threshold_weak,
            rank, pad_len, jitter_len)
        templates_updated[-n_splits:] = split_templates_denoised

        ## add new templates and spike train to the existing one
        #templates_updated = np.concatenate((templates, new_temps), axis=0)
        #spike_train_new[:, 1] += templates.shape[0]
        #spike_train_updated = np.concatenate((spike_train, spike_train_new), axis=0)
        idx_sort = np.argsort(spike_train_updated[:, 0])
        spike_train_updated = spike_train_updated[idx_sort]
        shifts_updated = shifts_updated[idx_sort]
        scales_updated = scales_updated[idx_sort]

        np.save(fname_templates_updated, templates_updated)
        np.save(fname_spike_train_updated, spike_train_updated)
        np.save(fname_shifts_updated, shifts_updated)
        np.save(fname_scales_updated, scales_updated)

        # can be used to find gpu memory not freed
        # import gc
        #n_objects = 0
        #for obj in gc.get_objects():
        #    try:
        #        if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
        #            print(obj, type(obj), obj.size())
        #
        #            n_objects += 1
        #    except:
        #        pass
        #print(n_objects)

        #units_to_process = np.arange(templates.shape[0], templates_updated.shape[0])

    #else:
    #    templates_updated = np.load(fname_templates_updated)
    #    units_to_process = np.arange(templates.shape[0], templates_updated.shape[0])

    ## kill duplicate templates
    #methods = ['low_ptp', 'duplicate']
    #(fname_templates_out, fname_spike_train_out,
    # _, _, _)  = postprocess.run(
    #    methods,
    #    os.path.join(output_directory,
    #                 'duplicate_remove'),
    #    None,
    #    None,
    #    fname_templates_updated,
    #    fname_spike_train_updated,
    #    None,
    #    None,
    #    None,
    #    None,
    #    units_to_process)

    #return fname_templates_out, fname_spike_train_out

    return (fname_templates_updated, fname_spike_train_updated,
            fname_shifts_updated, fname_scales_updated)

def get_cleaned_ptp(templates, spike_train, shifts, scales,
                    reader_residual, fname_templates, CONFIG):

    n_units, n_times, n_channels = templates.shape

    ptp = templates.ptp(1)
    ptp_max = ptp.max(1)
    vis_chans = [None]*n_units
    for k in range(n_units):
        vis_chans[k] = np.where(ptp[k] > 0)[0]

    min_max_loc = np.stack((templates.argmin(1),
                            templates.argmax(1))).transpose(1, 0, 2)
    min_max_loc[min_max_loc < 2] = 2
    min_max_loc[min_max_loc > n_times -3] = n_times - 3

    # move to gpu
    templates = torch.from_numpy(templates).float().cuda()
    spike_train = torch.from_numpy(spike_train).long().cuda()
    shifts = torch.from_numpy(shifts).float().cuda()
    scales = torch.from_numpy(scales).float().cuda()
    min_max_loc = torch.from_numpy(min_max_loc).long().cuda()

    residual_comp = RESIDUAL_GPU2(
        None, CONFIG, None, None, None,
        None, None, None, None, None, True)
    residual_comp.load_templates(fname_templates)
    residual_comp.make_bsplines_parallel()

    cleaned_ptp = [None]*n_units
    spike_times_list = [None]*n_units
    shifts_list = [None]*n_units
    scales_list = [None]*n_units
    for batch_id in tqdm(range(reader_residual.n_batches)):
        t_start, t_end = reader_residual.idx_list[batch_id]
        batch_offset = int(t_start - reader_residual.buffer)
        idx_in = torch.nonzero((spike_train[:, 0] > t_start)&(spike_train[:, 0] <= t_end))[:, 0]

        spike_times_batch = spike_train[idx_in, 0]
        spike_times_batch -= batch_offset
        neuron_ids_batch = spike_train[idx_in, 1]

        shifts_batch = shifts[idx_in]
        scales_batch = scales[idx_in]

        # get residual batch
        residual = reader_residual.read_data_batch(batch_id, add_buffer=True)
        residual = torch.from_numpy(residual).cuda()

        # get min/max values
        # due to memory constraint, find the values 10,000 spikes at a time
        max_spikes = 5000
        counter = torch.cat((torch.arange(0, len(spike_times_batch), max_spikes),
                             torch.tensor([len(spike_times_batch)]))).cuda()

        # get the values near template min/max locations for each channel
        # min_max_vals_spikes: # spikes x 2 (min/max) x # channels x 5 (-2 to 2 of argmin/argmax)
        # this will get min/max values of cleaned spikes.
        # it will first get residual values and then add templates
        #min_max_vals_spikes = torch.cuda.FloatTensor(len(spike_times_batch), 2, n_channels).fill_(0)
        #for j in range(len(counter)-1):
        #    ii_start = counter[j]
        #    ii_end = counter[j+1]

        #    min_max_loc_spikes = (min_max_loc[neuron_ids_batch[ii_start:ii_end]] +
        #                          spike_times_batch[ii_start:ii_end, None, None] - n_times//2)
        #    min_max_vals_spikes[ii_start:ii_end] = torch.gather(
        #        residual, 0, min_max_loc_spikes.reshape(-1, n_channels)).reshape(
        #        -1, 2, n_channels)

        #    shifted_templates = residual_comp.get_shifted_templates(
        #            neuron_ids_batch[ii_start:ii_end],
        #            shifts_batch[ii_start:ii_end],
        #            scales_batch[ii_start:ii_end])
        #    min_max_vals_spikes[ii_start:ii_end] += torch.gather(
        #        shifted_templates, 1, min_max_loc[neuron_ids_batch[ii_start:ii_end]])
        #ptp_batch = min_max_vals_spikes[:,1] - min_max_vals_spikes[:,0]
        min_max_vals_spikes = torch.cuda.FloatTensor(len(spike_times_batch), 2, 5, n_channels).fill_(0)
        for j in range(len(counter)-1):
            ii_start = counter[j]
            ii_end = counter[j+1]

            min_max_loc_spikes = (min_max_loc[neuron_ids_batch[ii_start:ii_end]] +
                                  spike_times_batch[ii_start:ii_end, None, None] - n_times//2)
            min_max_loc_spikes = min_max_loc_spikes[:, :, None] + torch.arange(-2, 3).cuda()[None, None, :, None]
            min_max_vals_spikes[ii_start:ii_end] = torch.gather(
                residual, 0, min_max_loc_spikes.reshape(-1, n_channels)).reshape(
                -1, 2, 5, n_channels)

            shifted_templates = residual_comp.get_shifted_templates(
                    neuron_ids_batch[ii_start:ii_end],
                    shifts_batch[ii_start:ii_end],
                    scales_batch[ii_start:ii_end])
            min_max_loc_spikes = min_max_loc[
                neuron_ids_batch[ii_start:ii_end]][
                :, :, None] + torch.arange(-2, 3).cuda()[None, None, :, None]
            min_max_vals_spikes[ii_start:ii_end] += torch.gather(
                shifted_templates,
                1,
                min_max_loc_spikes.reshape(-1, 10, n_channels)
            ).reshape(-1, 2, 5, n_channels)
        ptp_batch = torch.max(min_max_vals_spikes[:,1], 1)[0] - torch.min(min_max_vals_spikes[:,0], 1)[0]

        del min_max_loc_spikes
        del min_max_vals_spikes
        del shifted_templates
        del residual
        torch.cuda.empty_cache()

        for k in range(n_units):
            idx_ = neuron_ids_batch == k
            if cleaned_ptp[k] is not None:
                cleaned_ptp[k] = np.concatenate(
                    (cleaned_ptp[k], ptp_batch[idx_].cpu().numpy()[:, vis_chans[k]]),
                    axis = 0)
                spike_times_list[k] = np.hstack((spike_times_list[k],
                                                 spike_times_batch[idx_].cpu().numpy() + batch_offset))
                shifts_list[k] = np.hstack((shifts_list[k],
                                            shifts_batch[idx_].cpu().numpy()))
                scales_list[k] = np.hstack((scales_list[k],
                                            scales_batch[idx_].cpu().numpy()))
            else:
                cleaned_ptp[k] = ptp_batch[idx_].cpu().numpy()[:, vis_chans[k]]
                spike_times_list[k] = spike_times_batch[idx_].cpu().numpy() + batch_offset
                shifts_list[k] = shifts_batch[idx_].cpu().numpy()
                scales_list[k] = scales_batch[idx_].cpu().numpy()

        del ptp_batch
        del spike_times_batch
        del shifts_batch
        del scales_batch
        torch.cuda.empty_cache()

    del templates
    del spike_train
    del shifts
    del scales
    del min_max_loc
    del residual_comp
    torch.cuda.empty_cache()

    return cleaned_ptp, spike_times_list, shifts_list, scales_list, vis_chans

def pca_gmm_and_dip(data, min_data):

    if data.shape[0] < min_data:
        return np.zeros(len(data), 'int32'), 1

    pca = PCA(n_components=1)
    score = pca.fit_transform(data)

    label = np.zeros(len(score), 'int32')

    p_val = dp(score[:, 0])[1]
    if p_val < 0.05:
        g = GaussianMixture(n_components=2)
        label_ = g.fit_predict(score)
        idx_0 = label_==0
        if np.sum(idx_0) == 0 or np.sum(~idx_0) == 0:
            return label, 1
        else:
            return label_, p_val

        label_0 = pca_gmm_and_dip(data[idx_0], min_data)[0]
        label_1 = pca_gmm_and_dip(data[~idx_0], min_data)[0]
        label_1 += np.max(label_0) + 1
        label[idx_0] = label_0
        label[~idx_0] = label_1

    return label, p_val

def run_split(cleaned_ptp, spike_times_list,
              shifts_list, scales_list, vis_chans,
              templates, spike_train, reader_raw, CONFIG,
              update_original_templates=False,
              min_ptp_split=5, min_fr_split=1,
              min_ptp_accept=1000, min_fr_accept=1,
              min_fraction_accept=0.2):

    n_units, n_times, n_channels = templates.shape

    ptp_max = templates.ptp(1).max(1)

    min_fr = CONFIG.cluster.min_fr
    len_rec = spike_train[:,0].ptp()/CONFIG.recordings.sampling_rate
    min_data = int(min_fr*len_rec)

    # minimum location of max channel. it is used to align new split templates
    max_k = templates.ptp(1).max(1).argmax()
    max_k_mc = templates[max_k].ptp(0).argmax()
    max_chan_min_loc = templates[max_k, :, max_k_mc].argmin()

    new_temps = np.zeros((0, n_times, n_channels), 'float32')
    new_spts = []
    new_shifts = []
    new_scales = []
    for k in tqdm(range(len(cleaned_ptp))):

        spt_ = spike_times_list[k]

        if (ptp_max[k] > min_ptp_split) and (len(spt_) > len_rec*min_fr_split):

            shifts_ = shifts_list[k]
            scales_ = scales_list[k]

            idx_sort = np.argsort(spt_)
            cleaned_ptp_unit = cleaned_ptp[k][idx_sort]
            spt_ = spt_[idx_sort]
            shifts_ = shifts_[idx_sort]
            scales_ = scales_[idx_sort]

            isi_ = np.diff(np.hstack((-1000, spt_)))
            cleaned_ptp_unit = cleaned_ptp_unit[isi_ > 300]
            spt_ = spt_[isi_ > 300]
            shifts_ = shifts_[isi_ > 300]
            scales_ = scales_[isi_ > 300]
            #std_ = np.std(cleaned_ptp_unit, 0)

            #if std_.max() > 1.2:
            label, pval = pca_gmm_and_dip(cleaned_ptp_unit, min_data)
            if pval < 0.05:
                unique_label, n_counts = np.unique(label, return_counts=True)
                unique_label = unique_label[n_counts > min_data]
                n_counts = n_counts[n_counts > min_data]

                if len(unique_label) <= 1:
                    continue

                # estimate templates from raw
                new_temps_k = np.zeros((len(unique_label), n_times, n_channels))
                for ii in range(len(unique_label)):
                    spt_temp = spt_[label == unique_label[ii]]
                    new_temps_k[ii] = reader_raw.read_waveforms(spt_temp)[0].mean(0)

                mcs = new_temps_k.ptp(1).argmax(1)
                for ii in range(len(unique_label)):
                    max_chan_min_loc
                    min_loc_k = new_temps_k[ii, :, mcs[ii]].argmin()
                    if  min_loc_k != max_chan_min_loc:
                        spt_temp = spt_[label == unique_label[ii]]
                        spt_temp -= (max_chan_min_loc - min_loc_k)
                        new_temps_k[ii] = reader_raw.read_waveforms(spt_temp)[0].mean(0)

                ptp_new = new_temps_k.ptp(1).max(1)
                idx_keep = np.logical_or(ptp_new > min_ptp_accept,
                                         n_counts > min_fr_accept*len_rec,
                                         n_counts/np.sum(n_counts) > min_fraction_accept)

                unique_label = unique_label[idx_keep]

                if len(unique_label) <= 1:
                    continue

                # find out what matches the orignal unit
                # get ptp of each split unit
                split_units_ptp = np.zeros((len(unique_label), cleaned_ptp_unit.shape[1]))
                for ii in range(len(unique_label)):
                    split_units_ptp[ii] = cleaned_ptp_unit[label == unique_label[ii]].mean(0)
                # compare to the original unit's ptp
                original_unit_ptp = templates[k].ptp(0)[vis_chans[k]]
                split_unit_closest = unique_label[np.sum(np.square(
                    split_units_ptp - original_unit_ptp), 1).argmin()]

                # add spike times
                for ii in unique_label[unique_label != split_unit_closest]:
                    new_spts.append(spt_[label == ii])
                    new_shifts.append(shifts_[label == ii])
                    new_scales.append(scales_[label == ii])

                # save all units other than the closest on as new templates
                new_temps = np.concatenate(
                    (new_temps, new_temps_k[unique_label != split_unit_closest]), axis=0)

                # if the split unit closest to the orignal has enough spikes,
                # the new template replaces the original one
                #if np.sum(label == split_unit_closest) > 300:
                if update_original_templates:
                    templates[k] = new_temps_k[unique_label == split_unit_closest][0]

                spike_times_list[k] = spt_[label == split_unit_closest]
                shifts_list[k] = shifts_[label == split_unit_closest]
                scales_list[k] = scales_[label == split_unit_closest]

    # updated templates
    templates_updated = np.concatenate((templates, new_temps), 0).astype('float32')

    # updated spike train of original units
    spike_train_updated = np.zeros((0, 2), 'int32')
    shifts_updated = np.zeros(0, 'float32')
    scales_updated = np.zeros(0, 'float32')
    for k in range(n_units):
        spike_train_temp = np.vstack(
            (spike_times_list[k], k*np.ones(len(spike_times_list[k]), 'int32'))).T
        spike_train_updated = np.concatenate(
            (spike_train_updated, spike_train_temp), axis=0)
        shifts_updated = np.hstack((shifts_updated, shifts_list[k]))
        scales_updated = np.hstack((scales_updated, scales_list[k]))

    # add new spike train
    spike_train_new = np.zeros((0, 2), 'int32')
    shifts_new = np.zeros(0, 'float32')
    scales_new = np.zeros(0, 'float32')
    for k in range(new_temps.shape[0]):
        spike_train_temp = np.vstack((new_spts[k], k*np.ones(len(new_spts[k]), 'int32'))).T
        spike_train_new = np.concatenate((spike_train_new, spike_train_temp), axis=0)
        shifts_new = np.hstack((shifts_new, new_shifts[k]))
        scales_new = np.hstack((scales_new, new_scales[k]))
    spike_train_new[:, 1] += n_units
    spike_train_updated = np.concatenate((spike_train_updated, spike_train_new), axis=0)
    shifts_updated = np.concatenate((shifts_updated, shifts_new), axis=0)
    scales_updated = np.concatenate((scales_updated, scales_new), axis=0)

    return templates_updated, spike_train_updated, shifts_updated, scales_updated


text:
<?php
/**
 * @author @jenschude <jens.schulze@commercetools.de>
 */

namespace Commercetools\Core\Model\Project;

use Commercetools\Core\Model\Common\JsonObject;
use Commercetools\Core\Model\Common\Collection;
use Commercetools\Core\Model\Common\DateTimeDecorator;
use Commercetools\Core\Model\Message\MessagesConfiguration;
use DateTime;

/**
 * @package Commercetools\Core\Model\Project
 * @link https://docs.commercetools.com/http-api-projects-project.html#project
 * @method string getKey()
 * @method Project setKey(string $key = null)
 * @method string getName()
 * @method Project setName(string $name = null)
 * @method Collection getCountries()
 * @method Project setCountries(Collection $countries = null)
 * @method Collection getCurrencies()
 * @method Project setCurrencies(Collection $currencies = null)
 * @method Collection getLanguages()
 * @method Project setLanguages(Collection $languages = null)
 * @method DateTimeDecorator getCreatedAt()
 * @method Project setCreatedAt(DateTime $createdAt = null)
 * @method DateTimeDecorator getTrialUntil()
 * @method Project setTrialUntil(DateTime $trialUntil = null)
 * @method MessagesConfiguration getMessages()
 * @method Project setMessages(MessagesConfiguration $messages = null)
 * @method int getVersion()
 * @method Project setVersion(int $version = null)
 * @method ShippingRateInputType getShippingRateInputType()
 * @method Project setShippingRateInputType(ShippingRateInputType $shippingRateInputType = null)
 * @method ExternalOAuth getExternalOAuth()
 * @method Project setExternalOAuth(ExternalOAuth $externalOAuth = null)
 * @method CartsConfiguration getCarts()
 * @method Project setCarts(CartsConfiguration $carts = null)
 */
class Project extends JsonObject
{
    public function fieldDefinitions()
    {
        return [
            'version' => [static::TYPE => 'int'],
            'key' => [static::TYPE => 'string'],
            'name' => [static::TYPE => 'string'],
            'countries' => [static::TYPE => Collection::class],
            'currencies' => [static::TYPE => Collection::class],
            'languages' => [static::TYPE => Collection::class],
            'createdAt' => [
                static::TYPE => DateTime::class,
                static::DECORATOR => DateTimeDecorator::class
            ],
            'trialUntil' => [
                static::TYPE => DateTime::class,
                static::DECORATOR => DateTimeDecorator::class
            ],
            'messages' => [static::TYPE => MessagesConfiguration::class],
            'shippingRateInputType' => [static::TYPE => ShippingRateInputType::class],
            'externalOAuth' => [static::TYPE => ExternalOAuth::class],
            'carts' => [static::TYPE => CartsConfiguration::class]
        ];
    }
}


text:
<!doctype html>

<meta charset="utf-8"/>
<title>Node Test</title>

Test Data

        test link
        <input type="text" id="test-input" readonly maxlength="20" value="hello"/>
        <input type="radio" id="test-radio" />
        <input type="radio" id="test-radio2" checked />
        label
        <button type="button" tabindex="3">Submit</button>
        <textarea rows="2" cols="2">
            test
        </textarea>

            td

    <select id="test-select">
        <option id="test-opt" value="1">0</option>
        <option>2</option>
        <option>3</option>
    </select>
    <select id="test-select2">
        <option>2</option>
    </select>
    <select id="test-select3" multiple autocomplete="off">
        <option selected>1</option>
        <option selected>2</option>
        <option>3</option>
    </select>

#test-mouseenter


text:
from astLib import astWCS, astCoords
# astlib requires pyfits (or astropy) in order
# to create a WCS object from a FITS header.
from astropy.io import fits as pyfits

from ginga.util.wcsmod import common

astWCS.NUMPY_MODE = True

coord_types = ['j2000', 'b1950', 'galactic']

class AstLibWCS(common.BaseWCS):
    """
    A WCS interface for astLib.astWCS.WCS
    You need to install python module 'astLib'
    (http://sourceforge.net/projects/astlib)
    if you want to use this version.

    """
    def __init__(self, logger):
        super(AstLibWCS, self).__init__(logger)

        self.kind = 'astlib/wcstools'

    def load_header(self, header, fobj=None):
        self.header = {}
        self.header.update(header.items())

        self.fix_bad_headers()

        try:
            # reconstruct a pyfits header
            hdr = pyfits.Header(header.items())

            self.logger.debug("Trying to make astLib wcs object")
            self.wcs = astWCS.WCS(hdr, mode='pyfits')

            self.coordsys = self.get_coord_system_name(self.header)
            self.logger.debug("Coordinate system is: %s" % (self.coordsys))

        except Exception as e:
            self.logger.error("Error making WCS object: %s" % (str(e)))
            self.wcs = None

    def get_coord_system_name(self, header):
        coordsys = common.get_coord_system_name(header)
        coordsys = coordsys.upper()
        if coordsys in ('FK4',):
            return 'b1950'
        elif coordsys in ('FK5', 'ICRS'):
            return 'j2000'
        elif coordsys in ('PIXEL',):
            return 'pixel'

        #raise common.WCSError("Cannot determine appropriate coordinate system from FITS header")  # noqa
        return 'j2000'

    def spectral_coord(self, idxs, coords='data'):
        raise common.WCSError("This feature not supported by astWCS")

    def pixtoradec(self, idxs, coords='data'):
        if coords == 'fits':
            # Via astWCS.NUMPY_MODE, we've forced pixels referenced from 0
            idxs = tuple(map(lambda x: x - 1, idxs))

        try:
            ra_deg, dec_deg = self.wcs.pix2wcs(idxs[0], idxs[1])

        except Exception as e:
            self.logger.error("Error calculating pixtoradec: %s" % (str(e)))
            raise common.WCSError(e)

        return ra_deg, dec_deg

    def radectopix(self, ra_deg, dec_deg, coords='data', naxispath=None):
        try:
            x, y = self.wcs.wcs2pix(ra_deg, dec_deg)

        except Exception as e:
            self.logger.error("Error calculating radectopix: %s" % (str(e)))
            raise common.WCSError(e)

        if coords == 'fits':
            # Via astWCS.NUMPY_MODE, we've forced pixels referenced from 0
            x, y = x + 1, y + 1

        return (x, y)

    def pixtosystem(self, idxs, system=None, coords='data'):

        if self.coordsys == 'raw':
            raise common.WCSError("No usable WCS")

        if system is None:
            system = 'j2000'

        # Get a coordinates object based on ra/dec wcs transform
        ra_deg, dec_deg = self.pixtoradec(idxs, coords=coords)
        self.logger.debug("ra, dec = %f, %f" % (ra_deg, dec_deg))

        # convert to alternate coord
        try:
            fromsys = self.coordsys.upper()

            if fromsys == 'PIXEL':
                # these are really pixel values
                return (ra_deg, dec_deg)

            tosys = system.upper()

            if fromsys == 'B1950':
                equinox = 1950.0
            else:
                equinox = 2000.0

            lon_deg, lat_deg = astCoords.convertCoords(fromsys, tosys,
                                                       ra_deg, dec_deg,
                                                       equinox)
        except Exception as e:
            raise common.WCSError(
                "Error converting between coordinate systems "
                "'%s' and '%s': %s" % (fromsys, tosys, str(e)))

        return (lon_deg, lat_deg)

# register our WCS with ginga
common.register_wcs('astlib', AstLibWCS, coord_types)


text:
using Skybrud.Social.GitHub.Options.User.Organizations;
using Skybrud.Social.GitHub.Options.User.Repositories;
using Skybrud.Social.GitHub.Responses.Emails;
using Skybrud.Social.GitHub.Responses.Followers;
using Skybrud.Social.GitHub.Responses.Organizations;
using Skybrud.Social.GitHub.Responses.Repositories;
using Skybrud.Social.GitHub.Responses.Users;

namespace Skybrud.Social.GitHub.Endpoints.User {

    ///
    /// Class representing the User endpoint.
    ///
    public class GitHubUserEndpoint {

        #region Properties

        ///
        /// Gets a reference to the GitHub service.
        ///
        public GitHubHttpService Service { get; }

        ///
        /// Gets a reference to the raw endpoint.
        ///
        public GitHubUserRawEndpoint Raw => Service.Client.User;

        #endregion

        #region Constructors

        internal GitHubUserEndpoint(GitHubHttpService service) {
            Service = service;
        }

        #endregion

        #region Member methods

        ///
        /// Gets information about the authenticated user.
        ///
        /// <returns>An instance of <see cref="GitHubUserResponse"/> representing the response.</returns>
        public GitHubUserResponse GetUser() {
            return new GitHubUserResponse(Raw.GetUser());
        }

        ///
        /// Gets a list of email addresses of the authenticated user.
        ///
        /// <returns>An instance of <see cref="GitHubEmailListResponse"/> representing the response.</returns>
        public GitHubEmailListResponse GetEmails() {
            return new GitHubEmailListResponse(Raw.GetEmails());
        }

        ///
        /// Gets a list of users following the authenticated user.
        ///
        /// <returns>An instance of <see cref="GitHubUserListResponse"/> representing the response.</returns>
        public GitHubUserListResponse GetFollowers() {
            return new GitHubUserListResponse(Raw.GetFollowers());
        }

        ///
        /// Gets a list of users the authenticated user is following.
        ///
        /// <returns>An instance of <see cref="GitHubUserListResponse"/> representing the response.</returns>
        public GitHubUserListResponse GetFollowing() {
            return new GitHubUserListResponse(Raw.GetFollowing());
        }

        ///
        /// Gets whether the authenticated user is following the user with the specified <paramref name="username"/>.
        ///
        /// <param name="username">The username (login) of the user.</param>
        /// <returns>An instance of <see cref="GitHubGetFollowingResponse"/> representing the response.</returns>
        public GitHubGetFollowingResponse IsFollowing(string username) {
            return new GitHubGetFollowingResponse(Raw.IsFollowing(username));
        }

        ///
        /// Gets a list of repositories of the authenticated user.
        ///
        /// <returns>An instance of <see cref="GitHubRepositoryListResponse"/> representing the response.</returns>
        public GitHubRepositoryListResponse GetRepositories() {
            return new GitHubRepositoryListResponse(Raw.GetRepositories());
        }

        ///
        /// Gets a list of repositories of the authenticated user.
        ///
        /// <param name="perPage">The maximum amount of organizations to returned by each page. Maximum is <c>100</c>.</param>
        /// <returns>An instance of <see cref="GitHubRepositoryListResponse"/> representing the response.</returns>
        public GitHubRepositoryListResponse GetRepositories(int perPage) {
            return new GitHubRepositoryListResponse(Raw.GetRepositories(perPage));
        }

        ///
        /// Gets a list of repositories of the authenticated user.
        ///
        /// <param name="perPage">The maximum amount of organizations to returned by each page. Maximum is <c>100</c>.</param>
        /// <param name="page">The page to be returned.</param>
        /// <returns>An instance of <see cref="GitHubRepositoryListResponse"/> representing the response.</returns>
        public GitHubRepositoryListResponse GetRepositories(int perPage, int page) {
            return new GitHubRepositoryListResponse(Raw.GetRepositories(perPage, page));
        }

        ///
        /// Gets a list of repositories of the authenticated user.
        ///
        /// <param name="options">The options for the request to the API.</param>
        /// <returns>An instance of <see cref="GitHubRepositoryListResponse"/> representing the response.</returns>
        public GitHubRepositoryListResponse GetRepositories(GitHubGetRepositoriesOptions options) {
            return new GitHubRepositoryListResponse(Raw.GetRepositories(options));
        }

        ///
        /// Gets a list of organizations the authenticated user is a member of.
        ///
        /// <returns>An instance of <see cref="GitHubOrganizationListResponse"/> representing the response.</returns>
        /// <see>
        ///     <cref>https://docs.github.com/en/rest/reference/orgs#list-organizations-for-the-authenticated-user</cref>
        /// </see>
        public GitHubOrganizationListResponse GetOrganizations() {
            return new GitHubOrganizationListResponse(Raw.GetOrganizations());
        }

        ///
        /// Returns a list of organizations the authenticated user is a member of.
        ///
        /// <param name="options">The options for the request to the API.</param>
        /// <returns>An instance of <see cref="GitHubOrganizationListResponse"/> representing the response.</returns>
        /// <see>
        ///     <cref>https://docs.github.com/en/rest/reference/orgs#list-organizations-for-the-authenticated-user</cref>
        /// </see>
        public GitHubOrganizationListResponse GetOrganizations(GitHubGetOrganizationsOptions options) {
            return new GitHubOrganizationListResponse(Raw.GetOrganizations(options));
        }

        #endregion

    }

}


text:
package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public final class MainPanel extends JPanel {
    public MainPanel() {
        super(new GridLayout(2, 1));

        JTextField textField = new JTextField();
//         textField.setCaret(new DefaultCaret() {
//             @Override public void paint(Graphics g) {}
//         });
        ((DefaultCaret) textField.getCaret()).setBlinkRate(0);

        add(makeTitlePanel(new JTextField(), "default"));
        add(makeTitlePanel(textField, "DefaultCaret#setBlinkRate(0)"));
        setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
        setPreferredSize(new Dimension(320, 240));
    }
    private JComponent makeTitlePanel(JComponent cmp, String title) {
        JPanel p = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 1d;
        c.fill    = GridBagConstraints.HORIZONTAL;
        c.insets  = new Insets(5, 5, 5, 5);
        p.add(cmp, c);
        p.setBorder(BorderFactory.createTitledBorder(title));
        return p;
    }

    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
               | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}


text:
"""
Django settings for tutorial project.

Generated by 'django-admin startproject' using Django 1.10.2.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'us%!@&39o250e79)l!4*0ac4oquo+^nm83vp#y%mw9i$7)i&fy'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['35.158.62.226','ec2-35-158-62-226.eu-central-1.compute.amazonaws.com','127.0.0.1']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
	'services'
]

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        #'rest_framework.permissions.IsAdminUser',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [],
    'PAGE_SIZE': 10
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'tutorial.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'tutorial.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'asc_qwerty',
        'USER': 'root',
        'PASSWORD': 'ascqwerty',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }

}

# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'


text:
@implementation ASImageIdAPI

- (NSString *)requestPathUrl{
    return @"/app/content?";
}
-(NSDictionary *)requestParameter{
    return @{@"type":@(0)};
}
- (NSArray *)getImageIds{

    NSLog(@"message = %@",self.response.responseJSONObject);
    return nil;
}
- (Class )responseModelClass{
    return [ExampleModel class];
}
@end


text:
<?php

namespace Sonata\AdminBundle\Admin;

use Knp\Menu\ItemInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Stateless breadcrumbs builder (each method needs an Admin object).
 *
 * @author Grégoire Paris <postmaster@greg0ire.fr>
 */
final class BreadcrumbsBuilder implements BreadcrumbsBuilderInterface
{
    /**
     * @var string[]
     */
    private $config = [];

    /**
     * @param string[] $config
     */
    public function __construct(array $config = [])
    {
        $resolver = new OptionsResolver();
        $this->configureOptions($resolver);

        $this->config = $resolver->resolve($config);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'child_admin_route' => 'edit',
        ]);
    }

    public function getBreadcrumbs(AdminInterface $admin, $action)
    {
        $breadcrumbs = [];
        if ($admin->isChild()) {
            return $this->getBreadcrumbs($admin->getParent(), $action);
        }

        $menu = $this->buildBreadcrumbs($admin, $action);

        do {
            $breadcrumbs[] = $menu;
        } while ($menu = $menu->getParent());

        $breadcrumbs = array_reverse($breadcrumbs);
        array_shift($breadcrumbs);

        return $breadcrumbs;
    }

    /**
     * {@inheritdoc}
     * NEXT_MAJOR : make this method private.
     */
    public function buildBreadcrumbs(AdminInterface $admin, $action, ItemInterface $menu = null)
    {
        if (!$menu) {
            $menu = $admin->getMenuFactory()->createItem('root');

            $menu = $menu->addChild(
                'link_breadcrumb_dashboard',
                [
                    'uri' => $admin->getRouteGenerator()->generate('sonata_admin_dashboard'),
                    'extras' => ['translation_domain' => 'SonataAdminBundle'],
                ]
            );
        }

        $menu = $this->createMenuItem(
            $admin,
            $menu,
            sprintf('%s_list', $admin->getClassnameLabel()),
            $admin->getTranslationDomain(),
            [
                'uri' => $admin->hasRoute('list') && $admin->hasAccess('list') ?
                $admin->generateUrl('list') :
                null,
            ]
        );

        $childAdmin = $admin->getCurrentChildAdmin();

        if ($childAdmin) {
            $id = $admin->getRequest()->get($admin->getIdParameter());

            $menu = $menu->addChild(
                $admin->toString($admin->getSubject()),
                [
                    'uri' => $admin->hasRoute($this->config['child_admin_route']) && $admin->hasAccess($this->config['child_admin_route'], $admin->getSubject()) ?
                    $admin->generateUrl($this->config['child_admin_route'], ['id' => $id]) :
                    null,
                    'extras' => [
                        'translation_domain' => false,
                    ],
                ]
            );

            $menu->setExtra('safe_label', false);

            return $this->buildBreadcrumbs($childAdmin, $action, $menu);
        }

        if ('list' === $action) {
            $menu->setUri(false);
        } elseif ('create' !== $action && $admin->hasSubject()) {
            $menu = $menu->addChild($admin->toString($admin->getSubject()), [
                'extras' => [
                    'translation_domain' => false,
                ],
            ]);
        } else {
            $menu = $this->createMenuItem(
                $admin,
                $menu,
                sprintf('%s_%s', $admin->getClassnameLabel(), $action),
                $admin->getTranslationDomain()
            );
        }

        return $menu;
    }

    /**
     * Creates a new menu item from a simple name. The name is normalized and
     * translated with the specified translation domain.
     *
     * @param AdminInterface $admin             used for translation
     * @param ItemInterface  $menu              will be modified and returned
     * @param string         $name              the source of the final label
     * @param string         $translationDomain for label translation
     * @param array          $options           menu item options
     *
     * @return ItemInterface
     */
    private function createMenuItem(
        AdminInterface $admin,
        ItemInterface $menu,
        $name,
        $translationDomain = null,
        $options = []
    ) {
        $options = array_merge([
            'extras' => [
                'translation_domain' => $translationDomain,
            ],
        ], $options);

        return $menu->addChild(
            $admin->getLabelTranslatorStrategy()->getLabel(
                $name,
                'breadcrumb',
                'link'
            ),
            $options
        );
    }
}


text:
export HOMEBREW_CACHE="/Library/Caches/Homebrew"
export HOMEBREW_LOGS="/Library/Logs/Homebrew/"