Skip to content

Commit cc5cd05

Browse files
committed
URl handler issue on Mac for all users nroduit#583
1 parent 802c1bc commit cc5cd05

File tree

8 files changed

+132
-138
lines changed

8 files changed

+132
-138
lines changed

.github/workflows/build-installer.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ jobs:
151151
"${signArgs[@]}" "${customOptions[@]}" "${commonOptions[@]}"
152152
153153
if [ "$machine" = "macosx" ] ; then
154-
codesign --timestamp --entitlements "${{ env.RES }}/uri-launcher.entitlements" --options runtime --force -vvv --keychain "$HOME/Library/Keychains/signing_temp.keychain-db" --sign "${{ secrets.MACOS__DEVELOPER_ID }}" "${{ env.RES }}/weasis-url-handler.app"
155-
cp -Rf "${{ env.RES }}/weasis-url-handler.app" "${{ env.WEASIS_OUTPUT }}/${{ env.NAME }}.app/Contents/MacOS/"
154+
cp -Rf "${{ env.RES }}/run.sh" "${{ env.WEASIS_OUTPUT }}/${{ env.NAME }}.app/Contents/MacOS/"
155+
chmod 755 "${{ env.WEASIS_OUTPUT }}/${{ env.NAME }}.app/Contents/MacOS/run.sh"
156156
codesign --timestamp --entitlements "${{ env.RES }}/uri-launcher.entitlements" --options runtime --force -vvv --keychain "$HOME/Library/Keychains/signing_temp.keychain-db" --sign "${{ secrets.MACOS__DEVELOPER_ID }}" "${{ env.WEASIS_OUTPUT }}/${{ env.NAME }}.app"
157157
fi
158158

weasis-distributions/script/launcher.sh

Lines changed: 0 additions & 55 deletions
This file was deleted.

weasis-distributions/script/package-weasis.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ $JPKGCMD --type app-image --input "$INPUT_DIR" --dest "$OUTPUT_PATH" --name "$NA
246246
--add-launcher "${DICOMIZER_CONFIG}" --resource-dir "$RES" --app-version "$WEASIS_CLEAN_VERSION" \
247247
"${tmpArgs[@]}" --verbose "${signArgs[@]}" "${customOptions[@]}" "${commonOptions[@]}"
248248

249+
if [ "$machine" = "macosx" ] ; then
250+
cp -Rf "$RES/run.sh" "$OUTPUT_PATH/$NAME.app/Contents/MacOS/"
251+
chmod 755 "$OUTPUT_PATH/$NAME.app/Contents/MacOS/run.sh"
252+
codesign --timestamp --entitlements "$RES/uri-launcher.entitlements" --options runtime --force -vvv --sign "$CERTIFICATE" "$RES/$NAME.app"
253+
fi
254+
249255
if [ "$PACKAGE" = "YES" ] ; then
250256
VENDOR="Weasis Team"
251257
COPYRIGHT="© 2009-2024 Weasis Team"

weasis-distributions/script/resources/macosx/Info.plist

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<plist version="1.0">
44
<dict>
55
<key>LSMinimumSystemVersion</key>
6-
<string>10.11</string>
6+
<string>10.13</string>
77
<key>CFBundleDevelopmentRegion</key>
88
<string>English</string>
99
<key>CFBundleAllowMixedLocalizations</key>
1010
<true/>
1111
<key>CFBundleExecutable</key>
12-
<string>DEPLOY_LAUNCHER_NAME</string>
12+
<string>run.sh</string>
1313
<key>CFBundleIconFile</key>
1414
<string>DEPLOY_ICON_FILE</string>
1515
<key>CFBundleIdentifier</key>
@@ -38,6 +38,17 @@
3838
<string>true</string>
3939
<key>NSMicrophoneUsageDescription</key>
4040
<string>The application DEPLOY_LAUNCHER_NAME is requesting access to the microphone.</string>
41+
<key>CFBundleURLTypes</key>
42+
<array>
43+
<dict>
44+
<key>CFBundleURLName</key>
45+
<string>org.weasis.url.handler</string>
46+
<key>CFBundleURLSchemes</key>
47+
<array>
48+
<string>weasis</string>
49+
</array>
50+
</dict>
51+
</array>
4152
<key>UTImportedTypeDeclarations</key>
4253
<array>
4354
<dict>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
# Get the path to the directory containing this script
3+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
4+
"$SCRIPT_DIR/Weasis" "$@"

