Skip to content
Open
Show file tree
Hide file tree
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 @@ -122,9 +122,6 @@ public class GridRestProcessor extends GridProcessorAdapter implements IgniteRes
/** The default interval used to invalidate sessions, in seconds. */
public static final int DFLT_SES_TOKEN_INVALIDATE_INTERVAL = 5 * 60;

/** Index of task name wrapped by VisorGatewayTask */
private static final int WRAPPED_TASK_IDX = 1;

/** Protocols. */
private final Collection<GridRestProtocol> protos = new ArrayList<>();

Expand Down Expand Up @@ -1104,16 +1101,6 @@ static Session fromClientId(UUID clientId) {
return new Session(clientId, UUID.randomUUID());
}

/**
* Static constructor.
*
* @param sesTokId Session token ID.
* @return New session instance with random client ID and given session ID.
*/
static Session fromSessionToken(UUID sesTokId) {
return new Session(UUID.randomUUID(), sesTokId);
}

/**
* Checks expiration of session and if expired then sets TIMEDOUT_FLAG.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@

package org.apache.ignite.internal.processors.rest.protocols.http.jetty;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.sql.Date;
import java.sql.Time;
Expand Down Expand Up @@ -100,8 +94,8 @@
* Jetty REST handler. The following URL format is supported: {@code /ignite?cmd=cmdName&param1=abc&param2=123}
*/
public class GridJettyRestHandler extends AbstractHandler {
/** Used to sent request charset. */
private static final String CHARSET = StandardCharsets.UTF_8.name();
/** */
private static final String IGNITE_CMD_PATH = "/ignite";

/** */
private static final String FAILED_TO_PARSE_FORMAT = "Failed to parse parameter of %s type [%s=%s]";
Expand Down Expand Up @@ -148,12 +142,6 @@
/** Request handlers. */
private GridRestProtocolHandler hnd;

/** Default page. */
private volatile String dfltPage;

/** Favicon. */
private volatile byte[] favicon;

/** Mapper from Java object to JSON. */
private final ObjectMapper jsonMapper;

Expand All @@ -175,27 +163,6 @@
this.authChecker = authChecker;
this.log = ctx.log(getClass());
this.jsonMapper = new IgniteObjectMapper(ctx);

// Init default page and favicon.
try {
initDefaultPage();

if (log.isDebugEnabled())
log.debug("Initialized default page.");
}
catch (IOException e) {
U.warn(log, "Failed to initialize default page: " + e.getMessage());
}

try {
initFavicon();

if (log.isDebugEnabled())
log.debug(favicon != null ? "Initialized favicon, size: " + favicon.length : "Favicon is null.");
}
catch (IOException e) {
U.warn(log, "Failed to initialize favicon: " + e.getMessage());
}
}

/**
Expand Down Expand Up @@ -302,115 +269,17 @@
}
}

/**
* @throws IOException If failed.
*/
private void initDefaultPage() throws IOException {
assert dfltPage == null;

InputStream in = getClass().getResourceAsStream("rest.html");

if (in != null) {
LineNumberReader rdr = new LineNumberReader(new InputStreamReader(in, CHARSET));

try {
StringBuilder buf = new StringBuilder(2048);

for (String line = rdr.readLine(); line != null; line = rdr.readLine()) {
buf.append(line);

if (!line.endsWith(" "))
buf.append(' ');
}

dfltPage = buf.toString();
}
finally {
U.closeQuiet(rdr);
}
}
}

/**
* @throws IOException If failed.
*/
private void initFavicon() throws IOException {
assert favicon == null;

InputStream in = getClass().getResourceAsStream("favicon.ico");

if (in != null) {
BufferedInputStream bis = new BufferedInputStream(in);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

try {
byte[] buf = new byte[2048];

while (true) {
int n = bis.read(buf);

if (n == -1)
break;

bos.write(buf, 0, n);
}

favicon = bos.toByteArray();
}
finally {
U.closeQuiet(bis);
}
}
}

