|
| 1 | +package net.servicestack.eclipse.handlers; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.ByteArrayInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import net.servicestack.eclipse.nativetypes.INativeTypesHandler; |
| 16 | +import net.servicestack.eclipse.nativetypes.JavaNativeTypesHandler; |
| 17 | + |
| 18 | +import org.eclipse.core.commands.AbstractHandler; |
| 19 | +import org.eclipse.core.commands.ExecutionEvent; |
| 20 | +import org.eclipse.core.commands.ExecutionException; |
| 21 | +import org.eclipse.core.resources.IFile; |
| 22 | +import org.eclipse.core.resources.IResource; |
| 23 | +import org.eclipse.core.runtime.CoreException; |
| 24 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 25 | +import org.eclipse.core.runtime.IStatus; |
| 26 | +import org.eclipse.core.runtime.MultiStatus; |
| 27 | +import org.eclipse.core.runtime.OperationCanceledException; |
| 28 | +import org.eclipse.core.runtime.Status; |
| 29 | +import org.eclipse.core.runtime.jobs.Job; |
| 30 | +import org.eclipse.swt.widgets.Shell; |
| 31 | +import org.eclipse.ui.IWorkbenchWindow; |
| 32 | +import org.eclipse.ui.PlatformUI; |
| 33 | +import org.eclipse.ui.handlers.HandlerUtil; |
| 34 | +import org.eclipse.ui.progress.UIJob; |
| 35 | +import org.eclipse.ui.services.ISourceProviderService; |
| 36 | +import org.eclipse.jface.dialogs.ErrorDialog; |
| 37 | +import org.eclipse.jface.dialogs.MessageDialog; |
| 38 | + |
| 39 | +/** |
| 40 | + * Our sample handler extends AbstractHandler, an IHandler base class. |
| 41 | + * @see org.eclipse.core.commands.IHandler |
| 42 | + * @see org.eclipse.core.commands.AbstractHandler |
| 43 | + */ |
| 44 | +public class UpdateReferenceHandler extends AbstractHandler { |
| 45 | + /** |
| 46 | + * The constructor. |
| 47 | + */ |
| 48 | + public UpdateReferenceHandler() { |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * the command has been executed, so extract extract the needed information |
| 53 | + * from the application context. |
| 54 | + */ |
| 55 | + public Object execute(ExecutionEvent event) throws ExecutionException { |
| 56 | + Job job = new Job("Updating ServiceStack Reference") { |
| 57 | + final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); |
| 58 | + @Override |
| 59 | + protected IStatus run(IProgressMonitor monitor) { |
| 60 | + |
| 61 | + monitor.beginTask("Updating ServiceStack Reference", 5); |
| 62 | + // Get the source provider service |
| 63 | + IFile fileToUpdate = UpdateReferenceCurrentSelection.getInstance().UpdateReferenceFile; |
| 64 | + if(fileToUpdate == null) { |
| 65 | + showErrorDialogOnUIThread(shell,"Unable to read ServiceStack reference from project."); |
| 66 | + return Status.CANCEL_STATUS; |
| 67 | + } |
| 68 | + String fileContents = ""; |
| 69 | + try { |
| 70 | + fileContents = getStringFromInputStream(fileToUpdate.getContents()); |
| 71 | + } catch (IOException e) { |
| 72 | + // TODO Auto-generated catch block |
| 73 | + e.printStackTrace(); |
| 74 | + } catch (CoreException e) { |
| 75 | + // TODO Auto-generated catch block |
| 76 | + e.printStackTrace(); |
| 77 | + } |
| 78 | + |
| 79 | + if(fileContents == null || fileContents.length() == 0) { |
| 80 | + showErrorDialogOnUIThread(shell,"Unable to read ServiceStack reference from project."); |
| 81 | + return Status.CANCEL_STATUS; |
| 82 | + } |
| 83 | + |
| 84 | + INativeTypesHandler nativeTypesHandler = new JavaNativeTypesHandler(); |
| 85 | + Map<String,String> options = nativeTypesHandler.parseComments(fileContents); |
| 86 | + String baseUrl = options.get("BaseUrl"); |
| 87 | + options.remove("BaseUrl"); |
| 88 | + String updatedCode = ""; |
| 89 | + monitor.worked(1); |
| 90 | + try { |
| 91 | + updatedCode = nativeTypesHandler.getUpdatedCode(baseUrl, options); |
| 92 | + } |
| 93 | + catch (FileNotFoundException e) { |
| 94 | + showErrorDialogOnUIThread(shell,"Failed to updated ServiceStack reference. Server responded with 404." + e.getLocalizedMessage()); |
| 95 | + |
| 96 | + return Status.CANCEL_STATUS; |
| 97 | + } |
| 98 | + catch (IOException e) { |
| 99 | + showErrorDialogOnUIThread(shell,"Failed to updated ServiceStack reference. " + e.getClass().getName() + " - " + e.getLocalizedMessage()); |
| 100 | + |
| 101 | + return Status.CANCEL_STATUS; |
| 102 | + } |
| 103 | + |
| 104 | + monitor.worked(3); |
| 105 | + if(updatedCode == null || updatedCode.length() == 0) { |
| 106 | + showErrorDialogOnUIThread(shell, "Failed to updated ServiceStack reference. Response was empty. Check BaseUrl."); |
| 107 | + return Status.CANCEL_STATUS; |
| 108 | + } |
| 109 | + if(!nativeTypesHandler.isFileAServiceStackReference(updatedCode)) { |
| 110 | + showErrorDialogOnUIThread(shell, "Response from server was not a valid ServiceStack reference. Check BaseUrl."); |
| 111 | + return Status.CANCEL_STATUS; |
| 112 | + } |
| 113 | + InputStream stream = new ByteArrayInputStream(updatedCode.getBytes(StandardCharsets.UTF_8)); |
| 114 | + try { |
| 115 | + fileToUpdate.setContents(stream, 0, null); |
| 116 | + fileToUpdate.refreshLocal(IResource.DEPTH_ZERO, null); |
| 117 | + } catch (CoreException e) { |
| 118 | + showErrorDialogOnUIThread(shell,"Failed to write ServiceStack reference. " + e.getLocalizedMessage()); |
| 119 | + return Status.CANCEL_STATUS; |
| 120 | + } |
| 121 | + monitor.worked(1); |
| 122 | + monitor.done(); |
| 123 | + return Status.OK_STATUS; |
| 124 | + } |
| 125 | + |
| 126 | + }; |
| 127 | + |
| 128 | + job.schedule(); |
| 129 | + |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + private void showErrorDialogOnUIThread(final Shell shell, final String message) { |
| 134 | + UIJob uiJob = new UIJob("update servicestack reference error") { |
| 135 | + |
| 136 | + @Override |
| 137 | + public IStatus runInUIThread(IProgressMonitor monitor) { |
| 138 | + MessageDialog.openError( |
| 139 | + shell, |
| 140 | + "Error", |
| 141 | + message); |
| 142 | + return Status.OK_STATUS; |
| 143 | + } |
| 144 | + |
| 145 | + }; |
| 146 | + uiJob.schedule(); |
| 147 | + } |
| 148 | + |
| 149 | + private String getStringFromInputStream(InputStream inputStream) throws IOException { |
| 150 | + InputStreamReader is = new InputStreamReader(inputStream); |
| 151 | + StringBuilder sb=new StringBuilder(); |
| 152 | + BufferedReader br = new BufferedReader(is); |
| 153 | + String read = br.readLine(); |
| 154 | + |
| 155 | + while(read != null) { |
| 156 | + //System.out.println(read); |
| 157 | + sb.append(read); |
| 158 | + sb.append(System.lineSeparator()); |
| 159 | + read =br.readLine(); |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + return sb.toString(); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments