// Path: src/main/java/ru/vyarus/dropwizard/orient/internal/cmd/StudioVirtualFolder.java
// public class StudioVirtualFolder implements OCallable<Object, String> {
// 
//     public static final String STUDIO_PATH = "/www/";
//     public static final String STUDIO_INDEX = "index.html";
// 
//     @Override
//     public Object call(final String iArgument) {
//         final String fileName = STUDIO_PATH
//                 + MoreObjects.firstNonNull(Strings.emptyToNull(iArgument), STUDIO_INDEX);
//         final URL url = getClass().getResource(fileName);
//         if (url != null) {
//             final OServerCommandGetStaticContent.OStaticContent content =
//                     new OServerCommandGetStaticContent.OStaticContent();
//             content.is = new BufferedInputStream(getClass().getResourceAsStream(fileName));
//             content.contentSize = -1;
//             content.type = OServerCommandGetStaticContent.getContentType(url.getFile());
//             return content;
//         }
//         return null;
//     }
// }
// 
// Path: src/main/java/ru/vyarus/dropwizard/orient/internal/cmd/StudioVirtualFolder.java
// public static final String STUDIO_INDEX = "index.html";
// 
// Path: src/main/java/ru/vyarus/dropwizard/orient/internal/cmd/StudioVirtualFolder.java
// public static final String STUDIO_PATH = "/www/";


import com.orientechnologies.orient.server.network.protocol.http.command.get.OServerCommandGetStaticContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.vyarus.dropwizard.orient.internal.cmd.StudioVirtualFolder;
import java.io.IOException;
import java.net.URL;
import static ru.vyarus.dropwizard.orient.internal.cmd.StudioVirtualFolder.STUDIO_INDEX;
import static ru.vyarus.dropwizard.orient.internal.cmd.StudioVirtualFolder.STUDIO_PATH;
package ru.vyarus.dropwizard.orient.internal;




/**
 * Installs studio if studio jar is available on classpath (searches for 'www/index.html' in classpath,
 * assuming it's a studio).
 * Studio is available on url http://localhost:2480/studio/.
 *
 * @author Vyacheslav Rusakov
 * @since 21.08.2015
 */
public class OrientStudioInstaller {


    private final Logger logger = LoggerFactory.getLogger(OrientStudioInstaller.class);

    private final OServerCommandGetStaticContent command;

    public OrientStudioInstaller(final OServerCommandGetStaticContent command) {
        this.command = command;
    }

    /**
     * Searches for studio webjar and if found installs studio.
     *
     * @return true if studio installed, false otherwise
     * @throws Exception if installation fails
     */
    public boolean install() throws Exception {
        final boolean detected = detect();
        if (detected) {
            registerStudio();
        }
        return detected;
    }

    private void registerStudio() throws Exception {
        logger.debug("Registering studio application");
// Path: src/main/java/ru/vyarus/dropwizard/orient/internal/cmd/StudioVirtualFolder.java
// public class StudioVirtualFolder implements OCallable<Object, String> {
// 
//     public static final String STUDIO_PATH = "/www/";
//     public static final String STUDIO_INDEX = "index.html";
// 
//     @Override
//     public Object call(final String iArgument) {
//         final String fileName = STUDIO_PATH
//                 + MoreObjects.firstNonNull(Strings.emptyToNull(iArgument), STUDIO_INDEX);
//         final URL url = getClass().getResource(fileName);
//         if (url != null) {
//             final OServerCommandGetStaticContent.OStaticContent content =
//                     new OServerCommandGetStaticContent.OStaticContent();
//             content.is = new BufferedInputStream(getClass().getResourceAsStream(fileName));
//             content.contentSize = -1;
//             content.type = OServerCommandGetStaticContent.getContentType(url.getFile());
//             return content;
//         }
//         return null;
//     }
// }
// 
// Path: src/main/java/ru/vyarus/dropwizard/orient/internal/cmd/StudioVirtualFolder.java
// public static final String STUDIO_INDEX = "index.html";
// 
// Path: src/main/java/ru/vyarus/dropwizard/orient/internal/cmd/StudioVirtualFolder.java
// public static final String STUDIO_PATH = "/www/";


// Path: src/main/java/ru/vyarus/dropwizard/orient/internal/OrientStudioInstaller.java
import com.orientechnologies.orient.server.network.protocol.http.command.get.OServerCommandGetStaticContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.vyarus.dropwizard.orient.internal.cmd.StudioVirtualFolder;
import java.io.IOException;
import java.net.URL;
import static ru.vyarus.dropwizard.orient.internal.cmd.StudioVirtualFolder.STUDIO_INDEX;
import static ru.vyarus.dropwizard.orient.internal.cmd.StudioVirtualFolder.STUDIO_PATH;

package ru.vyarus.dropwizard.orient.internal;




/**
 * Installs studio if studio jar is available on classpath (searches for 'www/index.html' in classpath,
 * assuming it's a studio).
 * Studio is available on url http://localhost:2480/studio/.
 *
 * @author Vyacheslav Rusakov
 * @since 21.08.2015
 */
public class OrientStudioInstaller {


    private final Logger logger = LoggerFactory.getLogger(OrientStudioInstaller.class);

    private final OServerCommandGetStaticContent command;

    public OrientStudioInstaller(final OServerCommandGetStaticContent command) {
        this.command = command;
    }

    /**
     * Searches for studio webjar and if found installs studio.
     *
     * @return true if studio installed, false otherwise
     * @throws Exception if installation fails
     */
    public boolean install() throws Exception {
        final boolean detected = detect();
        if (detected) {
            registerStudio();
        }
        return detected;
    }

    private void registerStudio() throws Exception {
        logger.debug("Registering studio application");
        command.registerVirtualFolder("studio", new StudioVirtualFolder());