Changeset 2829 for vlo/trunk/vlo_web_app/src/main/java/eu/clarin/cmdi/vlo/pages/ResourceLinkPanel.java
- Timestamp:
- 04/23/13 14:06:35 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vlo/trunk/vlo_web_app/src/main/java/eu/clarin/cmdi/vlo/pages/ResourceLinkPanel.java
r2768 r2829 1 1 2 package eu.clarin.cmdi.vlo.pages; 2 3 3 4 import eu.clarin.cmdi.vlo.CommonUtils; 4 5 import eu.clarin.cmdi.vlo.FacetConstants; 5 import eu.clarin.cmdi.vlo.config.VloConfig;6 import java.io.IOException;7 import java.net.HttpURLConnection;8 import java.net.MalformedURLException;9 import java.net.URL;10 import java.net.URLConnection;11 6 import java.util.HashMap; 12 7 import java.util.Map; … … 15 10 import org.apache.wicket.markup.html.image.Image; 16 11 import org.apache.wicket.markup.html.link.ExternalLink; 17 import org.apache.wicket.markup.html.panel.Panel;18 12 import org.apache.wicket.resource.ContextRelativeResource; 19 13 import org.slf4j.Logger; 20 14 import org.slf4j.LoggerFactory; 21 15 22 public class ResourceLinkPanel extends Panel {23 16 24 private final static Logger LOG = LoggerFactory.getLogger(ResourceLinkPanel.class); 17 /** 18 * Panel showing a resource link. 19 * 20 * In the extension of the link panel class, this class adds a label text and 21 * an icon that is specific for search page links. 22 */ 23 public class ResourceLinkPanel extends LinkPanel { 24 25 private final static Logger LOG = 26 LoggerFactory.getLogger(ResourceLinkPanel.class); 25 27 26 28 private static final long serialVersionUID = 1L; 27 29 28 private final static String URN_NBN_RESOLVER_URL = "http://www.nbn-resolving.org/redirect/"; 30 private final static String URN_NBN_RESOLVER_URL = 31 "http://www.nbn-resolving.org/redirect/"; 29 32 30 private final static ImageResource ANNOTATION = new ImageResource(new ContextRelativeResource("Images/text-x-log.png"), 31 "Annotation file"); 32 private final static ImageResource AUDIO = new ImageResource(new ContextRelativeResource("Images/audio-x-generic.png"), "Audio file"); 33 private final static ImageResource IMAGE = new ImageResource(new ContextRelativeResource("Images/image-x-generic.png"), "Image file"); 34 private final static ImageResource TEXT = new ImageResource(new ContextRelativeResource("Images/text-x-generic.png"), "Text file"); 35 private final static ImageResource VIDEO = new ImageResource(new ContextRelativeResource("Images/video-x-generic.png"), "Video file"); 33 private final static ImageResource ANNOTATION = new ImageResource(new 34 ContextRelativeResource("Images/text-x-log.png"), "Annotation file"); 35 private final static ImageResource AUDIO = new ImageResource(new 36 ContextRelativeResource("Images/audio-x-generic.png"), "Audio file"); 37 private final static ImageResource IMAGE = new ImageResource(new 38 ContextRelativeResource("Images/image-x-generic.png"), "Image file"); 39 private final static ImageResource TEXT = new ImageResource(new 40 ContextRelativeResource("Images/text-x-generic.png"), "Text file"); 41 private final static ImageResource VIDEO = new ImageResource(new 42 ContextRelativeResource("Images/video-x-generic.png"), "Video file"); 36 43 37 private final static Map<String, ImageResource> ICON_MAP = new HashMap<String, ImageResource>(); 44 private final static Map<String, ImageResource> ICON_MAP = new 45 HashMap<String, ImageResource>(); 38 46 static { 39 47 ICON_MAP.put(FacetConstants.RESOURCE_TYPE_AUDIO, AUDIO); … … 43 51 ICON_MAP.put(FacetConstants.RESOURCE_TYPE_ANNOTATION, ANNOTATION); 44 52 } 45 53 54 /** 55 * Panel constructor 56 * 57 * @param id Wicket mark up identifier 58 * @param mimeType mime type of the resource indicated 59 * @param resourceLink URL to pointing to the resource 60 */ 46 61 public ResourceLinkPanel(String id, String mimeType, String resourceLink) { 47 62 super(id); … … 58 73 add(link); 59 74 } 60 61 /** 62 * Modifies resourceLink if necessary (adds support for different URN resolvers) 63 * @param resourceLink 64 * @return Modified resourceLink, if no modifications are necessary original parameter resourceLink is returned 65 */ 66 private String getHref(String resourceLink) { 67 String result = resourceLink; 68 if (resourceLink != null) { 69 if (resourceLink.startsWith(FacetConstants.HANDLE_PREFIX)) { 70 String handle = resourceLink.substring(FacetConstants.HANDLE_PREFIX.length()); 71 result = VloConfig.getHandleServerUrl() + handle; 72 } else if(resourceLink.startsWith(FacetConstants.URN_NBN_PREFIX)) { 73 result = URN_NBN_RESOLVER_URL+resourceLink; 74 } 75 } 76 return result; 77 } 78 79 protected String getNameFromLink(String resourceLink) { 80 String result = resourceLink; 81 // We ALWAYS backoff to the resourceLink as default thingy. 82 83 84 85 // HandleResolver does not work at the moment on the clarin server see http://trac.clarin.eu/ticket/136, Disabled it for the release. 86 // if (resourceLink != null) { 87 // if (resourceLink.startsWith(FacetConstants.HANDLE_PREFIX)) { 88 // try { 89 // String handle = resourceLink.substring(FacetConstants.HANDLE_PREFIX.length()); 90 // HandleResolver handleResolver = new HandleResolver(); 91 // handleResolver.setTcpTimeout(5000);//5 secs, default is one minute 92 // HandleValue values[] = handleResolver.resolveHandle(handle, new String[] { "URL" }, null); 93 // 94 // for (HandleValue handleValue : values) { 95 // String url = handleValue.getDataAsString(); 96 // int index = url.lastIndexOf('/'); 97 // if (index != -1) { 98 // String name = url.substring(index + 1).trim(); 99 // if (name.length() > 1) { 100 // result = name + " (" + resourceLink + ")"; 101 // } 102 // break; 103 // } 104 // } 105 // } catch (HandleException e) { 106 // LOG.warn("Error trying to get the name of the handle", e); 107 // } 108 // } 109 // } 110 111 /** NOTE: We are trying a different approach from the "official" one. 112 * Will use the REST interface of hdl.handle.net. 113 */ 114 if (resourceLink != null) { 115 if (resourceLink.startsWith(FacetConstants.HANDLE_PREFIX)) { 116 String handle = resourceLink.substring(FacetConstants.HANDLE_PREFIX.length()); 117 resourceLink = VloConfig.getHandleServerUrl() + handle; 118 // Now points to something like http://hdl.handle.net/1839/00-0000-0000-0004-3357-F 119 HttpURLConnection con = null; 120 URL u; 121 try{ 122 u = new URL(resourceLink); 123 System.out.println(resourceLink); 124 URLConnection c = u.openConnection(); 125 if(c instanceof HttpURLConnection){ 126 con = (HttpURLConnection) c; 127 } 128 if(con != null){ 129 if(con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || 130 con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP || 131 con.getResponseCode() == HttpURLConnection.HTTP_SEE_OTHER){ 132 result = con.getURL().toString(); 133 /* for (Map.Entry<String, List<String>> header : con.getHeaderFields().entrySet()) { 134 if(header != null && header.getKey() != null){ 135 System.out.println("XXX:" + header.getKey()); 136 System.out.println(header.getValue().get(0)); 137 if(header.getKey().equals("Location")){ 138 result = header.getValue().get(0); 139 } 140 } 141 }*/ 142 } 143 } 144 } catch (MalformedURLException e) { 145 LOG.warn("Error trying to get the name of the handle", e); 146 } catch (IOException e) { 147 LOG.warn("Error trying to get the name of the handle", e); 148 } 149 } 150 } 151 return result; 152 } 153 75 154 76 private ImageResource getImage(String mimeType) { 155 77 ImageResource image = ICON_MAP.get(CommonUtils.normalizeMimeType(mimeType));
Note: See TracChangeset
for help on using the changeset viewer.