incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From asa...@apache.org
Subject svn commit: r1404721 - in /sling/whiteboard/asanso/plugins/eclipse: slingclipse-api/src/org/apache/sling/slingclipse/api/ slingclipse-http/src/org/apache/sling/slingclipse/http/impl/ slingclipse-plugin/src/org/apache/sling/slingclipse/helper/ slingclip...
Date Thu, 01 Nov 2012 18:46:53 GMT
Author: asanso
Date: Thu Nov  1 18:46:53 2012
New Revision: 1404721

URL: http://svn.apache.org/viewvc?rev=1404721&view=rev
Log:
SLING-2636 - [Tooling] Add ability to Import from Sling Repository. Adding some import content
logic

Added:
    sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/ResponseType.java
Modified:
    sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/Repository.java
    sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/RepositoryInfo.java
    sling/whiteboard/asanso/plugins/eclipse/slingclipse-http/src/org/apache/sling/slingclipse/http/impl/RepositoryImpl.java
    sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/helper/SlingclipseHelper.java
    sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/ui/wizards/ImportWizard.java

Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/Repository.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/Repository.java?rev=1404721&r1=1404720&r2=1404721&view=diff
==============================================================================
--- sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/Repository.java
(original)
+++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/Repository.java
Thu Nov  1 18:46:53 2012
@@ -18,10 +18,19 @@ package org.apache.sling.slingclipse.api
 
 public interface Repository {
 	
+	public static String JCR_PRIMARY_TYPE= "jcr:primaryType";
+	public static String NT_FILE= "nt:file";
+	
 	//TODO change with properties
 	public void setRepositoryInfo(RepositoryInfo repositoryInfo);
 
 	public void addNode(FileInfo fileInfo);
 	
 	public void deleteNode(FileInfo fileInfo);
+ 
+	public String listChildrenNode(String path,ResponseType responseType);
+ 	
+	public String getNodeContent(String path,ResponseType responseType);
+
+	public byte[] getNode(String path);
 }

Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/RepositoryInfo.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/RepositoryInfo.java?rev=1404721&r1=1404720&r2=1404721&view=diff
==============================================================================
--- sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/RepositoryInfo.java
(original)
+++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/RepositoryInfo.java
Thu Nov  1 18:46:53 2012
@@ -41,4 +41,13 @@ public class RepositoryInfo {
 		return url;
 	}
 	
+	public String getHost(){
+		String host=url.substring(7);
+		return host.split(":")[0];
+	}
+	
+	public int getPort(){
+		String host=url.substring(7);
+		return Integer.parseInt(host.split(":")[1]);
+	}	
 }

Added: sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/ResponseType.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/ResponseType.java?rev=1404721&view=auto
==============================================================================
--- sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/ResponseType.java
(added)
+++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/ResponseType.java
Thu Nov  1 18:46:53 2012
@@ -0,0 +1,5 @@
+package org.apache.sling.slingclipse.api;
+
+public enum ResponseType {
+	JSON,XML
+}

Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-http/src/org/apache/sling/slingclipse/http/impl/RepositoryImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-http/src/org/apache/sling/slingclipse/http/impl/RepositoryImpl.java?rev=1404721&r1=1404720&r2=1404721&view=diff
==============================================================================
--- sling/whiteboard/asanso/plugins/eclipse/slingclipse-http/src/org/apache/sling/slingclipse/http/impl/RepositoryImpl.java
(original)
+++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-http/src/org/apache/sling/slingclipse/http/impl/RepositoryImpl.java
Thu Nov  1 18:46:53 2012
@@ -18,15 +18,18 @@ package org.apache.sling.slingclipse.htt
 
 import java.io.File;
 
+import org.apache.commons.httpclient.Credentials;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.multipart.FilePart;
 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
 import org.apache.commons.httpclient.methods.multipart.Part;
 import org.apache.commons.httpclient.methods.multipart.StringPart;
 import org.apache.sling.slingclipse.api.FileInfo;
+import org.apache.sling.slingclipse.api.ResponseType;
 
 public class RepositoryImpl extends AbstractRepository{
 	
@@ -67,5 +70,67 @@ public class RepositoryImpl extends Abst
 		}
 		
 	}
+	
+	@Override
+	public String listChildrenNode(String path,ResponseType responseType) {
+		//TODO handle the response type
+		GetMethod get= new GetMethod(repositoryInfo.getUrl()+path+".1.json");
+		try{
+			httpClient.getParams().setAuthenticationPreemptive(true);
+		    Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(),
repositoryInfo.getPassword());
+		    //TODO
+		    httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(),repositoryInfo.getPort(),
AuthScope.ANY_REALM), defaultcreds);
+			int responseStatus=httpClient.executeMethod(get);
+			//TODO change responseAsString with something like
+			//return EncodingUtil.getString(rawdata, m.getResponseCharSet());
+			return get.getResponseBodyAsString();
+			//TODO handle the response status
+		} catch (Exception e) {
+			return null;
+			//TODO handle the error
+		}finally{
+			get.releaseConnection();
+		}
+	}
 
