Skip to content

Commit 4339654

Browse files
committed
NON-ISSUE Make Java11 compatible. (Remove javax dependency)
This closes #382.
1 parent 7900e4f commit 4339654

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

line-bot-cli/src/main/java/com/linecorp/bot/cli/RichMenuImageUploadCommand.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.util.concurrent.Futures.getUnchecked;
21-
import static javax.activation.FileTypeMap.getDefaultFileTypeMap;
2221

2322
import java.io.IOException;
23+
import java.net.FileNameMap;
24+
import java.net.URLConnection;
2425
import java.nio.file.Files;
2526
import java.nio.file.Paths;
2627

2728
import org.springframework.beans.factory.annotation.Autowired;
2829
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2930
import org.springframework.stereotype.Component;
3031

32+
import com.google.common.annotations.VisibleForTesting;
33+
3134
import com.linecorp.bot.cli.arguments.Arguments;
3235
import com.linecorp.bot.client.LineMessagingClient;
3336
import com.linecorp.bot.model.response.BotApiResponse;
@@ -40,14 +43,16 @@
4043
@ConditionalOnProperty(name = "command", havingValue = "richmenu-upload")
4144
@AllArgsConstructor(onConstructor = @__(@Autowired))
4245
public class RichMenuImageUploadCommand implements CliCommand {
46+
private static final FileNameMap FILE_NAME_MAP = URLConnection.getFileNameMap();
47+
4348
private LineMessagingClient lineMessagingClient;
4449
private Arguments arguments;
4550

4651
@Override
4752
public void execute() throws IOException {
4853
final String richMenuId = checkNotNull(arguments.getRichMenuId(), "--rich-menu-id= is not set.");
4954
final String image = checkNotNull(arguments.getImage(), "--image= is not set.");
50-
final String contentType = checkNotNull(getDefaultFileTypeMap().getContentType(image),
55+
final String contentType = checkNotNull(resolveContentTypeForFileName(image),
5156
"Can't assume Content-Type");
5257
log.info("Content-Type: {}", contentType);
5358

@@ -57,4 +62,9 @@ public void execute() throws IOException {
5762

5863
log.info("Request Successfully finished. {}", botApiResponse);
5964
}
65+
66+
@VisibleForTesting
67+
static String resolveContentTypeForFileName(final String fileName) {
68+
return FILE_NAME_MAP.getContentTypeFor(fileName);
69+
}
6070
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2019 LINE Corporation
3+
*
4+
* LINE Corporation licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.linecorp.bot.cli;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.util.function.Function;
22+
23+
import org.junit.Test;
24+
25+
public class RichMenuImageUploadCommandTest {
26+
27+
@Test
28+
public void resolveContentTypeForFileNameTest() {
29+
final Function<String, String> target =
30+
RichMenuImageUploadCommand::resolveContentTypeForFileName;
31+
32+
// Verify
33+
assertThat(target.apply("image.PNG")).isEqualTo("image/png");
34+
assertThat(target.apply("image.png")).isEqualTo("image/png");
35+
assertThat(target.apply("image.jpg")).isEqualTo("image/jpeg");
36+
assertThat(target.apply("image.jpeg")).isEqualTo("image/jpeg");
37+
}
38+
}

0 commit comments

Comments
 (0)