
org.glowroot.ui.TraceExportHttpService Maven / Gradle / Ivy
/*
* Copyright 2011-2015 the original author or authors.
*
* 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.glowroot.ui;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.glowroot.agent.shaded.google.common.base.Charsets;
import org.glowroot.agent.shaded.google.common.collect.ImmutableList;
import org.glowroot.agent.shaded.google.common.collect.Lists;
import org.glowroot.agent.shaded.google.common.io.CharSource;
import org.glowroot.agent.shaded.google.common.io.Resources;
import org.glowroot.agent.shaded.google.common.net.MediaType;
import org.glowroot.agent.shaded.netty.channel.ChannelFuture;
import org.glowroot.agent.shaded.netty.channel.ChannelHandlerContext;
import org.glowroot.agent.shaded.netty.handler.codec.http.DefaultFullHttpResponse;
import org.glowroot.agent.shaded.netty.handler.codec.http.DefaultHttpResponse;
import org.glowroot.agent.shaded.netty.handler.codec.http.FullHttpResponse;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpContent;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpHeaderNames;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpHeaderValues;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpRequest;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpResponse;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpUtil;
import org.glowroot.agent.shaded.netty.handler.codec.http.QueryStringDecoder;
import org.glowroot.agent.shaded.netty.handler.stream.ChunkedInput;
import org.glowroot.agent.shaded.slf4j.Logger;
import org.glowroot.agent.shaded.slf4j.LoggerFactory;
import org.glowroot.ui.TraceCommonService.TraceExport;
import static org.glowroot.agent.shaded.google.common.base.Preconditions.checkNotNull;
import static org.glowroot.agent.shaded.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static org.glowroot.agent.shaded.netty.handler.codec.http.HttpResponseStatus.OK;
import static org.glowroot.agent.shaded.netty.handler.codec.http.HttpVersion.HTTP_1_1;
class TraceExportHttpService implements HttpService {
private static final Logger logger = LoggerFactory.getLogger(TraceExportHttpService.class);
private final TraceCommonService traceCommonService;
private final String version;
TraceExportHttpService(TraceCommonService traceCommonService, String version) {
this.traceCommonService = traceCommonService;
this.version = version;
}
@Override
public @Nullable FullHttpResponse handleRequest(ChannelHandlerContext ctx, HttpRequest request)
throws Exception {
QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
List serverIds = decoder.parameters().get("server-id");
if (serverIds == null) {
serverIds = ImmutableList.of("");
}
String serverId = serverIds.get(0);
List traceIds = decoder.parameters().get("trace-id");
checkNotNull(traceIds, "Missing trace id in query string: %s", request.uri());
String traceId = traceIds.get(0);
logger.debug("handleRequest(): serverId={}, traceId={}", serverId, traceId);
TraceExport traceExport = traceCommonService.getExport(serverId, traceId);
if (traceExport == null) {
logger.warn("no trace found for id: {}", traceId);
return new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
}
ChunkedInput in = getExportChunkedInput(traceExport);
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, MediaType.ZIP.toString());
response.headers().set("Content-Disposition",
"attachment; filename=" + traceExport.fileName() + ".zip");
boolean keepAlive = HttpUtil.isKeepAlive(request);
if (keepAlive && !request.protocolVersion().isKeepAliveDefault()) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
HttpServices.preventCaching(response);
ctx.write(response);
ChannelFuture future = ctx.write(in);
HttpServices.addErrorListener(future);
if (!keepAlive) {
HttpServices.addCloseListener(future);
}
// return null to indicate streaming
return null;
}
private ChunkedInput getExportChunkedInput(TraceExport traceExport)
throws IOException {
ChunkSource chunkSource = render(traceExport);
return ChunkedInputs.fromChunkSourceToZipFileDownload(chunkSource, traceExport.fileName());
}
private ChunkSource render(TraceExport traceExport) throws IOException {
String htmlStartTag = "";
String exportCssPlaceholder = "";
String exportJsPlaceholder = "";
String headerPlaceholder = "";
String entriesPlaceholder = "";
String profilePlaceholder = "";
String footerMessagePlaceholder = "";
String templateContent = asCharSource("trace-export.html").read();
Pattern pattern = Pattern.compile("(" + htmlStartTag + "|" + exportCssPlaceholder + "|"
+ exportJsPlaceholder + "|" + headerPlaceholder + "|" + entriesPlaceholder + "|"
+ profilePlaceholder + "|" + footerMessagePlaceholder + ")");
Matcher matcher = pattern.matcher(templateContent);
int curr = 0;
List chunkSources = Lists.newArrayList();
while (matcher.find()) {
chunkSources.add(ChunkSource.wrap(templateContent.substring(curr, matcher.start())));
curr = matcher.end();
String match = matcher.group();
if (match.equals(htmlStartTag)) {
// Need to add "Mark of the Web" for IE, otherwise IE won't run javascript
// see http://msdn.microsoft.com/en-us/library/ms537628(v=vs.85).aspx
chunkSources.add(
ChunkSource.wrap("\r\n"));
} else if (match.equals(exportCssPlaceholder)) {
chunkSources.add(ChunkSource.wrap(""));
} else if (match.equals(exportJsPlaceholder)) {
chunkSources.add(ChunkSource.wrap(""));
} else if (match.equals(headerPlaceholder)) {
chunkSources.add(ChunkSource.wrap(""));
} else if (match.equals(entriesPlaceholder)) {
chunkSources
.add(ChunkSource.wrap(""));
} else if (match.equals(profilePlaceholder)) {
chunkSources
.add(ChunkSource.wrap(""));
} else if (match.equals(footerMessagePlaceholder)) {
chunkSources.add(ChunkSource.wrap("Glowroot version " + version));
} else {
logger.error("unexpected match: {}", match);
}
}
chunkSources.add(ChunkSource.wrap(templateContent.substring(curr)));
return ChunkSource.concat(chunkSources);
}
private static ChunkSource asChunkSource(String exportResourceName) {
return ChunkSource.from(asCharSource(exportResourceName));
}
private static CharSource asCharSource(String exportResourceName) {
URL url = Resources.getResource("org/glowroot/ui/export-dist/" + exportResourceName);
return Resources.asCharSource(url, Charsets.UTF_8);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy