zyratlo commented on code in PR #5258:
URL: https://github.com/apache/texera/pull/5258#discussion_r3461778250


##########
notebook-migration-service/src/main/scala/org/apache/texera/service/resource/NotebookMigrationResource.scala:
##########
@@ -0,0 +1,422 @@
+// 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.texera.service.resource
+
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.module.scala.DefaultScalaModule
+import com.typesafe.scalalogging.LazyLogging
+import io.dropwizard.auth.Auth
+import jakarta.annotation.security.RolesAllowed
+import jakarta.ws.rs._
+import jakarta.ws.rs.core._
+import org.apache.texera.auth.SessionUser
+import org.apache.texera.dao.SqlServer
+import org.jooq.JSONB
+import org.apache.texera.dao.jooq.generated.tables.Notebook
+import org.apache.texera.dao.jooq.generated.tables.WorkflowNotebookMapping
+import java.net.{HttpURLConnection, URL}
+import java.nio.charset.StandardCharsets
+import scala.util.control.NonFatal
+import org.apache.texera.common.config.StorageConfig
+
+object NotebookMigrationResource extends LazyLogging {
+
+  private val mapper: ObjectMapper = new 
ObjectMapper().registerModule(DefaultScalaModule)
+
+  // Build an error response body via the mapper so the message is 
JSON-escaped; interpolating
+  // e.getMessage directly produces malformed JSON when it contains quotes, 
backslashes, or newlines.
+  private def errorJson(message: String): String =
+    mapper.writeValueAsString(mapper.createObjectNode().put("error", message))
+
+  private val jupyterUrl = StorageConfig.jupyterURL
+  private val jupyterToken = StorageConfig.jupyterToken
+  // The token is passed as a URL param so the browser iframe can authenticate 
when loading the notebook.
+  // jupyterIframeURL is process-global state. This is safe ONLY because each 
user runs their own pod
+  // (own notebook-migration-service JVM + own Jupyter) in the k8s deployment, 
so this singleton is
+  // effectively per-user. Do NOT deploy this service as a shared multi-user 
instance without adding
+  // per-user keying here, or one user's upload would overwrite another's 
iframe URL.
+  private var jupyterIframeURL = 
s"$jupyterUrl/notebooks/work/notebook.ipynb?token=$jupyterToken"
+
+  private def isJupyterAvailable(jupyterUrl: String): Boolean = {
+    var conn: java.net.HttpURLConnection = null
+    try {
+      conn = new java.net.URL(s"$jupyterUrl/api")
+        .openConnection()
+        .asInstanceOf[java.net.HttpURLConnection]
+
+      conn.setRequestMethod("GET")
+      conn.setConnectTimeout(2000)
+      conn.setReadTimeout(2000)
+
+      val status = conn.getResponseCode
+
+      status == 200 || status == 403
+    } catch {
+      case _: Exception => false
+    } finally {
+      if (conn != null) conn.disconnect()
+    }
+  }
+
+  // Returns the Jupyter iframe reference URL
+  def getJupyterIframeURL(): Response = {
+    if (!isJupyterAvailable(jupyterUrl)) {
+      return Response
+        .status(500)
+        .entity(
+          """
+      {
+        "success": false,
+        "message": "Cannot connect to Jupyter server"
+      }
+      """
+        )
+        .build()
+    }
+
+    Response
+      .ok(
+        s"""
+    {
+      "success": true,
+      "url": "$jupyterIframeURL"
+    }
+    """
+      )
+      .build()
+  }

Review Comment:
   This value is already regex verified to be valid so there wouldn't be a case 
of invalid JSON. However, to future-proof for possible refactoring of this 
function it is OK to make this change as it doesn't affect anything now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to