/*
Copyright 2011 Selenium committers

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package org.openqa.selenium.remote.server.renderer;

import com.google.common.io.ByteStreams;

import org.openqa.selenium.remote.server.HttpRequest;
import org.openqa.selenium.remote.server.HttpResponse;
import org.openqa.selenium.remote.server.rest.Renderer;
import org.openqa.selenium.remote.server.rest.RestishHandler;

import java.net.URL;

import static org.openqa.selenium.remote.server.HttpStatusCodes.NOT_FOUND;
import static org.openqa.selenium.remote.server.HttpStatusCodes.OK;

public class ResourceCopyResult implements Renderer {

  private final String propertyName;

  public ResourceCopyResult(String propertyName) {
    if (propertyName.startsWith(":")) {
      this.propertyName = propertyName.substring(1);
    } else {
      this.propertyName = propertyName;
    }
  }

  public void render(HttpRequest request, HttpResponse response, RestishHandler handler) throws Exception {
    URL resource = (URL) request.getAttribute(propertyName);
    if (resource == null) {
      response.setStatus(NOT_FOUND);
      return;
    }

    response.setStatus(OK);
    response.setContent(ByteStreams.toByteArray(resource.openStream()));
  }
}

@@ -626,40 +626,142 @@
 ort 
-com.google.common.io.ByteStreams
+static org.openqa.selenium.remote.server.HttpStatusCodes.NOT_FOUND;%0Aimport static org.openqa.selenium.remote.server.HttpStatusCodes.OK
 ;%0A%0Ai
@@ -998,220 +998,464 @@
 ort 
-java.net.URL;%0A%0Aimport static org.openqa.selenium.remote.server.HttpStatusCodes.NOT_FOUND;%0Aimport static org.openqa.selenium.remote.server.HttpStatusCodes.OK;%0A%0Apublic class ResourceCopyResult implements Renderer %7B
+com.google.common.collect.ImmutableMap;%0Aimport com.google.common.io.ByteStreams;%0Aimport com.google.common.net.MediaType;%0A%0Aimport java.net.URL;%0A%0Apublic class ResourceCopyResult implements Renderer %7B%0A  %0A  private static final ImmutableMap%3CString, MediaType%3E MIME_TYPES = ImmutableMap.of(%0A      %22css%22, MediaType.CSS_UTF_8.withoutParameters(),%0A      %22html%22, MediaType.HTML_UTF_8.withoutParameters(),%0A      %22js%22, MediaType.JAVASCRIPT_UTF_8.withoutParameters());
 %0A%0A  
@@ -1951,16 +1951,325 @@
 n;%0A    %7D
+%0A    %0A    String fileName = resource.getFile();%0A    int dotIndex = fileName.lastIndexOf('.');%0A    if (dotIndex %3E= 0) %7B%0A      String extension = fileName.substring(dotIndex + 1);%0A      if (MIME_TYPES.containsKey(extension)) %7B%0A        response.setContentType(MIME_TYPES.get(extension).toString());%0A      %7D%0A    %7D
 %0A%0A    re

Set the Content-Type header so the server UI renders correctly in every browser.