+	@Override
+	public byte[] getNode(String path) {
+		GetMethod get= new GetMethod(repositoryInfo.getUrl()+path);
+		try{
+			httpClient.getParams().setAuthenticationPreemptive(true);
+		    Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(),
repositoryInfo.getPassword());
+		    //TODO
+		    httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(),repositoryInfo.getPort(),
AuthScope.ANY_REALM), defaultcreds);
+			int responseStatus=httpClient.executeMethod(get);
+			return get.getResponseBody(); 
+			//TODO handle the response status
+		} catch (Exception e) {
+			return null;
+			//TODO handle the error
+		}finally{
+			get.releaseConnection();
+		}
+	}
+	
+	
+	@Override
+	public String getNodeContent(String path,ResponseType responseType) {
+		//TODO handle the response type
+		GetMethod get= new GetMethod(repositoryInfo.getUrl()+path+".json");
+		try{
+			httpClient.getParams().setAuthenticationPreemptive(true);
+		    Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(),
repositoryInfo.getPassword());
+		    httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(),repositoryInfo.getPort(),
AuthScope.ANY_REALM), defaultcreds); 
+			int responseStatus=httpClient.executeMethod(get);
+			//TODO change responseAsString with something like
+			// return EncodingUtil.getString(rawdata, m.getResponseCharSet());
+			return get.getResponseBodyAsString(); 
+			//TODO handle the response status
+		} catch (Exception e) {
+			return null;
+			//TODO handle the error
+		}finally{
+			get.releaseConnection();
+		}
+	}
 }

Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/helper/SlingclipseHelper.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/helper/SlingclipseHelper.java?rev=1404721&r1=1404720&r2=1404721&view=diff
==============================================================================
--- sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/helper/SlingclipseHelper.java
(original)
+++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/helper/SlingclipseHelper.java
Thu Nov  1 18:46:53 2012
@@ -29,5 +29,13 @@ public class SlingclipseHelper {
 		int index= path.indexOf(JCR_ROOT)+JCR_ROOT.length();
         return path.substring(index);
 	}
+	
+	public static boolean isFilePath(String path){
+		return path.indexOf(".")!=-1;
+	}
+	
+	public static boolean isFolderPath(String path){
+		return path.indexOf(".")==-1;
+	}
 
 }

Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/ui/wizards/ImportWizard.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/ui/wizards/ImportWizard.java?rev=1404721&r1=1404720&r2=1404721&view=diff
==============================================================================
--- sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/ui/wizards/ImportWizard.java
(original)
+++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/ui/wizards/ImportWizard.java
Thu Nov  1 18:46:53 2012
@@ -16,6 +16,17 @@
  */
 package org.apache.sling.slingclipse.ui.wizards;
 
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.apache.sling.slingclipse.SlingclipsePlugin;
+import org.apache.sling.slingclipse.api.Repository;
+import org.apache.sling.slingclipse.api.RepositoryInfo;
+import org.apache.sling.slingclipse.api.ResponseType;
+import org.apache.sling.slingclipse.helper.SlingclipseHelper;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -26,6 +37,8 @@ import org.eclipse.jface.viewers.IStruct
 import org.eclipse.jface.wizard.Wizard;
 import org.eclipse.ui.IImportWizard;
 import org.eclipse.ui.IWorkbench;
+import org.json.JSONException;
+import org.json.JSONObject;
 
 /**
  * Renders the import wizard container page for the Slingclipse repository
@@ -47,12 +60,21 @@ public class ImportWizard extends Wizard
 	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
 	 */
 	public boolean performFinish() {
+		
+		try {
+			importFromRepository();
+		} catch ( Exception e) {
+			SlingclipsePlugin.getDefault().getLog().
+			log(new CoreException(new Status(Status.ERROR, SlingclipsePlugin.PLUGIN_ID, "Failed importing
repository ", e)).getStatus());
+		}
+		
 		if (mainPage.isPageComplete()) {
 			Job job = new Job("Import") {
 
 				protected IStatus run(IProgressMonitor monitor) {
 					monitor.setTaskName("Starting import...");
 					monitor.worked(10);
+					
 					// TODO: Actually run the job here
 					try {
 						long numMillisecondsToSleep = 5000; // 5 seconds
@@ -101,5 +123,39 @@ public class ImportWizard extends Wizard
 		super.addPages();
 		addPage(mainPage);
 	}
+	
+	private void importFromRepository() throws JSONException, IOException{
+		Repository repository = SlingclipsePlugin.getDefault().getRepository();
+		RepositoryInfo repositoryInfo = new RepositoryInfo(
+				mainPage.getUsername(),
+				mainPage.getPassword(),
+				mainPage.getRepositoryUrl());
+		repository.setRepositoryInfo(repositoryInfo);
+		
+		String repositoryPath=mainPage.getRepositoryPath();
+		
+		if (SlingclipseHelper.isFolderPath(repositoryPath)){
+			//handle the folder
+			String children=repository.listChildrenNode(repositoryPath,ResponseType.JSON);
+			//TODO add XML support			
+		}else{
+			//handle the file
+			String  nodeContent= repository.getNodeContent(repositoryPath, ResponseType.JSON);
+
+			JSONObject json = new JSONObject(nodeContent);
+			String primaryType= json.optString(Repository.JCR_PRIMARY_TYPE);
+			if (Repository.NT_FILE.equals(primaryType)){
+				byte [] node= repository.getNode(repositoryPath);
+				//TODO create file
+			}else{
+				//TODO
+				//handle the case that is not ntfile
+			} 			
+		}
+ 		
+	}
+	
+	private void createFile(String name, byte[] content) throws IOException{				
+	}
 
 }



Mime
View raw message