Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

package com.baidu.hugegraph.api.job;

import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -32,6 +37,7 @@

import org.slf4j.Logger;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.api.filter.StatusFilter.Status;
Expand All @@ -58,6 +64,8 @@ public class GremlinAPI extends API {

private static final Logger LOG = Log.logger(RestServer.class);

private static final int MAX_NAME_LENGTH = 256;

private static final Histogram gremlinJobInputHistogram =
MetricsUtil.registerHistogram(GremlinAPI.class, "gremlin-input");

Expand Down Expand Up @@ -148,7 +156,27 @@ public Map<String, String> aliases() {

public String name() {
// Get the first line of script as the name
return this.gremlin.split("\r\n|\r|\n", 2)[0];
String firstLine = this.gremlin.split("\r\n|\r|\n", 2)[0];
final Charset charset = Charset.forName(CHARSET);
final byte[] bytes = firstLine.getBytes(charset);
if (bytes.length <= MAX_NAME_LENGTH) {
return firstLine;
}

/*
* Reference https://stackoverflow.com/questions/3576754/truncating-strings-by-bytes
*/
CharsetDecoder decoder = charset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.reset();

ByteBuffer buffer = ByteBuffer.wrap(bytes, 0, MAX_NAME_LENGTH);
try {
return decoder.decode(buffer).toString();
} catch (CharacterCodingException e) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just catch line 175

throw new HugeException("Failed to decode truncated bytes of " +
"gremlin first line", e);
}
}

@Override
Expand Down