1111
1212import java .net .MalformedURLException ;
1313import java .net .URI ;
14+ import java .net .URISyntaxException ;
1415import java .sql .SQLException ;
1516
1617import static io .javalin .rendering .template .TemplateUtil .model ;
@@ -32,10 +33,10 @@ public static void show(Context ctx) throws SQLException {
3233 ctx .render ("urls/show.jte" , model ("page" , page ));
3334 }
3435
35- public static void create (Context ctx ) throws SQLException {
36+ public static void create (Context ctx ) throws SQLException , URISyntaxException {
3637
3738 try {
38- var url = ctx .formParamAsClass ("url" , URI . class ). get ( ).normalize ().toURL ();
39+ var url = new URI ( ctx .formParam ("url" ) ).normalize ().toURL ();
3940 var protocol = url .getProtocol ();
4041 var authority = url .getAuthority ();
4142 var urlName = String .format ("%s://%s" , protocol , authority );
@@ -51,19 +52,15 @@ public static void create(Context ctx) throws SQLException {
5152 ctx .redirect (NamedRoutes .rootPath ());
5253
5354 } catch (MalformedURLException | IllegalArgumentException e ) {
54- var page = new MainPage (
55- ctx .sessionAttribute ("currentUser" ),
56- ctx .formParam ("url" )
57- );
5855
5956 if (e instanceof MalformedURLException ) {
60- page . setFlash ( "Некорректный URL" );
57+ ctx . sessionAttribute ( "flash" , "Некорректный URL" );
6158 } else {
62- page . setFlash ( e .getMessage ());
59+ ctx . sessionAttribute ( "flash" , e .getMessage ());
6360 }
6461
65- page . setFlashType ( "alert alert-danger" );
66- ctx .render (NamedRoutes .rootPath (), model ( "page" , page ));
62+ ctx . sessionAttribute ( "flash-type" , "alert alert-danger" );
63+ ctx .redirect (NamedRoutes .rootPath ());
6764 }
6865 }
6966}
0 commit comments