weasis-launcher/src/main/java/org/weasis/launcher/Singleton.java

Lines changed: 97 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.List;
2929
import java.util.Objects;
3030
import java.util.Properties;
31+
import java.util.concurrent.ExecutorService;
32+
import java.util.concurrent.Executors;
3133
import java.util.concurrent.TimeUnit;
3234
import org.slf4j.Logger;
3335
import org.slf4j.LoggerFactory;
@@ -251,12 +253,7 @@ private static class SingletonServer {
251253
private final Thread thread;
252254

253255
SingletonServer(SingletonServerRunnable runnable) throws Exception {
254-
if ("1.8".equals(System.getProperty("java.specification.version"))) { // NON-NLS
255-
thread = new Thread(null, runnable, "SIThread", 0);
256-
} else {
257-
thread = new Thread(null, runnable, "SIThread", 0, false);
258-
}
259-
256+
thread = new Thread(null, runnable, "SIThread", 0, false);
260257
thread.setDaemon(true);
261258
this.runnable = runnable;
262259
}
@@ -348,83 +345,107 @@ private static String getStreamEncoding(InputStream is) throws IOException {
348345
}
349346

350347
private Void runSingletonServer() {
351-
while (true) {
352-
try (Socket s = ss.accept();
353-
InputStream is = s.getInputStream();
354-
InputStreamReader isr = new InputStreamReader(is, getStreamEncoding(is));
355-
BufferedReader in = new BufferedReader(isr)) {
356-
LOGGER.debug("Singleton server is waiting a connection");
357-
358-
// First read the random number
359-
String line = in.readLine();
360-
if (line.equals(String.valueOf(randomNumber))) {
361-
line = in.readLine();
362-
363-
LOGGER.debug("Recieve message: {}", line);
364-
if (SI_MAGICWORD.equals(line)) {
365-
LOGGER.debug("Got Magic work");
366-
List<String> recvArgs = new ArrayList<>();
367-
Properties props = new Properties();
368-
boolean arg = false;
369-
while (true) {
370-
try {
371-
line = in.readLine();
372-
if (SI_EOF.equals(line)) {
373-
break;
374-
} else if (SI_ARG.equals(line)) {
375-
arg = true;
376-
} else if (SI_PROP.equals(line)) {
377-
arg = false;
378-
} else if (Utils.hasText(line)) {
379-
if (arg) {
380-
recvArgs.add(line);
381-
} else {
382-
String[] vals = line.split("=", 2);
383-
if (vals.length == 2) {
384-
props.put(vals[0], vals[1]);
385-
}
386-
}
387-
}
388-
} catch (IOException ioe1) {
389-
LOGGER.error("Reading singleton lock file", ioe1);
390-
}
391-
}
392-
if (siApp.canStartNewActivation(props)) {
393-
siApp.newActivation(recvArgs);
394-
LOGGER.debug("Sending ACK");
395-
try (OutputStream os = s.getOutputStream();
396-
PrintStream ps = new PrintStream(os, true, isr.getEncoding())) {
397-
ps.println(SI_ACK);
398-
ps.flush();
399-
}
400-
} else {
401-
LOGGER.debug("Sending EXIT");
402-
try (OutputStream os = s.getOutputStream();
403-
PrintStream ps = new PrintStream(os, true, isr.getEncoding())) {
404-
ps.println(SI_EXIT);
405-
ps.flush();
406-
}
407-
System.exit(0);
408-
}
409-
} else if (SI_STOP.equals(line)) {
410-
removeSiFile();
348+
ExecutorService executor = Executors.newFixedThreadPool(1);
349+
350+
try (ServerSocket serverSocket = ss) {
351+
while (!Thread.currentThread().isInterrupted()) {
352+
try {
353+
Socket socket = serverSocket.accept();
354+
executor.submit(() -> handleClient(socket));
355+
} catch (IOException e) {
356+
if (serverSocket.isClosed()) {
411357
break;
412358
}
413-
} else {
414-
// random number does not match
415-
// should not happen
416-
// shutdown server socket
359+
LOGGER.error("Error accepting connection", e);
360+
}
361+
}
362+
} catch (IOException e) {
363+
LOGGER.error("Error starting Singleton server", e);
364+
} finally {
365+
executor.shutdown();
366+
}
367+
return null;
368+
}
369+
370+
private void handleClient(Socket socket) {
371+
try (InputStream is = socket.getInputStream();
372+
InputStreamReader isr = new InputStreamReader(is, getStreamEncoding(is));
373+
BufferedReader in = new BufferedReader(isr)) {
374+
375+
LOGGER.debug("Singleton server is waiting for a connection");
376+
377+
// First read the random number
378+
String line = in.readLine();
379+
if (line.equals(String.valueOf(randomNumber))) {
380+
line = in.readLine();
381+
LOGGER.debug("Receive message: {}", line);
382+
383+
if (SI_MAGICWORD.equals(line)) {
384+
handleMagicWord(in, socket, isr);
385+
} else if (SI_STOP.equals(line)) {
417386
removeSiFile();
418387
ss.close();
419-
serverStarted = false;
420-
LOGGER.error("Unexpected error: Singleton {} disabled", stringId);
421-
return null;
422388
}
423-
} catch (IOException ex) {
424-
LOGGER.error("Starting Singleton server", ex);
389+
} else {
390+
handleUnexpectedError();
425391
}
392+
} catch (IOException ex) {
393+
LOGGER.error("Error handling client", ex);
426394
}
427-
return null;
395+
}
396+
397+
private void handleMagicWord(BufferedReader in, Socket s, InputStreamReader isr)
398+
throws IOException {
399+
List<String> args = new ArrayList<>();
400+
Properties props = new Properties();
401+
boolean arg = false;
402+
403+
while (true) {
404+
String line = in.readLine();
405+
if (SI_EOF.equals(line)) {
406+
break;
407+
} else if (SI_ARG.equals(line)) {
408+
arg = true;
409+
} else if (SI_PROP.equals(line)) {
410+
arg = false;
411+
} else if (Utils.hasText(line)) {
412+
if (arg) {
413+
args.add(line);
414+
} else {
415+
String[] vals = line.split("=", 2);
416+
if (vals.length == 2) {
417+
props.put(vals[0], vals[1]);
418+
}
419+
}
420+
}
421+
}
422+
423+
if (siApp.canStartNewActivation(props)) {
424+
siApp.newActivation(args);
425+
LOGGER.debug("Sending ACK");
426+
sendResponse(s, isr, SI_ACK);
427+
} else {
428+
LOGGER.debug("Sending EXIT");
429+
sendResponse(s, isr, SI_EXIT);
430+
System.exit(0);
431+
}
432+
}
433+
434+
private void sendResponse(Socket s, InputStreamReader isr, String response) throws IOException {
435+
try (OutputStream os = s.getOutputStream();
436+
PrintStream ps = new PrintStream(os, true, isr.getEncoding())) {
437+
ps.println(response);
438+
ps.flush();
439+
}
440+
}
441+
442+
private void handleUnexpectedError() throws IOException {
443+
// random number does not match should not happen
444+
// shutdown server socket
445+
removeSiFile();
446+
ss.close();
447+
serverStarted = false;
448+
LOGGER.error("Unexpected error: Singleton {} disabled", stringId);
428449
}
429450
}
430451
}

weasis-launcher/src/main/java/org/weasis/launcher/WeasisLauncher.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,16 @@ public final void launch(Type type) throws Exception {
214214
if (app.isSupported(Action.APP_OPEN_URI)) {
215215
app.setOpenURIHandler(
216216
e -> {
217-
String uri = "dicom:get -r \"" + e.getURI().toString() + "\""; // NON-NLS
217+
String uri = e.getURI().toString();
218218
LOGGER.info("Get URI event from OS. URI: {}", uri);
219-
executeCommands(List.of(uri), null);
219+
int index = Utils.getWeasisProtocolIndex(uri);
220+
if (index < 0) {
221+
uri = "dicom:get -r \"" + uri + "\""; // NON-NLS
222+
executeCommands(List.of(uri), null);
223+
} else {
224+
configData.extractArgFromUri(uri);
225+
executeCommands(configData.getArguments(), null);
226+
}
220227
});
221228
}
222229
if (app.isSupported(Desktop.Action.APP_OPEN_FILE)) {

weasis-launcher/src/main/java/org/weasis/pref/ConfigData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private void filterConfigProperties(AppPreferences preferences) {
243243
}
244244
}
245245

246-
private void extractArgFromUri(String uri) {
246+
public void extractArgFromUri(String uri) {
247247
String url = URLDecoder.decode(uri, StandardCharsets.UTF_8);
248248
String[] cmds = url.split("\\$");
249249
boolean windows = System.getProperty(P_OS_NAME, "").toLowerCase().startsWith("win"); // NON-NLS

0 commit comments

Comments
 (0)