/** {@inheritDoc} */
@Override public void handle(String target, Request req, HttpServletRequest srvReq, HttpServletResponse res)
throws IOException {
@Override public void handle(String target, Request req, HttpServletRequest srvReq, HttpServletResponse res) {
if (log.isDebugEnabled())
log.debug("Handling request [target=" + target + ", req=" + req + ", srvReq=" + srvReq + ']');

if (target.startsWith("/ignite")) {
processRequest(target, srvReq, res);

req.setHandled(true);
}
else if (target.startsWith("/favicon.ico")) {
if (favicon == null) {
res.setStatus(HttpServletResponse.SC_NOT_FOUND);

req.setHandled(true);

return;
}

res.setStatus(HttpServletResponse.SC_OK);

res.setContentType("image/x-icon");

res.getOutputStream().write(favicon);
res.getOutputStream().flush();

req.setHandled(true);
}
else {
if (dfltPage == null) {
res.setStatus(HttpServletResponse.SC_NOT_FOUND);

req.setHandled(true);

return;
}

res.setStatus(HttpServletResponse.SC_OK);

res.setContentType("text/html");
if (!target.startsWith(IGNITE_CMD_PATH))
return;

res.getWriter().write(dfltPage);
res.getWriter().flush();
processRequest(target, srvReq, res);

req.setHandled(true);
}
req.setHandled(true);
}

/**
Expand Down Expand Up @@ -514,7 +383,7 @@
* @return REST request.
* @throws IgniteCheckedException If creation failed.
*/
@Nullable private GridRestRequest createRequest(
private GridRestRequest createRequest(

Check failure on line 386 in modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 72 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_ignite&issues=AZ4lnqZMek4mpMlU3e-k&open=AZ4lnqZMek4mpMlU3e-k&pullRequest=13132
GridRestCommand cmd,
Map<String, String> params,
HttpServletRequest req
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
Expand All @@ -59,9 +61,6 @@
* Jetty REST protocol implementation.
*/
public class GridJettyRestProtocol extends GridRestProtocolAdapter {
/**
*
*/
static {
if (!IgniteSystemProperties.getBoolean(IGNITE_JETTY_LOG_NO_OVERRIDE)) {
// See also https://www.eclipse.org/jetty/documentation/jetty-9/index.html#configuring-jetty-logging
Expand Down Expand Up @@ -155,9 +154,15 @@ else if (log.isDebugEnabled())
connector.setPort(port);

if (startJetty()) {
if (log.isInfoEnabled())
if (log.isInfoEnabled()) {
log.info(startInfo());

boolean isSsl = connector.getConnectionFactory(SslConnectionFactory.class) != null;
String proto = isSsl ? "https" : "http";

log.info("HTTP REST protocol address: " + proto + "://" + host + ":" + port + "/");
}

return;
}
}
Expand Down Expand Up @@ -312,7 +317,9 @@ private void loadJettyConfiguration(@Nullable URL cfgUrl) throws IgniteCheckedEx

assert httpSrv != null;

httpSrv.setHandler(jettyHnd);
WelcomeHandler welcomeHnd = new WelcomeHandler(log);

httpSrv.setHandler(new HandlerList(jettyHnd, welcomeHnd));

override(getJettyConnector());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.ignite.internal.processors.rest.protocols.http.jetty;

import java.io.IOException;
import java.io.InputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ignite.IgniteLogger;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

/**
* Handles welcome page.
*/
public class WelcomeHandler extends AbstractHandler {
/** Default page. */
private final byte[] dfltPage;

/** Favicon. */
private final byte[] favicon;

/** Logo. */
private final byte[] logo;

/** */
private final IgniteLogger log;

/** */
public WelcomeHandler(IgniteLogger log) {
this.log = log;

favicon = loadResource("ignite-rest-http/favicon.ico");
dfltPage = loadResource("ignite-rest-http/rest.html");
logo = loadResource("ignite-rest-http/logo.svg");
}

/** {@inheritDoc} */
@Override public void handle(String target, Request req, HttpServletRequest srvReq, HttpServletResponse res) throws IOException {
if (dfltPage == null || favicon == null || logo == null) {
res.setStatus(HttpServletResponse.SC_NOT_FOUND);
req.setHandled(true);

return;
}

if (target.startsWith("/favicon.ico")) {
res.setContentType("image/x-icon");
res.getOutputStream().write(favicon);
}
else if (target.startsWith("/logo.svg")) {
res.setContentType("image/svg+xml");
res.getOutputStream().write(logo);
}
else {
res.setContentType("text/html; charset=utf-8");
res.getOutputStream().write(dfltPage);
}

res.getOutputStream().flush();

res.setStatus(HttpServletResponse.SC_OK);
req.setHandled(true);
}

/** */
private byte[] loadResource(String path) {
try (InputStream in = getClass().getClassLoader().getResourceAsStream(path)) {
return in != null ? in.readAllBytes() : null;
}
catch (IOException e) {
log.error("Failed to load REST resource [path=" + path + ']', e);

return null;

Check warning on line 89 in modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/WelcomeHandler.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Return an empty array instead of null.

See more on https://sonarcloud.io/project/issues?id=apache_ignite&issues=AZ4ljC52Kr1Tka8MNI-I&open=AZ4ljC52Kr1Tka8MNI-I&pullRequest=13132
}
}
}
Loading
Loading