BrightSide Workbench Full Report + Source Code
SocialNet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.web;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import org.turro.string.Strings;
25 import org.turro.elephant.context.ElephantContext;
26 import org.turro.elephant.impl.repository.Repository;
27 import org.turro.elephant.impl.repository.RepositoryFile;
28 import org.turro.html.HtmlContent;
29 
34 public class SocialNet {
35 
36  private final String url, title, description;
37  private SocialImage image;
38 
39  public SocialNet(String url, String title, String description, Collection<RepositoryFile> files) {
40  this.url = url;
41  this.title = title;
42  this.description = escape(description);
44  files.addAll(repository.getRepositoryFiles("*_def_social.png,*_def_social.jpg"));
45  SocialImageMap.initialize(this, files);
46  }
47 
48  public boolean hasImage() {
49  return image != null && !image.isEmpty();
50  }
51 
52  public boolean hasLarge() {
53  return hasImage() && image.isLarge();
54  }
55 
56  public void setImage(SocialImage image) {
57  this.image = image;
58  }
59 
60  public String getUrl() {
61  return url;
62  }
63 
64  public List<String> getMetas() {
65  ArrayList<String> metas = new ArrayList<>();
66 
67  // Twitter card
68  if(!hasLarge()) {
69  metas.add("<meta name=\"twitter:card\" content=\"summary\">");
70  } else {
71  metas.add("<meta name=\"twitter:card\" content=\"summary_large_image\">"); // Twitter card
72  }
73  metas.add("<meta name=\"twitter:title\" content=\"" + escape(title) + "\"/>");
74  if(!Strings.isBlank(description)) {
75  metas.add("<meta name=\"twitter:description\" content=\"" + Strings.truncateAndWarn(description, 250) + "\"/>");
76  }
77  if(hasImage()) {
78  metas.add("<meta name=\"twitter:image\" content=\"" + ElephantContext.getServerBase("http") + image.getUrl() + "\"/>");
79  }
80 
81  // OpenGraph
82  metas.add("<meta property=\"og:site_name\" content=\"" + ElephantContext.getSiteName() + "\"/>");
83  metas.add("<meta property=\"og:type\" content=\"website\"/>");
84  metas.add("<meta property=\"og:url\" content=\"" + ElephantContext.getServerBase("http") + url + "\"/>");
85  metas.add("<meta property=\"og:title\" content=\"" + escape(title) + "\"/>");
86  if(!Strings.isBlank(description)) {
87  metas.add("<meta property=\"og:description\" content=\"" + Strings.truncateAndWarn(description, 250) + "\"/>");
88  }
89  if(hasImage()) {
90  metas.add("<meta property=\"og:image\" content=\"" + ElephantContext.getServerBase("http") + image.getUrl() + "\"/>");
91  }
92 
93  return metas;
94  }
95 
96  private String escape(String text) {
97  if(!Strings.isBlank(text)) {
98  text = HtmlContent.text(text);
99  text = text.replaceAll("\"", "'");
100  text = text.replaceAll("\n", " ");
101  }
102  return text;
103  }
104 
105 }
static String getServerBase(String scheme)
static void initialize(SocialNet context, Collection< RepositoryFile > files)
SocialNet(String url, String title, String description, Collection< RepositoryFile > files)
Definition: SocialNet.java:39
void setImage(SocialImage image)
Definition: SocialNet.java:56