- added first release of shortener url
- commented send to on folder git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@77391 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
a89419f959
commit
d8b45e32b8
|
@ -434,9 +434,10 @@ public class AppControllerExplorer implements EventHandler, TreeAppControllerInt
|
|||
String shareLinkUrl = currentUrl+ "?" +ConstantsExplorer.GET_ITEMID_PARAMETER+"="+getLinkEvent.getSourceFile().getIdentifier();
|
||||
shareLinkUrl+="&"+ConstantsExplorer.GET_OPEARATION_PARAMETER+"="+WsPortletInitOperation.gotofolder;
|
||||
|
||||
|
||||
|
||||
DialogShareLink dialog = new DialogShareLink("Copy to clipboard Share Link: Ctrl+C", shareLinkUrl);
|
||||
dialog.show();
|
||||
dialog.selectTxt();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -157,5 +157,7 @@ public interface GWTWorkspaceService extends RemoteService{
|
|||
* @throws Exception
|
||||
*/
|
||||
List<FileGridModel> getFolderChildrenForFileGridById(String folderId) throws Exception;
|
||||
|
||||
String getShortUrl(String longUrl) throws Exception;
|
||||
|
||||
}
|
||||
|
|
|
@ -152,5 +152,7 @@ public interface GWTWorkspaceServiceAsync {
|
|||
void getFolderChildrenForFileGridById(String folderId,
|
||||
AsyncCallback<List<FileGridModel>> callback);
|
||||
|
||||
void getShortUrl(String longUrl, AsyncCallback<String> callback);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -635,6 +635,10 @@ public class ContextMenuTree {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* switch visible operation on context menu according to selected item
|
||||
* @param selectedItem
|
||||
*/
|
||||
public void contextMenuSwitch(FileModel selectedItem) {
|
||||
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.INSERT_FOLDER.getId()).setVisible(true); //insert folder
|
||||
|
@ -642,6 +646,7 @@ public class ContextMenuTree {
|
|||
contextMenu.getItemByItemId(WorkspaceOperation.UPLOAD_FILE.getId()).setVisible(true);
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.UPLOAD_ARCHIVE.getId()).setVisible(true);
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.ADD_URL.getId()).setVisible(true);
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.SENDTO.getId()).setVisible(true); //send to
|
||||
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.PREVIEW.getId()).setVisible(false); //preview image
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.LINK.getId()).setVisible(false); //open link
|
||||
|
@ -650,6 +655,7 @@ public class ContextMenuTree {
|
|||
contextMenu.getItemByItemId(WorkspaceOperation.OPEN_REPORT_TEMPLATE.getId()).setVisible(false); //open report template
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.PASTE.getId()).setEnabled(false); //paste
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.REFRESH_FOLDER.getId()).setVisible(false); //refresh
|
||||
|
||||
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.SHARE.getId()).setVisible(false); //SHARE
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.UNSHARE.getId()).setVisible(false); //UNSHARE
|
||||
|
@ -675,6 +681,7 @@ public class ContextMenuTree {
|
|||
contextMenu.getItemByItemId(WorkspaceOperation.SHARE.getId()).setVisible(true); //SHARE
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.SHOW.getId()).setVisible(false); //show
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.REFRESH_FOLDER.getId()).setVisible(true); //refresh
|
||||
contextMenu.getItemByItemId(WorkspaceOperation.SENDTO.getId()).setVisible(false); //send to
|
||||
if(selectedItem.isShared()){//IS SHARED
|
||||
|
||||
if(selectedItem.isShareable()){ //IS SHARABLE
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.gcube.portlets.user.workspace.client.view.windows;
|
||||
|
||||
import org.gcube.portlets.user.workspace.client.AppControllerExplorer;
|
||||
import org.gcube.portlets.user.workspace.client.resources.Resources;
|
||||
|
||||
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
|
||||
|
@ -11,6 +12,7 @@ import com.extjs.gxt.ui.client.widget.VerticalPanel;
|
|||
import com.extjs.gxt.ui.client.widget.form.TextArea;
|
||||
import com.extjs.gxt.ui.client.widget.form.TextField;
|
||||
import com.google.gwt.dom.client.Style.Unit;
|
||||
import com.google.gwt.user.client.rpc.AsyncCallback;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
|
@ -24,7 +26,7 @@ public class DialogShareLink extends Dialog {
|
|||
private VerticalPanel vp = new VerticalPanel();
|
||||
// private Label label = new Label();
|
||||
|
||||
public DialogShareLink(String headingTxt, String urlValue) {
|
||||
public DialogShareLink(String headingTxt, final String urlValue) {
|
||||
setButtonAlign(HorizontalAlignment.CENTER);
|
||||
setIcon(Resources.getIconShareLink());
|
||||
vp.setHorizontalAlign(HorizontalAlignment.CENTER);
|
||||
|
@ -46,9 +48,31 @@ public class DialogShareLink extends Dialog {
|
|||
txt.setWidth(widht-20);
|
||||
txt.setHeight(height-74);
|
||||
// txt.setFieldLabel(msgTitle);
|
||||
txt.setValue(urlValue);
|
||||
|
||||
txt.setReadOnly(true);
|
||||
|
||||
|
||||
txt.mask("Shorting link...");
|
||||
AppControllerExplorer.rpcWorkspaceService.getShortUrl(urlValue, new AsyncCallback<String>() {
|
||||
|
||||
@Override
|
||||
public void onSuccess(String shortUrl) {
|
||||
txt.unmask();
|
||||
txt.setValue(shortUrl);
|
||||
selectTxt();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable caught) {
|
||||
txt.unmask();
|
||||
txt.setValue(urlValue);
|
||||
selectTxt();
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
this.getButtonById(Dialog.CLOSE).addSelectionListener(new SelectionListener<ButtonEvent>() {
|
||||
|
||||
@Override
|
||||
|
@ -72,6 +96,8 @@ public class DialogShareLink extends Dialog {
|
|||
}
|
||||
|
||||
public void selectTxt(){
|
||||
txt.select(0, txt.getValue().length());
|
||||
|
||||
if(txt.getValue()!=null)
|
||||
txt.select(0, txt.getValue().length());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,226 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.portlets.user.workspace.server;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.StringReader;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.gcube.common.core.scope.GCUBEScope;
|
||||
import org.gcube.common.core.utils.logging.GCUBELog;
|
||||
import org.gcube.portlets.user.homelibrary.home.HomeLibrary;
|
||||
import org.gcube.portlets.user.homelibrary.home.exceptions.InternalErrorException;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.Workspace;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceFolder;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceItem;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.ExternalResourceBrokenLinkException;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.ExternalResourcePluginNotFoundException;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.ItemNotFoundException;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.FolderItem;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.AquaMapsItem;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ExternalFile;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ExternalImage;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ExternalResourceLink;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ExternalUrl;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.Query;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.Report;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ReportTemplate;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.gcube.Document;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.gcube.Metadata;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ts.TimeSeries;
|
||||
import org.gcube.portlets.user.homelibrary.util.Extensions;
|
||||
import org.gcube.portlets.user.homelibrary.util.MimeTypeUtil;
|
||||
import org.gcube.portlets.user.homelibrary.util.zip.ZipUtil;
|
||||
import org.gcube.portlets.user.workspace.client.ConstantsExplorer;
|
||||
import org.gcube.portlets.user.workspace.server.property.PortalUrlGroupGatewayProperty;
|
||||
import org.gcube.portlets.user.workspace.server.util.WsUtil;
|
||||
import org.gcube.portlets.user.workspace.shared.HandlerResultMessage;
|
||||
|
||||
/**
|
||||
* @author Federico De Faveri defaveri@isti.cnr.it
|
||||
*
|
||||
*/
|
||||
public class DownloadPublicLinkServlet extends HttpServlet{
|
||||
|
||||
private static final long serialVersionUID = -8423345575690165644L;
|
||||
|
||||
protected static GCUBELog logger = new GCUBELog(DownloadPublicLinkServlet.class);
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void init() throws ServletException {
|
||||
super.init();
|
||||
logger.trace("Workspace DownloadServlet ready.");
|
||||
}
|
||||
|
||||
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
|
||||
String smp = req.getParameter("smp");
|
||||
boolean viewContent = (req.getParameter("viewContent")==null)?false:req.getParameter("viewContent").equals("true");
|
||||
|
||||
logger.trace("Input Params [smp: "+smp + ", viewContent: "+viewContent+"]");
|
||||
|
||||
if(smp==null || smp.isEmpty()){
|
||||
sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Item id is null");
|
||||
return;
|
||||
}
|
||||
|
||||
logger.trace("PUBLIC FILE DOWNLOAD REQUEST "+smp);
|
||||
|
||||
//COMMENTED 26/03/2013
|
||||
// String itemName = MimeTypeUtil.getNameWithExtension(item.getName(), mimeType);
|
||||
|
||||
// String contentDisposition = (viewContent)?"inline":"attachment";
|
||||
// //COMMENTED 26/03/2013
|
||||
//// resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + itemName + "\"" );
|
||||
// resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + item.getName() + "\"" );
|
||||
//
|
||||
// resp.setContentType(mimeType);
|
||||
//
|
||||
// resp.setContentLength((int) externalFile.getLength());
|
||||
//
|
||||
//
|
||||
// //MODIFIED 22-05-2013 CLOSE STREAM
|
||||
//// IOUtils.copy(externalFile.getData(), resp.getOutputStream());
|
||||
// is = externalFile.getData();
|
||||
// out = resp.getOutputStream();
|
||||
// IOUtils.copy(is, out);
|
||||
//
|
||||
// is.close();
|
||||
// out.close();
|
||||
|
||||
}
|
||||
|
||||
// protected void handleError(boolean urlRedirectOnError, HttpServletRequest req, HttpServletResponse resp, String itemId, String message) throws IOException{
|
||||
//
|
||||
// logger.warn("Handle error occurred: "+message);
|
||||
// if(urlRedirectOnError){
|
||||
// urlRedirect(req, resp, itemId);
|
||||
// }else
|
||||
// sendError(resp,message);
|
||||
//
|
||||
// }
|
||||
|
||||
protected void sendError(HttpServletResponse response, String message) throws IOException
|
||||
{
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
HandlerResultMessage resultMessage = HandlerResultMessage.errorResult(message);
|
||||
response.getWriter().write(resultMessage.toString());
|
||||
response.flushBuffer();
|
||||
}
|
||||
|
||||
public static String getRequestURL(HttpServletRequest req) {
|
||||
|
||||
String scheme = req.getScheme(); // http
|
||||
String serverName = req.getServerName(); // hostname.com
|
||||
int serverPort = req.getServerPort(); // 80
|
||||
String contextPath = req.getContextPath(); // /mywebapp
|
||||
// String servletPath = req.getServletPath(); // /servlet/MyServlet
|
||||
// String pathInfo = req.getPathInfo(); // /a/b;c=123
|
||||
// String queryString = req.getQueryString(); // d=789
|
||||
|
||||
// Reconstruct original requesting URL
|
||||
StringBuffer url = new StringBuffer();
|
||||
url.append(scheme).append("://").append(serverName);
|
||||
|
||||
if ((serverPort != 80) && (serverPort != 443)) {
|
||||
url.append(":").append(serverPort);
|
||||
}
|
||||
|
||||
logger.trace("server: "+url);
|
||||
logger.trace("contextPath: "+contextPath);
|
||||
// logger.trace("servletPath: "+servletPath);
|
||||
// url.append(contextPath).append(servletPath);
|
||||
|
||||
|
||||
url.append(contextPath);
|
||||
|
||||
// if (pathInfo != null) {
|
||||
// url.append(pathInfo);
|
||||
// }
|
||||
// if (queryString != null) {
|
||||
// url.append("?").append(queryString);
|
||||
|
||||
// }
|
||||
|
||||
PortalUrlGroupGatewayProperty p = new PortalUrlGroupGatewayProperty();
|
||||
|
||||
int lenght = p.getPath().length();
|
||||
|
||||
String groupgatewaypath = "/";
|
||||
|
||||
if(lenght>1){
|
||||
|
||||
String lastChar = p.getPath().substring(lenght-1, lenght-1);
|
||||
|
||||
groupgatewaypath+= lastChar.compareTo("/")!=0?p.getPath()+"/":p.getPath();
|
||||
}
|
||||
|
||||
url.append(groupgatewaypath);
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
InputStream is = null;
|
||||
|
||||
logger.trace("start");
|
||||
|
||||
// is = GCUBEStorage.getRemoteFile("/Home/test.user/Workspace3d660604-03ef-49eb-89c3-4c73f8a47914");
|
||||
|
||||
try{
|
||||
|
||||
Workspace ws = HomeLibrary.getHomeManagerFactory().getHomeManager().getHome("francesco.mangiacrapa", GCUBEScope.getScope("/gcube/devsec")).getWorkspace();
|
||||
//
|
||||
// ExternalFile f = (ExternalFile) ws.getItem("907ce8ef-5c0b-4601-83ac-215d1f432f6b");
|
||||
|
||||
WorkspaceItem wsItem = ws.getItem("907ce8ef-5c0b-4601-83ac-215d1f432f6b");
|
||||
|
||||
|
||||
logger.trace("metadata info recovered from HL: [ID: "+wsItem.getId() +", name: "+wsItem.getName()+"]");
|
||||
|
||||
|
||||
|
||||
|
||||
FileOutputStream out = new FileOutputStream(new File("/tmp/bla"));
|
||||
// byte[] buffer = new byte[1024];
|
||||
// int len;
|
||||
// while ((len = is.read(buffer)) != -1) {
|
||||
// out.write(buffer, 0, len);
|
||||
// }
|
||||
|
||||
logger.trace("cast as external file");
|
||||
ExternalFile f = (ExternalFile) wsItem;
|
||||
|
||||
is = f.getData();
|
||||
|
||||
IOUtils.copy(is, out);
|
||||
is.close();
|
||||
|
||||
out.close();
|
||||
|
||||
// logger.trace("Sleeping");
|
||||
// Thread.sleep(20000);
|
||||
// logger.trace("Alive");
|
||||
|
||||
logger.trace("end");
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -666,7 +666,7 @@ public class DownloadServlet extends HttpServlet{
|
|||
logger.trace("start");
|
||||
|
||||
// is = GCUBEStorage.getRemoteFile("/Home/test.user/Workspace3d660604-03ef-49eb-89c3-4c73f8a47914");
|
||||
|
||||
|
||||
try{
|
||||
|
||||
Workspace ws = HomeLibrary.getHomeManagerFactory().getHomeManager().getHome("francesco.mangiacrapa", GCUBEScope.getScope("/gcube/devsec")).getWorkspace();
|
||||
|
|
|
@ -50,6 +50,7 @@ import org.gcube.portlets.user.workspace.client.rpc.GWTWorkspaceService;
|
|||
import org.gcube.portlets.user.workspace.client.workspace.GWTWorkspaceItem;
|
||||
import org.gcube.portlets.user.workspace.server.notifications.NotificationsProducer;
|
||||
import org.gcube.portlets.user.workspace.server.notifications.NotificationsUtil;
|
||||
import org.gcube.portlets.user.workspace.server.shortner.UrlShortener;
|
||||
import org.gcube.portlets.user.workspace.server.util.UserUtil;
|
||||
import org.gcube.portlets.user.workspace.server.util.WsUtil;
|
||||
import org.gcube.portlets.user.workspace.server.util.scope.ScopeUtilFilter;
|
||||
|
@ -2190,4 +2191,21 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
|
|||
throw new Exception(error);
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.gcube.portlets.user.workspace.client.rpc.GWTWorkspaceService#getShortUrl(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String getShortUrl(String longUrl) throws Exception {
|
||||
|
||||
workspaceLogger.trace("get short url for "+ longUrl);
|
||||
|
||||
try{
|
||||
return UrlShortener.shorten(longUrl);
|
||||
|
||||
}catch (Exception e) {
|
||||
workspaceLogger.error("Error get short url for ", e);
|
||||
return longUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,134 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.portlets.user.workspace.server.resolver;
|
||||
|
||||
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
||||
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||
import org.gcube.resources.discovery.client.queries.impl.XQuery;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
* @Jun 26, 2013
|
||||
*
|
||||
*/
|
||||
public class UriResolverReaderParameter {
|
||||
|
||||
//Base Address
|
||||
String uri = "";
|
||||
//Query URL parameter
|
||||
String parameter = "";
|
||||
private String uriRequest = "";
|
||||
|
||||
public static Logger log = Logger.getLogger(UriResolverReaderParameter.class);
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*
|
||||
*/
|
||||
public UriResolverReaderParameter() throws Exception {
|
||||
|
||||
ScopeProvider.instance.set("/gcube/devsec");
|
||||
|
||||
XQuery query = queryFor(ServiceEndpoint.class);
|
||||
|
||||
query.addCondition("$resource/Profile/Name/text() eq 'HTTP-URI-Resolver'").setResult("$resource/Profile/AccessPoint");
|
||||
|
||||
DiscoveryClient<AccessPoint> client = clientFor(AccessPoint.class);
|
||||
|
||||
List<AccessPoint> endpoints = client.submit(query);
|
||||
|
||||
if (endpoints.size() == 0)
|
||||
throw new Exception("No Resolver available");
|
||||
|
||||
//Base Address
|
||||
// System.out.println(endpoints.get(0).address());
|
||||
|
||||
uri = endpoints.get(0)!=null?endpoints.get(0).address():"";
|
||||
|
||||
if(endpoints.get(0)!=null){
|
||||
|
||||
parameter = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get("parameter").value():"";
|
||||
}
|
||||
|
||||
uriRequest = uri+"?"+parameter;
|
||||
|
||||
|
||||
//Query URL parameter
|
||||
// System.out.println(endpoints.get(0).propertyMap().get("parameter").value());
|
||||
|
||||
}
|
||||
|
||||
public InputStream resolve(String smp) throws IOException{
|
||||
String query = uriRequest + "="+smp;
|
||||
|
||||
URL url = new URL(query);
|
||||
|
||||
return url.openStream();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Base Address of Uri Resolver
|
||||
*/
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Query URL parameter of Uri Resolver
|
||||
*/
|
||||
public String getParameter() {
|
||||
return parameter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("UriResolverReaderParameter [uri=");
|
||||
builder.append(uri);
|
||||
builder.append(", parameter=");
|
||||
builder.append(parameter);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
log.trace(new UriResolverReaderParameter());
|
||||
|
||||
UriResolverReaderParameter uriResolver = new UriResolverReaderParameter();
|
||||
InputStream is = uriResolver.resolve("smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV+gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y");
|
||||
|
||||
File file = new File("test.png");
|
||||
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
|
||||
|
||||
IOUtils.copy(is, out);
|
||||
is.close();
|
||||
|
||||
out.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,179 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.portlets.user.workspace.server.shortner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.httpclient.Credentials;
|
||||
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpException;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
||||
import org.apache.commons.httpclient.UsernamePasswordCredentials;
|
||||
import org.apache.commons.httpclient.auth.AuthScope;
|
||||
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
|
||||
import org.apache.commons.httpclient.methods.GetMethod;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.params.HttpMethodParams;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
* @Jun 4, 2013
|
||||
*
|
||||
*/
|
||||
public class HttpCallerUtil {
|
||||
|
||||
public static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(HttpCallerUtil.class);
|
||||
|
||||
public static final int TIME_OUT_REQUESTS = 5000;
|
||||
|
||||
|
||||
private String urlService = "";
|
||||
private HttpClient httpClient = null;
|
||||
private String username = "";
|
||||
private String password = "";
|
||||
|
||||
|
||||
public HttpCallerUtil(String url, String user, String password) {
|
||||
this.urlService = url;
|
||||
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
|
||||
connectionManager.getParams().setSoTimeout(TIME_OUT_REQUESTS);
|
||||
this.httpClient = new HttpClient(connectionManager);
|
||||
|
||||
this.username = user;
|
||||
this.password = password;
|
||||
Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
|
||||
httpClient.getState().setCredentials(AuthScope.ANY, defaultcreds);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String callGet(String urlMethod, Map<String, String> parameters) throws Exception {
|
||||
|
||||
// Create an instance of HttpClient.
|
||||
HttpClient client = new HttpClient();
|
||||
|
||||
String query = UrlEncoderUtil.encodeQuery(parameters);
|
||||
|
||||
String fullUrl = urlService+"/"+urlMethod+"?"+query;
|
||||
|
||||
logger.info("call get .... " + fullUrl);
|
||||
|
||||
// Create a method instance.
|
||||
GetMethod method = new GetMethod(fullUrl);
|
||||
|
||||
// Provide custom retry handler is necessary
|
||||
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
|
||||
|
||||
try {
|
||||
// Execute the method.
|
||||
int statusCode = client.executeMethod(method);
|
||||
|
||||
if (statusCode != HttpStatus.SC_OK) {
|
||||
logger.error("Method failed: " + method.getStatusLine());
|
||||
}
|
||||
|
||||
// Read the response body.
|
||||
byte[] responseBody = method.getResponseBody();
|
||||
|
||||
// Deal with the response.
|
||||
// Use caution: ensure correct character encoding and is not binary
|
||||
// data
|
||||
return new String(responseBody);
|
||||
|
||||
} catch (HttpException e) {
|
||||
logger.error("Fatal protocol violation: " + e.getMessage());
|
||||
throw new Exception("Fatal protocol violation: " + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
logger.error("Fatal transport error: " + e.getMessage());
|
||||
throw new Exception("Fatal transport violation: " + e.getMessage());
|
||||
} finally {
|
||||
// Release the connection.
|
||||
method.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String callPost(String urlMethod, String body, String contentType) throws Exception {
|
||||
|
||||
// Create a method instance.
|
||||
PostMethod method = new PostMethod(this.urlService + "/" + urlMethod);
|
||||
|
||||
method.setRequestHeader("Content-type", contentType);
|
||||
|
||||
logger.trace("call post .... " + this.urlService + "/" + urlMethod);
|
||||
logger.debug(" call post body.... " + body);
|
||||
// System.out.println("post body .... " + body);
|
||||
|
||||
method.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));
|
||||
|
||||
// Provide custom retry handler is necessary
|
||||
// method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(3, false));
|
||||
|
||||
byte[] responseBody = null;
|
||||
try {
|
||||
// Execute the method.
|
||||
int statusCode = httpClient.executeMethod(method);
|
||||
|
||||
if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
|
||||
logger.error("Method failed: " + method.getStatusLine()+"; Response bpdy: "+method.getResponseBody());
|
||||
method.releaseConnection();
|
||||
throw new Exception("Method failed: " + method.getStatusLine()+"; Response body: "+new String(method.getResponseBody()));
|
||||
}
|
||||
// Read the response body.
|
||||
responseBody = method.getResponseBody();
|
||||
|
||||
// Deal with the response.
|
||||
// Use caution: ensure correct character encoding and is not binary data
|
||||
// System.out.println(new String(responseBody));
|
||||
} catch (HttpException e) {
|
||||
logger.error("Fatal protocol violation: ", e);
|
||||
// e.printStackTrace();
|
||||
method.releaseConnection();
|
||||
throw new Exception("Fatal protocol violation: " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
logger.error("Fatal transport error: ", e);
|
||||
// e.printStackTrace();
|
||||
method.releaseConnection();
|
||||
throw new Exception("Fatal transport error: " + e.getMessage());
|
||||
}
|
||||
method.releaseConnection();
|
||||
return new String(responseBody);
|
||||
}
|
||||
|
||||
public String getUrlservice() {
|
||||
return urlService;
|
||||
}
|
||||
|
||||
public void setUrlservice(String urlservice) {
|
||||
this.urlService = urlservice;
|
||||
}
|
||||
|
||||
public HttpClient getClient() {
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public void setClient(HttpClient client) {
|
||||
this.httpClient = client;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.portlets.user.workspace.server.shortner;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
* @Jun 4, 2013
|
||||
*
|
||||
*/
|
||||
public class UrlEncoderUtil {
|
||||
|
||||
public static String charset = "UTF-8";
|
||||
|
||||
public static Logger logger = Logger.getLogger(UrlEncoderUtil.class);
|
||||
/**
|
||||
*
|
||||
* @param url
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
public static String encodeQuery(String... parameters){
|
||||
|
||||
String query = "";
|
||||
for (String string : parameters) {
|
||||
|
||||
try {
|
||||
query+=URLEncoder.encode(string, charset)+"&";
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
logger.error(e);
|
||||
return query;
|
||||
}
|
||||
|
||||
}
|
||||
return removeLastChar(query);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param url
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
public static String encodeQuery(Map<String, String> parameters){
|
||||
|
||||
String query = "";
|
||||
|
||||
if(parameters==null)
|
||||
return query;
|
||||
|
||||
for (String key : parameters.keySet()) {
|
||||
|
||||
try {
|
||||
|
||||
query+=String.format(key+"=%s", URLEncoder.encode(parameters.get(key), charset))+"&";
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
logger.error(e);
|
||||
return query;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return removeLastChar(query);
|
||||
|
||||
}
|
||||
|
||||
public static String removeLastChar(String string){
|
||||
|
||||
if(string == null)
|
||||
return null;
|
||||
|
||||
if(string.length()>0)
|
||||
return string.substring(0, string.length()-1);
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// System.out.println(UrlEncoderUtil.encodeQuery("request=GetStyles", "layers=test Name", "service=WMS", "version=1.1.1"));
|
||||
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
parameters.put("request", "GetStyles");
|
||||
parameters.put("layers", "test Name");
|
||||
parameters.put("version", "1.1.1");
|
||||
|
||||
System.out.println(UrlEncoderUtil.encodeQuery(parameters));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.portlets.user.workspace.server.shortner;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
* @Jun 28, 2013
|
||||
*
|
||||
*/
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
|
||||
public final class UrlShortener {
|
||||
|
||||
protected static final String APPLICATION_JSON = "application/json";
|
||||
protected static final String LONG_URL = "longUrl";
|
||||
|
||||
protected static String shortnerServerUrl = "https://www.googleapis.com/urlshortener/v1/url";
|
||||
|
||||
protected static String urlMethod = "";
|
||||
protected static String authenticationKeyValue ="AIzaSyDfMO0VY3o8GjRUqnTfqScjm_EsFEuBa3g";
|
||||
protected static String authenticationKeyParam = "key";
|
||||
|
||||
protected static Logger logger = Logger.getLogger(UrlShortener.class);
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public UrlShortener() {
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param longUrl
|
||||
* @return a shorten url
|
||||
*/
|
||||
public static String shorten(String longUrl) throws Exception{
|
||||
|
||||
if (longUrl == null) {
|
||||
return longUrl;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
URL url = new URL(shortnerServerUrl);
|
||||
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setDoOutput(true);
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", APPLICATION_JSON);
|
||||
|
||||
JSONObject jsonObj = new JSONObject();
|
||||
|
||||
jsonObj.put(LONG_URL, longUrl);
|
||||
// jsonObj.put(authenticationKeyParam, authenticationKeyValue);
|
||||
|
||||
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
||||
wr.write(jsonObj.toJSONString());
|
||||
wr.flush();
|
||||
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject jsonObject = (JSONObject) parser.parse(rd);
|
||||
|
||||
|
||||
wr.close();
|
||||
rd.close();
|
||||
return (String) jsonObject.get("id"); //is shorted url
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
|
||||
logger.error("MalformedURLException error in UrlShortener", e);
|
||||
return longUrl;
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.error("IOException error in UrlShortener", e);
|
||||
return longUrl;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String shorten;
|
||||
|
||||
try {
|
||||
shorten = UrlShortener.shorten("https://dev.d4science.org/group/data-e-infrastructure-gateway/workspace?itemid=062c558c-c7ce-4de3-a4c7-e1411816cc12&operation=gotofolder");
|
||||
|
||||
System.out.println("Shorted: "+shorten);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,249 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.portlets.user.workspace;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
* @Jun 28, 2013
|
||||
*
|
||||
*/
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
|
||||
public final class GoogleUrlShortener {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected static final String APPLICATION_JSON = "application/json";
|
||||
protected static final String LONG_URL = "longUrl";
|
||||
|
||||
static String shortnerServerUrl = "https://www.googleapis.com/urlshortener/v1/url";
|
||||
|
||||
static String urlMethod = "";
|
||||
static String authenticationKeyValue ="AIzaSyDfMO0VY3o8GjRUqnTfqScjm_EsFEuBa3g";
|
||||
static String authenticationKeyParam = "key";
|
||||
|
||||
// static String shortUrl = "https://dev.d4science.org/group/data-e-infrastructure-gateway/workspace?itemid=062c558c-c7ce-4de3-a4c7-e1411816cc12&operation=gotofolder";
|
||||
|
||||
|
||||
|
||||
public static String shorten(String longUrl) throws Exception {
|
||||
|
||||
if (longUrl == null) {
|
||||
return longUrl;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
URL url = new URL(shortnerServerUrl);
|
||||
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setDoOutput(true);
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", APPLICATION_JSON);
|
||||
|
||||
JSONObject jsonObj = new JSONObject();
|
||||
|
||||
jsonObj.put(LONG_URL, longUrl);
|
||||
jsonObj.put(authenticationKeyParam, authenticationKeyValue);
|
||||
|
||||
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
||||
wr.write(jsonObj.toJSONString());
|
||||
wr.flush();
|
||||
|
||||
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject jsonObject = (JSONObject) parser.parse(rd);
|
||||
|
||||
|
||||
wr.close();
|
||||
rd.close();
|
||||
return (String) jsonObject.get("id"); //is shorted url
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
return longUrl;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return longUrl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String shorten4(String longUrl) throws Exception {
|
||||
|
||||
if (longUrl == null) {
|
||||
return longUrl;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
|
||||
JSONObject jsonObj = new JSONObject();
|
||||
|
||||
jsonObj.put(LONG_URL, longUrl);
|
||||
jsonObj.put(authenticationKeyParam, authenticationKeyValue);
|
||||
String body = jsonObj.toJSONString();
|
||||
|
||||
HttpCallerUtil httpCaller = new HttpCallerUtil(shortnerServerUrl, "", "");
|
||||
|
||||
String json = httpCaller.callPost("", body, APPLICATION_JSON);
|
||||
|
||||
System.out.println("json: "+json);
|
||||
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject jsonObject = (JSONObject) parser.parse(json);
|
||||
|
||||
return (String) jsonObject.get("id"); //is shorted url
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
return longUrl;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return longUrl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static String shorten3(String longUrl) throws Exception {
|
||||
|
||||
if (longUrl == null) {
|
||||
return longUrl;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
|
||||
HttpCallerUtil httpCaller = new HttpCallerUtil(shortnerServerUrl, "", "");
|
||||
|
||||
// Map<String, String> parameters = new HashMap<String, String>();
|
||||
//
|
||||
// parameters.put("shortUrl", longUrl);
|
||||
// parameters.put("key", authenticationKey);
|
||||
|
||||
|
||||
// String json = httpCaller.callGet(ulrMethod, parameters);
|
||||
|
||||
String body = "{\"longUrl\":\"" + longUrl + "\", \"key\":\"" + authenticationKeyValue + "\"}";
|
||||
|
||||
urlMethod += authenticationKeyParam +"="+authenticationKeyValue;
|
||||
|
||||
String json = httpCaller.callPost("", body, APPLICATION_JSON);
|
||||
|
||||
System.out.println("json: "+json);
|
||||
|
||||
|
||||
// return json.substring(json.indexOf("http"), json.indexOf("\"", json.indexOf("http")));
|
||||
|
||||
return json;
|
||||
|
||||
// httpCaller.callGet(urlMethod, parameters);
|
||||
|
||||
// return json.substring(json.indexOf("http"), json.indexOf("\"", json.indexOf("http")));
|
||||
|
||||
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
return longUrl;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return longUrl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static String googUrl = "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyDfMO0VY3o8GjRUqnTfqScjm_EsFEuBa3g";
|
||||
|
||||
|
||||
public static String shorten2(String longUrl)
|
||||
{
|
||||
String shortUrl = "";
|
||||
|
||||
try
|
||||
{
|
||||
HttpURLConnection conn = (HttpURLConnection) new URL(googUrl).openConnection();
|
||||
conn.setDoOutput(true);
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", APPLICATION_JSON);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
|
||||
wr.write("{\"longUrl\":\"" + longUrl + "\"}");
|
||||
wr.flush();
|
||||
|
||||
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line = null;
|
||||
while ((line = rd.readLine()) != null) {
|
||||
sb.append(line + '\n');
|
||||
}
|
||||
String json = sb.toString();
|
||||
System.out.println("json: "+json);
|
||||
|
||||
wr.close();
|
||||
rd.close();
|
||||
return json.substring(json.indexOf("http"), json.indexOf("\"", json.indexOf("http")));
|
||||
|
||||
// Get the response
|
||||
// BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
// String line;
|
||||
// while ((line = rd.readLine()) != null)
|
||||
// {
|
||||
// if (line.indexOf("id") > -1)
|
||||
// {
|
||||
// // I'm sure there's a more elegant way of parsing
|
||||
// // the JSON response, but this is quick/dirty =)
|
||||
// shortUrl = line.substring(8, line.length() - 2);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
catch (MalformedURLException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
return shortUrl;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
String shorten;
|
||||
|
||||
try {
|
||||
shorten = GoogleUrlShortener.shorten4("https://dev.d4science.org/group/data-e-infrastructure-gateway/workspace?itemid=062c558c-c7ce-4de3-a4c7-e1411816cc12&operation=gotofolder");
|
||||
|
||||
System.out.println("Shorted: "+shorten);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,179 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.portlets.user.workspace;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.httpclient.Credentials;
|
||||
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpException;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
||||
import org.apache.commons.httpclient.UsernamePasswordCredentials;
|
||||
import org.apache.commons.httpclient.auth.AuthScope;
|
||||
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
|
||||
import org.apache.commons.httpclient.methods.GetMethod;
|
||||
import org.apache.commons.httpclient.methods.PostMethod;
|
||||
import org.apache.commons.httpclient.params.HttpMethodParams;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
* @Jun 4, 2013
|
||||
*
|
||||
*/
|
||||
public class HttpCallerUtil {
|
||||
|
||||
public static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(HttpCallerUtil.class);
|
||||
|
||||
public static final int TIME_OUT_REQUESTS = 5000;
|
||||
|
||||
|
||||
private String urlService = "";
|
||||
private HttpClient httpClient = null;
|
||||
private String username = "";
|
||||
private String password = "";
|
||||
|
||||
|
||||
public HttpCallerUtil(String url, String user, String password) {
|
||||
this.urlService = url;
|
||||
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
|
||||
connectionManager.getParams().setSoTimeout(TIME_OUT_REQUESTS);
|
||||
this.httpClient = new HttpClient(connectionManager);
|
||||
|
||||
this.username = user;
|
||||
this.password = password;
|
||||
Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
|
||||
httpClient.getState().setCredentials(AuthScope.ANY, defaultcreds);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String callGet(String urlMethod, Map<String, String> parameters) throws Exception {
|
||||
|
||||
// Create an instance of HttpClient.
|
||||
HttpClient client = new HttpClient();
|
||||
|
||||
String query = UrlEncoderUtil.encodeQuery(parameters);
|
||||
|
||||
String fullUrl = urlService+"/"+urlMethod+"?"+query;
|
||||
|
||||
logger.info("call get .... " + fullUrl);
|
||||
|
||||
// Create a method instance.
|
||||
GetMethod method = new GetMethod(fullUrl);
|
||||
|
||||
// Provide custom retry handler is necessary
|
||||
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
|
||||
|
||||
try {
|
||||
// Execute the method.
|
||||
int statusCode = client.executeMethod(method);
|
||||
|
||||
if (statusCode != HttpStatus.SC_OK) {
|
||||
logger.error("Method failed: " + method.getStatusLine());
|
||||
}
|
||||
|
||||
// Read the response body.
|
||||
byte[] responseBody = method.getResponseBody();
|
||||
|
||||
// Deal with the response.
|
||||
// Use caution: ensure correct character encoding and is not binary
|
||||
// data
|
||||
return new String(responseBody);
|
||||
|
||||
} catch (HttpException e) {
|
||||
logger.error("Fatal protocol violation: " + e.getMessage());
|
||||
throw new Exception("Fatal protocol violation: " + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
logger.error("Fatal transport error: " + e.getMessage());
|
||||
throw new Exception("Fatal transport violation: " + e.getMessage());
|
||||
} finally {
|
||||
// Release the connection.
|
||||
method.releaseConnection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String callPost(String urlMethod, String body, String contentType) throws Exception {
|
||||
|
||||
// Create a method instance.
|
||||
PostMethod method = new PostMethod(this.urlService + "/" + urlMethod);
|
||||
|
||||
method.setRequestHeader("Content-type", contentType);
|
||||
|
||||
logger.trace("call post .... " + this.urlService + "/" + urlMethod);
|
||||
logger.debug(" call post body.... " + body);
|
||||
// System.out.println("post body .... " + body);
|
||||
|
||||
method.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));
|
||||
|
||||
// Provide custom retry handler is necessary
|
||||
// method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(3, false));
|
||||
|
||||
byte[] responseBody = null;
|
||||
try {
|
||||
// Execute the method.
|
||||
int statusCode = httpClient.executeMethod(method);
|
||||
|
||||
if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
|
||||
logger.error("Method failed: " + method.getStatusLine()+"; Response bpdy: "+method.getResponseBody());
|
||||
method.releaseConnection();
|
||||
throw new Exception("Method failed: " + method.getStatusLine()+"; Response body: "+new String(method.getResponseBody()));
|
||||
}
|
||||
// Read the response body.
|
||||
responseBody = method.getResponseBody();
|
||||
|
||||
// Deal with the response.
|
||||
// Use caution: ensure correct character encoding and is not binary data
|
||||
// System.out.println(new String(responseBody));
|
||||
} catch (HttpException e) {
|
||||
logger.error("Fatal protocol violation: ", e);
|
||||
// e.printStackTrace();
|
||||
method.releaseConnection();
|
||||
throw new Exception("Fatal protocol violation: " + e.getMessage());
|
||||
} catch (Exception e) {
|
||||
logger.error("Fatal transport error: ", e);
|
||||
// e.printStackTrace();
|
||||
method.releaseConnection();
|
||||
throw new Exception("Fatal transport error: " + e.getMessage());
|
||||
}
|
||||
method.releaseConnection();
|
||||
return new String(responseBody);
|
||||
}
|
||||
|
||||
public String getUrlservice() {
|
||||
return urlService;
|
||||
}
|
||||
|
||||
public void setUrlservice(String urlservice) {
|
||||
this.urlService = urlservice;
|
||||
}
|
||||
|
||||
public HttpClient getClient() {
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public void setClient(HttpClient client) {
|
||||
this.httpClient = client;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
}
|
|
@ -14,7 +14,6 @@ import java.io.OutputStream;
|
|||
import org.gcube.common.core.scope.GCUBEScope;
|
||||
import org.gcube.common.core.scope.GCUBEScope.MalformedScopeExpressionException;
|
||||
import org.gcube.portlets.user.homelibrary.home.HomeLibrary;
|
||||
import org.gcube.portlets.user.homelibrary.home.HomeManagerFactory;
|
||||
import org.gcube.portlets.user.homelibrary.home.exceptions.HomeNotFoundException;
|
||||
import org.gcube.portlets.user.homelibrary.home.exceptions.InternalErrorException;
|
||||
import org.gcube.portlets.user.homelibrary.home.exceptions.UserNotFoundException;
|
||||
|
@ -24,9 +23,7 @@ import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.Insufficien
|
|||
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.ItemAlreadyExistException;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.WrongDestinationException;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.FolderItem;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ExternalFile;
|
||||
import org.gcube.portlets.user.homelibrary.home.workspace.folder.items.ExternalImage;
|
||||
import org.gcube.portlets.user.homelibrary.util.MimeTypeUtil;
|
||||
import org.gcube.portlets.user.homelibrary.util.WorkspaceUtil;
|
||||
|
||||
|
|
|
@ -0,0 +1,134 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.portlets.user.workspace;
|
||||
|
||||
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
||||
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||
import org.gcube.resources.discovery.client.queries.impl.XQuery;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
* @Jun 26, 2013
|
||||
*
|
||||
*/
|
||||
public class UriResolverReaderParameter {
|
||||
|
||||
//Base Address
|
||||
String uri = "";
|
||||
//Query URL parameter
|
||||
String parameter = "";
|
||||
private String uriRequest = "";
|
||||
|
||||
public static Logger log = Logger.getLogger(UriResolverReaderParameter.class);
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*
|
||||
*/
|
||||
public UriResolverReaderParameter() throws Exception {
|
||||
|
||||
ScopeProvider.instance.set("/gcube/devsec");
|
||||
|
||||
XQuery query = queryFor(ServiceEndpoint.class);
|
||||
|
||||
query.addCondition("$resource/Profile/Name/text() eq 'HTTP-URI-Resolver'").setResult("$resource/Profile/AccessPoint");
|
||||
|
||||
DiscoveryClient<AccessPoint> client = clientFor(AccessPoint.class);
|
||||
|
||||
List<AccessPoint> endpoints = client.submit(query);
|
||||
|
||||
if (endpoints.size() == 0)
|
||||
throw new Exception("No Resolver available");
|
||||
|
||||
//Base Address
|
||||
// System.out.println(endpoints.get(0).address());
|
||||
|
||||
uri = endpoints.get(0)!=null?endpoints.get(0).address():"";
|
||||
|
||||
if(endpoints.get(0)!=null){
|
||||
|
||||
parameter = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get("parameter").value():"";
|
||||
}
|
||||
|
||||
uriRequest = uri+"?"+parameter;
|
||||
|
||||
|
||||
//Query URL parameter
|
||||
// System.out.println(endpoints.get(0).propertyMap().get("parameter").value());
|
||||
|
||||
}
|
||||
|
||||
public InputStream resolve(String smp) throws IOException{
|
||||
String query = uriRequest + "="+smp;
|
||||
|
||||
URL url = new URL(query);
|
||||
|
||||
return url.openStream();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Base Address of Uri Resolver
|
||||
*/
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Query URL parameter of Uri Resolver
|
||||
*/
|
||||
public String getParameter() {
|
||||
return parameter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("UriResolverReaderParameter [uri=");
|
||||
builder.append(uri);
|
||||
builder.append(", parameter=");
|
||||
builder.append(parameter);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
log.trace(new UriResolverReaderParameter());
|
||||
|
||||
UriResolverReaderParameter uriResolver = new UriResolverReaderParameter();
|
||||
InputStream is = uriResolver.resolve("smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV+gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y");
|
||||
|
||||
File file = new File("test.png");
|
||||
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
|
||||
|
||||
IOUtils.copy(is, out);
|
||||
is.close();
|
||||
|
||||
out.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.portlets.user.workspace;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
* @Jun 4, 2013
|
||||
*
|
||||
*/
|
||||
public class UrlEncoderUtil {
|
||||
|
||||
public static String charset = "UTF-8";
|
||||
|
||||
public static Logger logger = Logger.getLogger(UrlEncoderUtil.class);
|
||||
/**
|
||||
*
|
||||
* @param url
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
public static String encodeQuery(String... parameters){
|
||||
|
||||
String query = "";
|
||||
for (String string : parameters) {
|
||||
|
||||
try {
|
||||
query+=URLEncoder.encode(string, charset)+"&";
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
logger.error(e);
|
||||
return query;
|
||||
}
|
||||
|
||||
}
|
||||
return removeLastChar(query);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param url
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
public static String encodeQuery(Map<String, String> parameters){
|
||||
|
||||
String query = "";
|
||||
|
||||
if(parameters==null)
|
||||
return query;
|
||||
|
||||
for (String key : parameters.keySet()) {
|
||||
|
||||
try {
|
||||
|
||||
query+=String.format(key+"=%s", URLEncoder.encode(parameters.get(key), charset))+"&";
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
logger.error(e);
|
||||
return query;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return removeLastChar(query);
|
||||
|
||||
}
|
||||
|
||||
public static String removeLastChar(String string){
|
||||
|
||||
if(string == null)
|
||||
return null;
|
||||
|
||||
if(string.length()>0)
|
||||
return string.substring(0, string.length()-1);
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// System.out.println(UrlEncoderUtil.encodeQuery("request=GetStyles", "layers=test Name", "service=WMS", "version=1.1.1"));
|
||||
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
parameters.put("request", "GetStyles");
|
||||
parameters.put("layers", "test Name");
|
||||
parameters.put("version", "1.1.1");
|
||||
|
||||
System.out.println(UrlEncoderUtil.encodeQuery(parameters));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue