Skip to content

Commit

Permalink
#708 wip, initial stubs/code
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Aug 5, 2019
1 parent a7d9d3b commit 675240d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/main/java/alfio/controller/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import alfio.manager.system.ConfigurationLevel;
import alfio.manager.system.ConfigurationManager;
import alfio.manager.user.UserManager;
import alfio.repository.EventRepository;
import alfio.util.RequestUtils;
import alfio.util.TemplateManager;
import ch.digitalfondue.jfiveparse.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
Expand All @@ -37,6 +40,9 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.EnumSet;
import java.util.Map;
Expand All @@ -53,6 +59,7 @@ public class IndexController {
private static final String UTF_8 = "UTF-8";

private final ConfigurationManager configurationManager;
private final EventRepository eventRepository;
private final Environment environment;
private final UserManager userManager;
private final TemplateManager templateManager;
Expand Down Expand Up @@ -98,14 +105,37 @@ public ResponseEntity<String> replyToK8s() {
"/event/{eventShortName}/reservation/{reservationId}/success",
"/event/{eventShortName}/ticket/{ticketId}/view"
})
public void replyToIndex(HttpServletResponse response) throws IOException {
public void replyToIndex(@PathVariable(value = "eventShortName", required = false) String eventShortName,
@RequestHeader(value = "User-Agent", required = false) String userAgent,
HttpServletResponse response) throws IOException {

response.setContentType(TEXT_HTML_CHARSET_UTF_8);
response.setCharacterEncoding(UTF_8);
try (var is = new ClassPathResource("alfio-public-frontend-index.html").getInputStream(); var os = response.getOutputStream()) {
is.transferTo(os);

if (eventShortName != null && RequestUtils.isSocialMediaShareUA(userAgent) && eventRepository.existsByShortName(eventShortName)) {
try (var is = new ClassPathResource("alfio/web-templates/event-open-graph-page.html").getInputStream(); var os = response.getOutputStream()) {
var res = getOpenGraphPage(is, eventShortName);
os.write(res);
}
} else {
try (var is = new ClassPathResource("alfio-public-frontend-index.html").getInputStream(); var os = response.getOutputStream()) {
is.transferTo(os);
}
}
}

private byte[] getOpenGraphPage(InputStream is, String eventShortName) {
var event = eventRepository.findByShortName(eventShortName);
var eventOpenGraph = new Parser().parse(new InputStreamReader(is, StandardCharsets.UTF_8));
eventOpenGraph.getElementsByTagName("title").get(0).appendChild(new Text(event.getDisplayName()));
getMetaElement(eventOpenGraph, "og:image").setAttribute("content", "file/" + event.getFileBlobId());
return eventOpenGraph.getOuterHTML().getBytes(StandardCharsets.UTF_8);
}

private static Element getMetaElement(Document document, String propertyValue) {
return (Element) document.getAllNodesMatching(Selector.select().element("meta").attrValEq("property", "og:image").toMatcher(), true).get(0);
}

@GetMapping("/event/{eventShortName}/code/{code}")
public String redirectCode(@PathVariable("eventShortName") String eventName,
@PathVariable("code") String code) {
Expand All @@ -114,7 +144,6 @@ public String redirectCode(@PathVariable("eventShortName") String eventName,
.toString();
}


// login related
@GetMapping("/authentication")
public void getLoginPage(@RequestParam(value="failed", required = false) String failed, @RequestParam(value = "recaptchaFailed", required = false) String recaptchaFailed,
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/alfio/util/RequestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.regex.Pattern;

@Log4j2
@UtilityClass
Expand All @@ -36,4 +37,10 @@ public static Optional<String> readRequest(HttpServletRequest request) {
return Optional.empty();
}
}

private static final Pattern SOCIAL_MEDIA_UA = Pattern.compile("facebookexternalhit/|XING-contenttabreceiver/|LinkedInBot/|Twitterbot/");

public static boolean isSocialMediaShareUA(String ua) {
return ua != null && SOCIAL_MEDIA_UA.matcher(ua).find();
}
}
10 changes: 10 additions & 0 deletions src/main/resources/alfio/web-templates/event-open-graph-page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="" prefix="og: http://ogp.me/ns#">
<head>
<meta charset="UTF-8">
<title></title>
<meta property="og:type" content="website" />
<meta property="og:image" content="" />
<meta property="og:description" content="" />
</head>
</html>
32 changes: 32 additions & 0 deletions src/test/java/alfio/util/RequestUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class RequestUtilsTest {

@Test
public void testUA() {
Assertions.assertTrue(RequestUtils.isSocialMediaShareUA("facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"));
Assertions.assertTrue(RequestUtils.isSocialMediaShareUA("XING-contenttabreceiver/2.0"));
Assertions.assertTrue(RequestUtils.isSocialMediaShareUA("LinkedInBot/1.0 (compatible; Mozilla/5.0; Jakarta Commons-HttpClient/3.1 +http://www.linkedin.com)"));
Assertions.assertTrue(RequestUtils.isSocialMediaShareUA("Twitterbot/1.0"));
Assertions.assertFalse(RequestUtils.isSocialMediaShareUA("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"));
}
}

0 comments on commit 675240d

Please sign in to comment.